예제 #1
0
        public string GetPortProcessName(int X)
        {
            string             proc = "-1";
            PowerShellExecutor pse  = new PowerShellExecutor();

            string[] st = pse.RunShellScript("$nets = netstat -bano|select-string 'LISTENING|UDP'; foreach ($n in $nets)    {    $p = $n -replace ' +',' ';    $nar = $p.Split(' ');    $pname = $(Get-Process -id $nar[-1]).ProcessName;    $n -replace \"$($nar[-1])\",\"$($ppath) $($pname)\";     }").Split('\n');
            foreach (var line in st.Skip(4))
            {
                string str2 = Regex.Replace(line, @"\s+", ";");
                str2 = str2.Trim(';');
                str2 = str2.Replace(";LISTENING;", ";");
                string[] lb   = str2.Split(';');
                string[] port = lb[1].Split(':');

                try{
                    if (Convert.ToInt16(port[1]) == X)
                    {
                        return(lb[3]);
                    }
                }
                catch (Exception e) {
                    continue;
                }
            }

            return(proc);
        }
예제 #2
0
        public string GetJson()
        {
            PowerShellExecutor pse = new PowerShellExecutor();
            IDictionary <int, OpenPortModel> list = new Dictionary <int, OpenPortModel>();
            int i = 0;

            string[] st = pse.RunShellScript("$nets = netstat -bano|select-string 'LISTENING|UDP'; foreach ($n in $nets)    {    $p = $n -replace ' +',' ';    $nar = $p.Split(' ');    $pname = $(Get-Process -id $nar[-1]).ProcessName;    $n -replace \"$($nar[-1])\",\"$($ppath) $($pname)\";     }").Split('\n');

            foreach (var line in st.Skip(4))
            {
                //Todo : Issue where process name is not parsed correctly
                Console.WriteLine(line);
                string str2 = Regex.Replace(line, @"\s+", ";");
                str2 = str2.Trim(';');
                str2 = str2.Replace(";LISTENING;", ";");
                string[] lb = str2.Split(';');
                try
                {
                    list.Add(i, new OpenPortModel(lb[0], lb[1], lb[2]));
                }catch (Exception e)
                {
                    //Hahaha
                }
                i++;
            }

            SetNumber(i);
            return(JsonConvert.SerializeObject(list));
        }
예제 #3
0
        public string SleepWindows()
        {
            try
            {
                PowerShellExecutor pse = new PowerShellExecutor();
                pse.RunShellScript("shutdown /l");
                return("0");

                ManagementObject classInstance = new ManagementObject("root\\CIMV2", "Win32_OperatingSystem.ReplaceKeyPropery='ReplaceKeyPropertyValue'", null);

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = classInstance.InvokeMethod("Sleep", null, null);
                return(outParams["ReturnValue"].ToString());
            }
            catch (ManagementException err)
            {
                return("-1");
            }
        }
