Esempio n. 1
0
 public LocalAuditFileInfo(LocalEnvironment env, FileInfo f) : base(env, f.FullName)
 {
     this.LocalAuditEnvironment = env;
     this.file     = f;
     this.Name     = this.file.Name;
     this.FullName = this.file.FullName;
 }
Esempio n. 2
0
 public LocalAuditFileInfo(LocalEnvironment env, string file_path) : base(env, file_path)
 {
     this.LocalAuditEnvironment = env;
     this.file     = new FileInfo(file_path);
     this.Name     = this.file.Name;
     this.FullName = this.file.FullName;
 }
 public LocalAuditDirectoryInfo(LocalEnvironment env, string dir_path) : base(env, dir_path)
 {
     if (string.IsNullOrEmpty(dir_path))
     {
         throw new ArgumentException("Value cannot be null.", "dir_path");
     }
     this.directory = new DirectoryInfo(dir_path);
     this.Name      = this.directory.Name;
     this.FullName  = this.directory.FullName;
 }
 public WinRmAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string host, string user, SecureString pass, LocalEnvironment host_environment) : base(message_handler, new OperatingSystem(PlatformID.Win32NT, new Version(0, 0)), host_environment)
 {
     this.Manager     = new MachineManager(host, user, pass);
     this.IsConnected = true;
 }
        public WinRmAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, IPAddress address, string user, SecureString pass, LocalEnvironment host_environment) : base(message_handler, new OperatingSystem(PlatformID.Win32NT, new Version(0, 0)), host_environment)
        {
            try
            {
                this.HostEnvironment.Status("Connecting to Windows host address {0}", address.ToString());
                this.Manager = new MachineManager(address.ToString(), user, pass);
                ICollection <dynamic> r = this.Manager.RunScript(@"{ Get-WMIObject Win32_OperatingSystem -ComputerName . | select-object Description, Caption,OSArchitecture, ServicePackMajorVersion
                                                                Get-WmiObject -Class Win32_ComputerSystem -Property Name

                }");
                if (r == null || r.Count < 2)
                {
                    this.HostEnvironment.Error("Could not remotely execute PowerShell command on Windows host address {0}.", address.ToString());
                    this.IsConnected = false;
                }
                else
                {
                    PSObject os   = r.First();
                    PSObject name = r.Last();
                    this.OSCaption    = (string)os.Properties["Caption"].Value;
                    this.ComputerName = (string)name.Properties["Name"].Value;
                    this.IsConnected  = true;
                    this.HostEnvironment.Success("Connected to Windows host address {0}. Computer name: {1}. Windows version: {2}.", this.Manager.IpAddress, this.ComputerName, this.OSCaption);
                }
            }
            catch (TrustedHostMissingException te)
            {
                this.HostEnvironment.Error(te, "Error connecting to Windows host address {0}. You must add the host address to the TrustedHosts list of the WinRM client for your computer", address.ToString());
                this.IsConnected = false;
            }
            catch (Exception e)
            {
                this.HostEnvironment.Error(e, "Error connecting to Windows host address {0}.", address.ToString());
                this.IsConnected = false;
                return;
            }
        }
 public GitHubAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string user_api_token, string repository_owner, string repository_name, string repository_branch, LocalEnvironment host_environment)
     : base(message_handler, new OperatingSystem(PlatformID.Unix, new Version(0, 0)), host_environment)
 {
     GitHubClient = new GitHubClient(new ProductHeaderValue("DevAudit"));
     if (!string.IsNullOrEmpty(user_api_token))
     {
         GitHubClient.Credentials = new Credentials(user_api_token);
     }
     try
     {
         Repository = GitHubClient.Repository.Get(repository_owner, repository_name).Result;
     }
     catch (AggregateException ae)
     {
         host_environment.Error(ae, "Error getting repository {0}/{1}.", repository_owner, repository_name);
         RepositoryInitialised = false;
         return;
     }
     catch (Exception e)
     {
         host_environment.Error(e, "Error getting repository {0}/{1}.", repository_owner, repository_name);
         RepositoryInitialised = false;
         return;
     }
     try
     {
         RepositoryBranch = GitHubClient.Repository.Branch.Get(repository_owner, repository_name, repository_branch).Result;
     }
     catch (AggregateException ae)
     {
         host_environment.Error(ae, "Error getting branch {0}.", repository_branch);
         RepositoryInitialised = false;
         return;
     }
     catch (Exception e)
     {
         host_environment.Error(e, "Error getting branch {0}.", repository_branch);
         RepositoryInitialised = false;
         return;
     }
     RepositoryName  = repository_name;
     RepositoryOwner = repository_owner;
     Success("Connected to GitHub repository {0}/{1} on branch {2}.", RepositoryOwner, RepositoryName, RepositoryBranch.Name);
     this.RepositoryInitialised = true;
 }
        public DockerAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string container, OperatingSystem os, LocalEnvironment host_environment) :
            base(message_handler, os, host_environment)
        {
            ProcessExecuteStatus process_status;
            string process_output, process_error;
            bool   container_exists = false;
            bool   container_running = false;
            string command = "docker", arguments = "ps -a";
            bool   r;

            if (this.HostEnvironment.IsDockerContainer)
            {
                r = this.HostEnvironment.Execute("chroot", " /hostroot " + command + " " + arguments, out process_status, out process_output, out process_error);
            }
            else
            {
                r = this.HostEnvironment.Execute("docker", "ps -a", out process_status, out process_output, out process_error);
            }
            if (r)
            {
                string[] p = process_output.Split(this.HostEnvironment.LineTerminator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (int i = 1; i < p.Count(); i++)
                {
                    if (string.IsNullOrEmpty(p [i]) || string.IsNullOrWhiteSpace(p [i]))
                    {
                        continue;
                    }
                    if (p[i].Trim().StartsWith(container) || p[i].Trim().EndsWith(container))
                    {
                        container_exists = true;
                        if (p [i].Contains("Up "))
                        {
                            container_running = true;
                        }
                        break;
                    }
                }
                if (container_exists)
                {
                    this.Container        = container;
                    this.ContainerRunning = container_running;
                    this.HostEnvironment.Success("Found Docker container with id or name {0}.", this.Container);
                }
                else
                {
                    this.HostEnvironment.Error("The Docker container with name or id {0} does not exist.", container);
                }
            }
            else
            {
                this.HostEnvironment.Error("Error executing command docker ps -a: {0}.", process_error);
            }
        }
Esempio n. 8
0
        public SshAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string client, string host_name, int port, string user, object pass, string keyfile, OperatingSystem os, LocalEnvironment host_environment)
            : base(message_handler, os, host_environment)
        {
            ConnectionInfo ci;

            Info("Connecting to {0}:{1}...", host_name, port);
            if (string.IsNullOrEmpty(keyfile))
            {
                ci = new ConnectionInfo(host_name, port, user, new PasswordAuthenticationMethod(user, ToInsecureString(pass)));
            }
            else if (!string.IsNullOrEmpty(keyfile) && pass != null)
            {
                ci = new ConnectionInfo(host_name, port, user, new PrivateKeyAuthenticationMethod(user,
                                                                                                  new PrivateKeyFile[] { new PrivateKeyFile(keyfile, ToInsecureString(pass)) }));
            }
            else
            {
                ci = new ConnectionInfo(host_name, port, user, new PrivateKeyAuthenticationMethod(user));
            }
            SshClient = new SshClient(ci);
            SshClient.ErrorOccurred   += SshClient_ErrorOccurred;
            SshClient.HostKeyReceived += SshClient_HostKeyReceived;
            SshClient.ConnectionInfo.AuthenticationBanner += Ci_AuthenticationBanner;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            try
            {
                SshClient.Connect();
            }
            catch (SshConnectionException ce)
            {
                Error("Connection error connecting to {0} : {1}", host_name, ce.Message);
                return;
            }
            catch (SshAuthenticationException ae)
            {
                Error("Authentication error connecting to {0} : {1}", host_name, ae.Message);
                return;
            }
            catch (System.Net.Sockets.SocketException se)
            {
                Error("Socket error connecting to {0} : {1}", host_name, se.Message);
                return;
            }
            finally
            {
                sw.Stop();
            }
            if (!SshClient.IsConnected || !SshClient.ConnectionInfo.IsAuthenticated)
            {
                Error("Failed to connect or authenticate to {0}", host_name);
                return;
            }
            this.IsConnected     = true;
            this.User            = user;
            this.HostName        = host_name;
            this.ssh_client_pass = pass;
            Success("Connected to {0} in {1} ms.", host_name, sw.ElapsedMilliseconds);
            string tmp_dir = Environment.OSVersion.Platform == PlatformID.Win32NT ? Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.User) : "/tmp";

            if (!string.IsNullOrEmpty(tmp_dir) && Directory.Exists(tmp_dir))
            {
                this.WorkDirectory = new DirectoryInfo(Path.Combine(tmp_dir, "devaudit-work", this.GetTimestamp()));
            }
            else
            {
                Warning("Could not get value of temporary directory from environment. The work directory wll be created in the DevAudit root directory.");
                this.WorkDirectory = new DirectoryInfo(Path.Combine("work", this.GetTimestamp()));
            }
            if (!this.WorkDirectory.Exists)
            {
                this.WorkDirectory.Create();
                this.WorkDirectory.Refresh();
                Debug("Created work directory {0}.", this.WorkDirectory.FullName);
            }
            Info("Using work directory: {0}.", this.WorkDirectory.FullName);
        }
        public DockerAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string container, OperatingSystem os, LocalEnvironment host_environment) :
            base(message_handler, os, host_environment)
        {
            Tuple <bool, bool> container_status = this.GetContainerStatus(container);

            if (container_status.Item1)
            {
                this.Container        = container;
                this.ContainerRunning = container_status.Item2;
                this.HostEnvironment.Success("Found Docker container with id or name {0}.", this.Container);
                this.GetOSName();
                this.GetOSVersion();
            }
            else
            {
                this.HostEnvironment.Error("The Docker container with name or id {0} does not exist.", container);
            }
        }
Esempio n. 10
0
 public AuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, OperatingSystem os, LocalEnvironment host_environment)
 {
     this.OS = os;
     if (OS.Platform == PlatformID.Win32NT)
     {
         this.LineTerminator = "\r\n";
         this.PathSeparator  = "\\";
     }
     else
     {
         this.LineTerminator = "\n";
         this.PathSeparator  = "/";
     }
     this.MessageHandler  = message_handler;
     this.HostEnvironment = host_environment;
 }
Esempio n. 11
0
 public ScriptEnvironment(LocalEnvironment host_env)
 {
     this.HostEnvironment = host_env;
 }
        public SshDockerAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string client, string host_name, int port, string user, object pass, string keyfile, string container, OperatingSystem os, LocalEnvironment host_environment)
            : base(message_handler, client, host_name, port, user, pass, keyfile, os, host_environment)
        {
            ProcessExecuteStatus process_status;
            string process_output, process_error;
            bool   container_exists  = false;
            bool   container_running = false;
            bool   r = base.Execute("docker", "ps -a", out process_status, out process_output, out process_error);

            if (r)
            {
                string[] p = process_output.Split(this.LineTerminator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (int i = 1; i < p.Count(); i++)
                {
                    if (string.IsNullOrEmpty(p[i]) || string.IsNullOrWhiteSpace(p[i]))
                    {
                        continue;
                    }
                    if (p[i].Trim().StartsWith(container) || p[i].Trim().EndsWith(container))
                    {
                        container_exists = true;
                        if (p[i].Contains("Up "))
                        {
                            container_running = true;
                        }
                        break;
                    }
                }
                if (container_exists)
                {
                    this.Container        = container;
                    this.ContainerRunning = container_running;
                    this.Success("Found Docker container with id or name {0}.", this.Container);
                    this.GetOSName();
                    this.GetOSVersion();
                }
                else
                {
                    this.Error("The Docker container with name or id {0} does not exist.", container);
                }
            }
            else
            {
                this.Error("Error executing command docker ps -a: {0}.", process_error);
            }
        }
Esempio n. 13
0
        public GitLabAuditEnvironment(EventHandler <EnvironmentEventArgs> message_handler, string api_token, string host_url, string project_name, string repository_branch, LocalEnvironment host_environment)
            : base(message_handler, new OperatingSystem(PlatformID.Unix, new Version(0, 0)), host_environment)
        {
            try
            {
                GitLabClient = new GitLabClient(host_url, api_token);
                IEnumerable <Project> projects = GitLabClient.Projects.Accessible().Result;
                Project = projects.Where(p => p.PathWithNamespace == project_name).FirstOrDefault();
                if (Project == null)
                {
                    host_environment.Error("Could not find project {0}.", project_name);
                    RepositoryInitialised = false;
                    return;
                }
                Repository = GitLabClient.GetRepository(Project.Id);
                if (Repository == null)
                {
                    host_environment.Error("Could not get repository for project {0}.", project_name);
                    RepositoryInitialised = false;
                    return;
                }
            }
            catch (AggregateException ae)
            {
                host_environment.Error(ae, "Error getting repository for project {0} from {1}.", project_name, host_url);
                RepositoryInitialised = false;
                return;
            }
            catch (Exception e)
            {
                host_environment.Error(e, "Error getting repository for project {0} from {1}.", project_name, host_url);
                RepositoryInitialised = false;
                return;
            }

            try
            {
                RepositoryBranch = Repository.Branches.All().Result.Where(b => b.Name == repository_branch).FirstOrDefault();
            }
            catch (AggregateException ae)
            {
                host_environment.Error(ae, "Error getting branch {0}.", repository_branch);
                RepositoryInitialised = false;
                return;
            }
            catch (Exception e)
            {
                host_environment.Error(e, "Error getting branch {0}.", repository_branch);
                RepositoryInitialised = false;
                return;
            }

            this.RepositoryInitialised = true;
        }