Ejemplo n.º 1
0
        /// <summary>
        /// Reads the contents of the file.
        /// </summary>
        /// <param name="content">The content of the file downloaded</param>
        /// <returns>Decrypted content of the file</returns>
        protected override String ReadContent(Byte[] content)
        {
            CIRegistry registry  = new CIRegistry();
            String     secureKey = registry.ReadSecureKey();

            RijndaelManaged    crypto       = new RijndaelManaged();
            Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(registry.ReadSecureKey(), new Byte[] { 90, 32, 230, 20, 59, 78, 112, 183 });

            Byte[] key = derivedBytes.GetBytes(crypto.KeySize / 8);
            Byte[] iv  = derivedBytes.GetBytes(crypto.BlockSize / 8);

            using (MemoryStream ms = new MemoryStream(content))
                using (CryptoStream cs = new CryptoStream(ms, crypto.CreateDecryptor(key, iv), CryptoStreamMode.Read))
                    using (MemoryStream msOut = new MemoryStream())
                    {
                        Int32 buffer;
                        while ((buffer = cs.ReadByte()) != -1)
                        {
                            msOut.WriteByte((Byte)buffer);
                        }

                        msOut.Flush();
                        return(Encoding.Default.GetString(msOut.ToArray()));
                    }
        }
        /// <summary>
        /// Reads the contents of the file.
        /// </summary>
        /// <param name="content">The content of the file downloaded</param>
        /// <returns>Decrypted content of the file</returns>
        protected override String ReadContent(Byte[] content)
        {
            CIRegistry registry = new CIRegistry();
            String secureKey = registry.ReadSecureKey();

            RijndaelManaged crypto = new RijndaelManaged();
            Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(registry.ReadSecureKey(), new Byte[] { 90, 32, 230, 20, 59, 78, 112, 183 });
            Byte[] key = derivedBytes.GetBytes(crypto.KeySize / 8);
            Byte[] iv = derivedBytes.GetBytes(crypto.BlockSize / 8);

            using(MemoryStream ms = new MemoryStream(content))
            using(CryptoStream cs = new CryptoStream(ms, crypto.CreateDecryptor(key, iv), CryptoStreamMode.Read))
            using (MemoryStream msOut = new MemoryStream())
            {
                Int32 buffer;
                while ((buffer = cs.ReadByte()) != -1)
                    msOut.WriteByte((Byte)buffer);

                msOut.Flush();
                return Encoding.Default.GetString(msOut.ToArray());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Encrypts the file.
        /// </summary>
        /// <param name="inputFile">The input file.</param>
        /// <param name="outputFile">The output file.</param>
        private static void EncryptFile(String inputFile, String outputFile)
        {
            CIRegistry registry = new CIRegistry();
          
            RijndaelManaged crypto = new RijndaelManaged();
            Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(registry.ReadSecureKey(), new Byte[] { 90, 32, 230, 20, 59, 78, 112, 183 });
            Byte[] key = derivedBytes.GetBytes(crypto.KeySize / 8);
            Byte[] iv = derivedBytes.GetBytes(crypto.BlockSize / 8);

            Byte[] buffer = null;
            using (FileStream inFile = new FileStream(inputFile, FileMode.Open))
            {
                buffer = new Byte[inFile.Length];
                inFile.Read(buffer, 0, buffer.Length);
            }

            using(FileStream outFile = new FileStream(outputFile, FileMode.Create))
            using(CryptoStream cs = new CryptoStream(outFile, crypto.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                cs.Write(buffer, 0, buffer.Length);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Encrypts the file.
        /// </summary>
        /// <param name="inputFile">The input file.</param>
        /// <param name="outputFile">The output file.</param>
        private static void EncryptFile(String inputFile, String outputFile)
        {
            CIRegistry registry = new CIRegistry();

            RijndaelManaged    crypto       = new RijndaelManaged();
            Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(registry.ReadSecureKey(), new Byte[] { 90, 32, 230, 20, 59, 78, 112, 183 });

            Byte[] key = derivedBytes.GetBytes(crypto.KeySize / 8);
            Byte[] iv  = derivedBytes.GetBytes(crypto.BlockSize / 8);

            Byte[] buffer = null;
            using (FileStream inFile = new FileStream(inputFile, FileMode.Open))
            {
                buffer = new Byte[inFile.Length];
                inFile.Read(buffer, 0, buffer.Length);
            }

            using (FileStream outFile = new FileStream(outputFile, FileMode.Create))
                using (CryptoStream cs = new CryptoStream(outFile, crypto.CreateEncryptor(key, iv), CryptoStreamMode.Write))
                {
                    cs.Write(buffer, 0, buffer.Length);
                }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Downloads a file from an http endpoint
        /// </summary>
        /// <param name="url">Location of the file to be downloaded</param>
        /// <returns>Contents of the file downloaded</returns>
        private String DownloadFile(String url)
        {
            // Grab the user data timeout from the registry
            CIRegistry registry        = new CIRegistry(this.eventLog);
            Int32      timeout         = registry.ReadUserDataTimeout();
            Boolean    isBase64Encoded = registry.IsBase64Encoded();

            // Make a web request to the provided url
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method  = "GET";
            request.Timeout = timeout;

            // Get the response from the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                // Read the steam into a string
                String content = reader.ReadToEnd();

                reader.Close();
                response.Close();

                // Return the file contents
                if (isBase64Encoded)
                {
                    Byte[] data = Convert.FromBase64String(content);
                    return(Encoding.UTF8.GetString(data));
                }
                else
                {
                    return(content);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Executes the main code for this service
        /// </summary>
        /// <param name="state">The service object</param>
        public static void Main(Object state)
        {
            // Set the event log from the servicebase
            EventLog EventLog = state == null ? new EventLog("Application") : ((ServiceBase)state).EventLog;

            // Load registry settings
            CIRegistry registry = new CIRegistry(EventLog);
            var        address  = registry.ReadUserDataFileKey();
            var        logFile  = registry.ReadLogFileKey();

            LogFile = logFile;

            WriteOutput("Starting Service");

            // Load all the assemblies and scan the types
            CIAssemblyLoader.Configure();

            ObjectFactory.Configure(c =>
            {
                foreach (var type in CIAssemblyLoader.TypesToScan)
                {
                    // Make sure the type is assignable to IInputModule
                    if (typeof(IInputModule).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
                    {
                        // Run the init method to configure this database endpoint
                        IInputModule obj = Activator.CreateInstance(type) as IInputModule;
                        CIEngine.Headers.Add(obj.Header);

                        // Set the properties for this object
                        obj.EventLog = EventLog;
                        obj.LogFile  = logFile;

                        // Add the object to the ObjectFactory
                        c.For <IInputModule>().Singleton().Use(obj).Named(obj.Header);

                        WriteOutput(String.Format("Adding module: {0}", type.ToString()));
                    }
                }
            });

            try
            {
                // Execute the script
                CIEngine engine = new CIEngine(EventLog, logFile);
                engine.Execute(address);
            }
            catch (Exception ex)
            {
                WriteOutput(ex.Message);
                WriteOutput(ex.StackTrace);
            }

            // Get the log file
            FileInfo info = new FileInfo(logFile);

            foreach (var type in CIAssemblyLoader.TypesToScan)
            {
                // Make sure the type is assignable to INotificationProvider
                if (typeof(INotificationProvider).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
                {
                    INotificationProvider obj = Activator.CreateInstance(type) as INotificationProvider;

                    // Make sure the notification is active before sending the notify command
                    if (obj.IsActive)
                    {
                        using (FileStream stream = info.OpenRead())
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                // Send the log file via this notification provider
                                CloudInitSession session = new CloudInitSession(reader.ReadToEnd());
                                obj.Notify(session);
                            }
                    }
                }
            }

            if (state != null)
            {
                ServiceBase service = (ServiceBase)state;

                // Disable this service if run once is enabled
                if (registry.ReadRunOnceKey())
                {
                    ServiceController sc = new ServiceController(service.ServiceName);
                    ServiceHelper.ChangeStartMode(sc, ServiceStartMode.Disabled);
                }

                // Stop the service
                service.Stop();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Downloads a file from an http endpoint
        /// </summary>
        /// <param name="url">Location of the file to be downloaded</param>
        /// <returns>Contents of the file downloaded</returns>
        private String DownloadFile(String url)
        {
            // Grab the user data timeout from the registry
            CIRegistry registry = new CIRegistry(this.eventLog);
            Int32 timeout = registry.ReadUserDataTimeout();
            Boolean isBase64Encoded = registry.IsBase64Encoded();

            // Make a web request to the provided url
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.Timeout = timeout;

            // Get the response from the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                // Read the steam into a string
                String content = reader.ReadToEnd();
                
                reader.Close();
                response.Close();
                
                // Return the file contents
                if (isBase64Encoded)
                {
                    Byte[] data = Convert.FromBase64String(content);
                    return Encoding.UTF8.GetString(data);
                }
                else
                {
                    return content;
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Executes the main code for this service
        /// </summary>
        /// <param name="state">The service object</param>
        public static void Main(Object state)
        {
            // Set the event log from the servicebase
            EventLog EventLog = state == null ? new EventLog("Application") : ((ServiceBase)state).EventLog;

            // Load registry settings
            CIRegistry registry = new CIRegistry(EventLog);
            var address = registry.ReadUserDataFileKey();
            var logFile = registry.ReadLogFileKey();

            LogFile = logFile;

            WriteOutput("Starting Service");

            // Load all the assemblies and scan the types
            CIAssemblyLoader.Configure();

            ObjectFactory.Configure(c =>
            {
                foreach (var type in CIAssemblyLoader.TypesToScan)
                {
                    // Make sure the type is assignable to IInputModule
                    if (typeof(IInputModule).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
                    {
                        // Run the init method to configure this database endpoint
                        IInputModule obj = Activator.CreateInstance(type) as IInputModule;
                        CIEngine.Headers.Add(obj.Header);

                        // Set the properties for this object
                        obj.EventLog = EventLog;
                        obj.LogFile = logFile;

                        // Add the object to the ObjectFactory
                        c.For<IInputModule>().Singleton().Use(obj).Named(obj.Header);

                        WriteOutput(String.Format("Adding module: {0}", type.ToString()));
                    }
                }
            });

            try
            {
                // Execute the script
                CIEngine engine = new CIEngine(EventLog, logFile);
                engine.Execute(address);
            }
            catch (Exception ex)
            {
                WriteOutput(ex.Message);
                WriteOutput(ex.StackTrace);
            }

            // Get the log file
            FileInfo info = new FileInfo(logFile);

            foreach (var type in CIAssemblyLoader.TypesToScan)
            {
                // Make sure the type is assignable to INotificationProvider
                if (typeof(INotificationProvider).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
                {
                    INotificationProvider obj = Activator.CreateInstance(type) as INotificationProvider;

                    // Make sure the notification is active before sending the notify command
                    if (obj.IsActive)
                    {
                        using(FileStream stream = info.OpenRead())
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            // Send the log file via this notification provider
                            CloudInitSession session = new CloudInitSession(reader.ReadToEnd());
                            obj.Notify(session);
                        }
                    }
                }
            }

            if (state != null)
            {
                ServiceBase service = (ServiceBase)state;

                // Disable this service if run once is enabled
                if (registry.ReadRunOnceKey())
                {
                    ServiceController sc = new ServiceController(service.ServiceName);
                    ServiceHelper.ChangeStartMode(sc, ServiceStartMode.Disabled);
                }

                // Stop the service
                service.Stop();
            }
        }