예제 #4
0
파일: Agent.cs 프로젝트: ozch/MIB_Agent
        public string Executioner(MySqlConnection con, string mac, Agent tm)
        {
            string       stm = string.Format("SELECT * FROM mib.execute where mac_address='{0}'", mac);
            MySqlCommand cmd = con.CreateCommand();

            cmd.CommandText = stm;

            MySqlDataReader scal = cmd.ExecuteReader();

            scal.Read();
            int boot_flag    = Convert.ToInt16(scal[1]);
            int service_flag = Convert.ToInt16(scal[2]);
            int kill_flag    = Convert.ToInt16(scal[3]);
            int script_flag  = Convert.ToInt16(scal[4]);
            int port_flag    = Convert.ToInt16(scal[5]);

            string boot_command = Convert.ToString(scal[6]);
            string service_name = Convert.ToString(scal[7]);
            string kill_name    = Convert.ToString(scal[8]);
            string script       = Convert.ToString(scal[9]);
            int    portno       = Convert.ToInt16(scal[10]);

            scal.Close();


            if (service_flag == 1)
            {
                try {
                    ServiceController sc  = new ServiceController();
                    string[]          val = service_name.Split(';');
                    string            re  = "";
                    if (val[0].Equals("start", StringComparison.InvariantCultureIgnoreCase))
                    {
                        re = sc.StartService(val[1]);
                        if (re == "0")
                        {
                            service_name = "started";
                        }
                        service_flag = 0;
                    }
                    else if (val[0].Equals("stop", StringComparison.InvariantCultureIgnoreCase))
                    {
                        re = sc.StopService(val[1]);
                        if (re == "0")
                        {
                            service_flag = 0;
                        }
                        service_name = "stopped";
                    }
                    else if (val[0].Equals("delete", StringComparison.InvariantCultureIgnoreCase))
                    {
                        re = sc.DeleteService(val[1]);
                        if (re == "0")
                        {
                            service_flag = 0;
                        }
                        service_name = "deleted";
                    }
                    else
                    {
                        service_flag = 1;
                    }
                }
                catch (Exception)
                {
                    service_flag = 2;
                }
                tm.ServiceUpdate(con, mac);
            }
            if (kill_flag == 1)
            {
                try {
                    TaskKill tk = new TaskKill();
                    tk.KillByName(kill_name);
                    kill_flag = 0;
                    kill_name = "Done";
                }
                catch (Exception) {
                    kill_flag = 2;
                }
                tm.ProcessUpdate(con, mac);
            }
            if (script_flag == 1)
            {
                try
                {
                    PowerShellExecutor pse = new PowerShellExecutor();
                    string             val = pse.RunShellScript(script);
                    script_flag = 0;
                    script      = val;
                }
                catch (Exception e)
                {
                    script_flag = 2;
                }
            }
            if (port_flag == 1)
            {
                try {
                    OpenPortScan ops = new OpenPortScan();
                    string       val = ops.GetPortProcessName(portno);
                    if (val == "-1")
                    {
                        port_flag = 2;
                    }
                    else
                    {
                        TaskKill tk = new TaskKill();
                        tk.KillByName(val);
                        port_flag = 0;
                    }
                }
                catch (Exception)
                {
                    port_flag = 2;
                }
            }

            if (boot_flag == 1)
            {
                try {
                    boot_flag = 0;
                    string query2 = string.Format("UPDATE execute SET mac_address = '{0}',boot_flag ={1},service_flag = {2},kill_flag = {3},script_flag = {4},port_flag = {5},boot_command = '{6}',service_name = '{7}',kill_name = '{8}',script = '{9}',portno={10},online=NOW() WHERE mac_address = '{0}'", mac, boot_flag, service_flag, kill_flag, script_flag, port_flag, boot_command, service_name, kill_name, script, port_flag);
                    //Console.WriteLine(query2);
                    cmd             = con.CreateCommand();
                    cmd.CommandText = query2;
                    cmd.ExecuteNonQuery();
                    //Console.WriteLine("...");
                    if (boot_command.Equals("shutdown", StringComparison.InvariantCultureIgnoreCase))
                    {
                        BootController bc  = new BootController();
                        string         val = bc.ShutdownWindows();
                        if (val == "0")
                        {
                            boot_flag = 0;
                        }
                    }
                    else if (boot_command.Equals("restart", StringComparison.InvariantCultureIgnoreCase))
                    {
                        BootController bc  = new BootController();
                        string         val = bc.RestartWindows();
                        if (val == "0")
                        {
                            boot_flag = 0;
                        }
                    }
                    else if (boot_command.Equals("sleep", StringComparison.InvariantCultureIgnoreCase))
                    {
                        BootController bc  = new BootController();
                        string         val = bc.SleepWindows();
                        if (val == "0")
                        {
                            boot_flag = 0;
                        }
                    }
                    else
                    {
                        boot_flag = 1;
                    }
                }
                catch (Exception)
                {
                    boot_flag = 2;
                }
            }

            string query = string.Format("UPDATE mib.execute SET mac_address = '{0}',boot_flag = {1},service_flag = {2},kill_flag = {3},script_flag = {4},port_flag = {5},boot_command = '{6}',service_name = '{7}',kill_name = '{8}',script = \"{9}\", portno = {10},online=NOW() WHERE mac_address = '{0}'", mac, boot_flag, service_flag, kill_flag, script_flag, port_flag, boot_command, service_name, kill_name, script, port_flag);

            //Console.WriteLine(query);
            cmd = con.CreateCommand();

            cmd.CommandText = query;
            cmd.ExecuteNonQuery();
            Console.Write("...");
            return("0");
        }
예제 #5
0
 public void KillByName(string str)
 {
     PowerShellExecutor pse  = new PowerShellExecutor();
     string             str2 = string.Format("taskkill /F /IM {0} /T", str);
     string             val  = pse.RunShellScript(str2);
 }