.NET - 通过 LAN 将可执行文件复制到另一台计算机并执行它

发布于 2024-09-19 16:06:16 字数 273 浏览 3 评论 0 原文

我正在使用 .NET,并疯狂地尝试找到任何有用的 API,让我可以通过 LAN 网络传输文件(当然是通过管理员凭据),然后在该计算机上执行它。

我读过一些使用 WMI 的内容,但谷歌搜索“.net WMI 复制文件”或“.net WMI 执行文件”对我根本没有帮助。

任何参考将不胜感激。

编辑

我无法使用 PsExec 等第三方工具(尽管它完全满足我的需要)。这是因为 PsExec 涉及许可证,我无法将其与我的应用程序一起分发。

I'm using .NET, and going crazy trying to find any helpful API that lets me transfer a file across a LAN network (trough admin credentials of course) and then execute it on that machine.

I've read some thing using WMI, but googling for ".net WMI copy files" or ".net WMI execute files" isn't helping me at all.

Any references would be greatly appreciated.

EDIT

I can't use a third party tool such as PsExec (although it does perfectly what I need). This is because of the license involved with PsExec I cannot distribute it with my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北城半夏 2024-09-26 16:06:16

我知道已经过去很多年了,但遇到了这个挑战并看到了这篇文章(以及其他文章),因此我将分享解决方案,以防它可以帮助任何人前进。它可用于通过 WMI 移动任何您想要的文件。

解决方案:

1:将 EXE 转换为 Base64

byte[] bytes = File.ReadAllBytes(pathToExe);
String file = Convert.ToBase64String(bytes);

2:通过 WMI 将 Base64 回显为文件并使用 certutil 进行解码

ConnectionOptions co = new ConnectionOptions();

// isLocal is a variable indicating whether machine name/IP is local    
if (!isLocal) {  
    co.Username = "domainOrMachine\accountName";
    co.Password = "password for account";
    co.EnablePrivileges = true; 
    co.Impersonation = ImpersonationLevel.Impersonate;
}

//ip is a vaiable holding the target endpoint
ManagementScope s = new ManagementScope(@"\\" + ip + @"\root\cimv2", co);
s.open();

ObjectGetOptions ogo = new ObjectGetOptions();
ManagementClass prog = new ManagementClass(s, new 
ManagementPath("Win32_Process"), ogo);
ManagementBaseObject mbo = prog.GetMethodParameters("Create");

mbo["CommandLine"] = @"cmd /c ""echo " + base64String + @" > c:\windows\temp\b64_exec.txt && certutil -decode c:\windows\temp\b64_exec.txt c:\windows\temp\b64_exec.exe && c:\windows\temp\b64_exec.exe""";

prog.InvokeMethod("Create", mbo, null);

现在,有一些问题需要突出显示

  • 传递的整个命令的长度不能大于超过 8191 个字符(命令的最大长度)
  • 您可以分解 Base64 文件并将其分成多个块发送 - 只需记住 > >管道到新文件或覆盖现有文件并且>>附加或添加到现有文件。
  • 您还需要发出 del 命令来清理 base64 文件 - 根据可用空间,您可能需要进行第二次 WMI 调用来调用它。

就我而言,我不想一次发送 7000 个字符的完整 2MB 文件,因此我创建了一个简单的 .net 下载器,该下载器编译为小于 6KB,并将其全部放入单个语句中(如上所述)。下载我的可执行文件,完成后(当我看到我想要的文件时,通过 WMI 查询)我只需通过 WMI 删除它。

I know it's been years, but ran into this challenge and came accross this post (among others) so going to share the solution in case it helps anyone moving forward. It can be used to move any file you want over WMI.

Solution:

1: Convert EXE to Base64

byte[] bytes = File.ReadAllBytes(pathToExe);
String file = Convert.ToBase64String(bytes);

2: Echo Base64 to a file over WMI and decode with certutil

ConnectionOptions co = new ConnectionOptions();

// isLocal is a variable indicating whether machine name/IP is local    
if (!isLocal) {  
    co.Username = "domainOrMachine\accountName";
    co.Password = "password for account";
    co.EnablePrivileges = true; 
    co.Impersonation = ImpersonationLevel.Impersonate;
}

//ip is a vaiable holding the target endpoint
ManagementScope s = new ManagementScope(@"\\" + ip + @"\root\cimv2", co);
s.open();

ObjectGetOptions ogo = new ObjectGetOptions();
ManagementClass prog = new ManagementClass(s, new 
ManagementPath("Win32_Process"), ogo);
ManagementBaseObject mbo = prog.GetMethodParameters("Create");

mbo["CommandLine"] = @"cmd /c ""echo " + base64String + @" > c:\windows\temp\b64_exec.txt && certutil -decode c:\windows\temp\b64_exec.txt c:\windows\temp\b64_exec.exe && c:\windows\temp\b64_exec.exe""";

prog.InvokeMethod("Create", mbo, null);

Now, there are some gotcha's that need to be highlighted

  • The entire command passed can't have a length greater than 8191 characters, the maximum length of a command
  • You can break up the base64 file and send it in multiple chunks - just remember that > pipes to a new file or overwrites an existing file and >> appends or adds to an existing file.
  • You will want to also issue a del command to clean up the base64 file - depending on the room available, you might have to make a second WMI call to invoke that.

In my case, I didn't want to send a full 2MB file 7000 characters at a time, so I created a simple .net downloader that compiled to less than 6KB and got it all into a single statement (as per above.) It downloads my executable and when complete (when I see the file I want, queried over WMI) I just delete it over WMI.

九八野马 2024-09-26 16:06:16

我认为这并不容易实现。不过,您可以使用 .net 复制 exe。然后(同样来自 .net,使用 Process.Start)调用 psExec 并使其远程执行程序。

I don't think that this is easily achieved. You can however copy the exe with .net. And then (also from .net, with Process.Start) invoke psExec and make it execute the program remotely.

獨角戲 2024-09-26 16:06:16

在没有 PowerShell 2.0 远程处理已启用,我发现 PsExec 命令行工具非常有用。它需要远程计算机上的管理权限。

On machines that don't have PowerShell 2.0 remoting enabled, I find the PsExec commandline tool very useful. It requires administration permissions on the remote machine.

一场信仰旅途 2024-09-26 16:06:16

You can consider to use Scheduler service (AT command) to start an application (see http://msdn.microsoft.com/en-us/library/aa384006.aspx) after the application code are copied to the remote computer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文