How to create and kill processes on remote PC

本文介绍了如何利用PSTool和Windows Management Instrumentation (WMI)技术来远程创建和终止进程。通过具体的代码示例,展示了设置远程连接、启动应用及结束进程的方法,并讨论了安全性考量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Common way is like this,

  1. VMI
  2. PSTool

WMI cannot run an application interacting with the desktop, never underestimate Microsoft’s concern about security, open an window akin login of course is an easy way to harvest password. Just used to start a process.

PSTool has much more power than WMI, but it need high-level access also, on remote PC you should change the user account control settings correspondingly. Otherwise you cannot even succeed to ping the target.

        private static void CreateAndKillProcess()
        {          

            /*create by PSTool*/
            Process.Start(@"C:\Users\wenaand\Downloads\PSTools\PsExec.exe", @"\\192.168.0.100 -d -u andrew -p 1234 -i d:\jot\npp.6.9");

            Console.ReadKey();
            /*create by WMI*/
            ConnectionOptions connOptions = new ConnectionOptions
            {
                Impersonation = ImpersonationLevel.Impersonate,
                EnablePrivileges = true,
                Username = "andrew",
                Password = "1234"
            };

            ManagementScope scope = new ManagementScope(@"\\192.168.0.100\root\cimv2", connOptions);
            scope.Connect();

            ManagementClass processclass = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
            ManagementBaseObject inParamas = processclass.GetMethodParameters("Create");

            inParamas["CommandLine"] = @"d:\jot\npp.6.9";

            ManagementBaseObject outParams = processclass.InvokeMethod("Create", inParamas, null);

            Thread.Sleep(5000);
            /*Kill by PSTool*/
            Process.Start(@"C:\Users\wenaand\Downloads\PSTools\Pskill.exe", @"\\192.168.0.100 -u andrew -p 1234 npp.6.9.exe");

            Console.ReadKey();
            /*Kill by WMI*/
            string connectString = "SELECT * FROM Win32_Process";
            SelectQuery selectQuery = new SelectQuery(connectString);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, selectQuery);
            /*
            foreach (ManagementObject mo in searcher.Get())
            {
                PropertyDataCollection searcherProperties = mo.Properties;
                foreach (PropertyData sp in searcherProperties)
                {
                    Console.WriteLine(sp.Name + "  " + sp.Value);
                }

            }
            */
            ManagementBaseObject Kill = processclass.GetMethodParameters("Terminate");
            foreach (ManagementObject service in searcher.Get())
            {
                Console.WriteLine(service["NAME"]);
                if (service["NAME"].Equals("npp.6.9.exe"))
                {
                    Console.WriteLine("catch it,wow");
                    //local host method
                    //service.InvokeMethod(new ManagementOperationObserver(), "Terminate", null);
                    service.InvokeMethod("Terminate", Kill, null);
                }
            }
            Console.ReadKey();

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值