C# 使用c擦除PE头#

C# 使用c擦除PE头#,c#,C#,我正在尝试使用以下功能删除我的PE头: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication9 { class Program { public enu

我正在尝试使用以下功能删除我的PE头:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication9
{
    class Program
    {
        public enum MemoryProtectionConsts : uint
        {
            EXECUTE = 0x10,
            EXECUTE_READ = 0x20,
            EXECUTE_READWRITE = 0x40,
            NOACCESS = 0x01,
            READONLY = 0x02,
            READWRITE = 0x04
        }

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string lpModuleName);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool VirtualProtect(IntPtr lpAddress, int dwSize, MemoryProtectionConsts flNewProtect,
            int lpflOldProtect);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint = "RtlSecureZeroMemory")]
        private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);
        private static int ErasePEHeader() // hModule = Handle to the module, procName = Process name (eg. "notepad")
        {
            int OldProtect = 0;
            IntPtr pBaseAddr = GetModuleHandle(null);
            VirtualProtect(pBaseAddr, 4096, // Assume x86 page size
             MemoryProtectionConsts.READWRITE, OldProtect);
            SecureZeroMemory(pBaseAddr, (IntPtr)4096);
            return 0;
        }
        static void Main(string[] args)
        {
            ErasePEHeader();
            Console.WriteLine("");
            Console.ReadKey();
        }
    }
}
但是,始终显示未处理的异常:


在我的示例中,启动我的异常,并且从不删除我的PE头。在本例中,我的目标是删除PE头,仅用于学习目的。

没有SecureZeroMem.dll这就是代码无法加载它的原因。如果您正在查找函数
SecureZeroMemory
,它位于kernel32.dll中

[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint "RtlSecureZeroMemory")]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);

前面的答案使用了
SecureZeroMemory()
,但由于它不是一个导出函数,我们不能简单地调用它

但是,您可以使用
Marashal.Copy()
将零填充数组复制到内存中

public static void Copy (byte[] source, int startIndex, IntPtr destination, int length);

请参见

您是否阅读了错误消息中的文字?无法加载DLL':'SecureZeroMem.DLL':找不到指定的模块。看起来很清楚。您没有要加载的“SecureZeroMem.dll”。您希望它来自哪里?在任何系统DLL中都没有这样的函数-请参阅