简体   繁体   English

C# 我如何以append代码结尾运行一个.NET.EXE,最好是从那个.EXE里面?

[英]C# How do I append code to the end of a running .NET .EXE, preferably from inside that .EXE?

Can a running .NET.EXE append data to itself?能不能跑.NET.EXE append数据给自己? What's stopping it?是什么阻止了它?

I could launch a separate process to do it just fine.我可以启动一个单独的过程来完成它。

But I can't figure out how to write to itself while it's running.但是我不知道如何在它运行时给自己写信。 Is there anyway to do this?有没有办法做到这一点? IN .NET在 .NET

EDIT: And preferably no hacky solutions like write it out somewhere else then copy/rename编辑:最好不要像把它写在其他地方然后复制/重命名这样的骇人听闻的解决方案

EDIT2: Clarifying type of executeable EDIT2:阐明可执行文件的类型

EDIT3: Purpose : Writing binary stream to my running EXE file allows me to then parse the.EXE file on disk for those bytes and use them in the program . EDIT3:目的:将二进制 stream 写入我正在运行的 EXE 文件,然后我可以解析磁盘上的 .EXE 文件以获取这些字节并在程序中使用它们 Without having to create any new files or registry entries or stuff like that.无需创建任何新文件或注册表项或类似的东西。 It is self contained.它是独立的。 This is extremely convenient.这非常方便。

EDIT4: For those against this file, please thinking about the functions of: FILE ZIPPING , DLL LINKING , and PORTABLE APPLICATIONS before trying to discredit this idea, EDIT4:对于那些反对这个文件的人,请在试图抹黑这个想法之前考虑以下功能: FILE ZIPPINGDLL LINKINGPORTABLE APPLICATIONS

There are a lot of bad consequences for storing data this way, as said in the comments, but there's a bigger problem: the answer to "What's stopping it?"正如评论中所说,以这种方式存储数据有很多不好的后果,但还有一个更大的问题:“是什么阻止了它?”的答案。 question.题。 The Windows PE loader locks the image file for writing while in execution, so you can't get an HANDLE to the file with write permissions, as NtCreateFile and NtOpenFile system calls with FILE_WRITE_DATA option will fail, as well as any attempt to delete the file. Windows PE 加载器在执行时锁定图像文件以进行写入,因此您无法获得具有写入权限的文件的HANDLE ,因为带有FILE_WRITE_DATA选项的NtCreateFileNtOpenFile系统调用将失败,以及任何删除文件的尝试. This block is implemented at kernel level and set during the NtCreateProcess system call, before the process and its modules entry point are actually called.该块在内核级实现并在NtCreateProcess系统调用期间设置,在进程及其模块入口点被实际调用之前。

The only dirty trick possible without writing data to the disk, sending it to a remote server and without kernel privileges is to use another process via an helper executable, code injection or command-line arguments scripts (eg with PowerShell) which can kill your process releasing the lock, append data to the end of file and restart it.在不将数据写入磁盘、将其发送到远程服务器且没有内核权限的情况下,唯一可能的肮脏技巧是通过辅助可执行文件、代码注入或命令行参数脚本(例如使用 PowerShell)使用另一个进程,这可以杀死您的进程释放锁,将数据附加到文件末尾并重新启动它。 Of course these options have even worse consequences, I wrote it only to make clear the OS limitations (made by purpose) and why no professional software uses this technique to store data.当然,这些选项有更糟糕的后果,我写它只是为了说明操作系统的限制(故意造成的)以及为什么没有专业软件使用这种技术来存储数据。

Despite all the negativity, there is a clean way to do this:尽管存在所有负面因素,但有一种干净的方法可以做到这一点:

The way I have found only requires the program be executed on an NTFS drive.我发现的方法只需要在 NTFS 驱动器上执行程序。

The trick is to have your app copy itself to an alternate stream as soon as it's launched, then execute that image and immediately close itself.诀窍是让您的应用程序在启动后立即将其自身复制到备用 stream,然后执行该映像并立即自行关闭。 This can be easily done with the commands:这可以使用以下命令轻松完成:

type myapp.exe > myapp.exe:image
forfiles /m myapp.exe /c myapp.exe:image

Once your application is running from an alternate stream (myapp.exe:image), it is free to modify the original file (myapp.exe) and read the data that's stored within it.一旦您的应用程序从备用 stream (myapp.exe:image) 运行,就可以自由修改原始文件 (myapp.exe) 并读取其中存储的数据。 The next time the program starts, the modified application will be copied to the alternate stream and executed.下次程序启动时,修改后的应用程序将被复制到备用stream并执行。

This allows you to get the effect of an executable writing to itself while running, without dealing with any extra files and allows you to store all settings within a single.exe file.这允许您在运行时获得可执行文件写入自身的效果,而无需处理任何额外的文件,并允许您将所有设置存储在一个 single.exe 文件中。

The file must be executed on an NTFS partition, but that is not a big deal since all Windows installations use this format.该文件必须在 NTFS 分区上执行,但这没什么大不了的,因为所有 Windows 安装都使用这种格式。 You can still copy the file to other filesystems, you just cannot execute it there.您仍然可以将文件复制到其他文件系统,只是不能在那里执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM