首页
社区
课程
招聘
[原创]C# .net内存特征码搜索和内存修改
发表于: 2025-1-19 14:30 5619

[原创]C# .net内存特征码搜索和内存修改

2025-1-19 14:30
5619

.net的内存特征码搜索

功能:
1、内存特征码搜索(支持跨进程,堆栈搜索,半码F?、全码??,编写自己的搜索的工具)
2、程序集dll、C/C++模块的获取基址和映像大小
3、内存修改(可以带??,如:5F ?? 6E 7D ?? 8A)

部分实现代码:
获取程序集模块的基址

public static ulong Get_Assembly_Module_BaseAddress(string assemblyName)
{
    try
    {
        if (string.IsNullOrEmpty(assemblyName)) return 0;
        return (ulong)Marshal.GetHINSTANCE(
            AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(m => m.GetModules()
            .Where(n => n.Name.Contains(assemblyName)))
            .FirstOrDefault());
    }
    catch { return 0; }
}

获取模块的映像大小,.net和C/C++的通用

public static ulong Get_Moule_SizeOfImage(ulong baseAddress)
{
    try
    {
        IMAGE_DOS_HEADER dosHeader = (IMAGE_DOS_HEADER)Marshal.PtrToStructure((IntPtr)baseAddress, typeof(IMAGE_DOS_HEADER));
        IMAGE_NT_HEADERS ntHeader = (IMAGE_NT_HEADERS)Marshal.PtrToStructure((IntPtr)(baseAddress + (ulong)dosHeader.e_lfanew),
                typeof(IMAGE_NT_HEADERS));
        return (ulong)ntHeader.OptionalHeader.SizeOfImage;
    }
    catch { return 0; }
}

获取C/C++模块的基址

public static ulong Get_C_Module_BaseAddress(string cModuleName)
{
    try
    {
        if (string.IsNullOrEmpty(cModuleName)) return 0;
        return (ulong)Process.GetCurrentProcess()
            .Modules.Cast<ProcessModule>()
            .Where(m => m.ModuleName.Contains(cModuleName))
            .ToArray().FirstOrDefault().BaseAddress;
    }
    catch { return 0; }
}

获取C/C++模块的映像大小

public static ulong Get_C_Module_SizeOfImage(string cModuleName)
{
    try
    {
        if (string.IsNullOrEmpty(cModuleName)) return 0;
        return (ulong)Process.GetCurrentProcess()
            .Modules.Cast<ProcessModule>()
            .Where(m => m.ModuleName.Contains(cModuleName))
            .ToArray().FirstOrDefault().ModuleMemorySize;
    }
    catch { return 0; }
}

修改内存数据

public static bool WriteMemoryData(ulong baseAddress, string data)
{
    try
    {
        if (string.IsNullOrEmpty(data)) return false;
        data = data.Replace(" ", "");
        if ((data.Length & 1) != 0) return false;    // 不能为单数
        uint len = (uint)data.Length / 2;            // 计算特征码长度
        uint oldProtect;
        if (VirtualProtect((IntPtr)baseAddress, len, PAGE_EXECUTE_READWRITE, out oldProtect))
        {
            for (uint i = 0; i < len; i++)
            {
                string tempStr = data.Substring((int)i * 2, 2);
                if (tempStr != "??")
                    Marshal.WriteByte((IntPtr)(baseAddress + i), Convert.ToByte(tempStr, 16));
            }
            VirtualProtect((IntPtr)baseAddress, len, oldProtect, out oldProtect);
            return true;
        }
    }
    catch { return false; }
    return false;
}

调用:

private static void Main(string[] args)
{
    // System.dll是.NET Framework的核心程序集,ntdll.dll是Windows系统的核心模块
    var systemAssembly = PatchPattern.Get_Assembly_Module_BaseAddress("System.dll");
    Console.WriteLine("System基址:0x" + systemAssembly.ToString("X"));
    Console.WriteLine("System大小:0x" + PatchPattern.Get_Moule_SizeOfImage(systemAssembly).ToString("X"));
    Console.WriteLine("ntdll基址:0x" + PatchPattern.Get_C_Module_BaseAddress("ntdll.dll").ToString("X"));
    Console.WriteLine("ntdll大小:0x" + PatchPattern.Get_C_Module_SizeOfImage("ntdll.dll").ToString("X"));

    // Hello World! 的特征码为 48 00 65 00 6C 00 6C 00 6F 00 20 00 57 00 6F 00 72 00 6C 00 64 00 21 00
    string testStr = "Hello World!";
    string patternStr = "48 00 65 00 6C 00 6C ?? 6F 00 20 00 ?7 00 6F ?? 72 00 6C 00 64 00 21 00";

    IntPtr hProcess = Process.GetCurrentProcess().Handle;
    // 获取主模块基址和大小
    ulong baseAddress = (ulong)Process.GetCurrentProcess().MainModule.BaseAddress;
    ulong size = (ulong)Process.GetCurrentProcess().MainModule.ModuleMemorySize;
    Console.WriteLine("模块基址:0x" + baseAddress.ToString("X") + "----模块大小:0x" + size.ToString("X"));

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    // 遍历内存,搜索特征码
    // 注意:搜索前确保程序集或者dll已加载
    List<ulong> result = PatchPattern.SundayPatternFind(hProcess, baseAddress, baseAddress + size, patternStr, 0);
    stopwatch.Stop();
    Console.WriteLine("搜索用时: " + stopwatch.ElapsedMilliseconds + " 毫秒");
    Console.WriteLine("搜索到特征码:" + result.Count + "个");
    result.ForEach(x => Console.WriteLine("特征码地址:0x" + x.ToString("X")));

    // 你好,世界!的unicode编码为 60 4F 7D 59 0C FF 16 4E 4C 75 01 FF
    Encoding.Unicode.GetBytes("你好,世界!").ToList().ForEach(x => Console.Write(x.ToString("X2") + " "));
    Console.WriteLine();
    // 修改搜索到的内存数据
    if (result.Count > 0)
        // 将特征码替换为你好,世界!的unicode编码,并添加截断0000字节
        if (PatchPattern.WriteMemoryData(result[0], "60 4F 7D 59 0C FF 16 4E 4C 75 01 FF" + "0000"))
        {
            Console.WriteLine("修改内存数据成功");
            Console.WriteLine("修改为:" + Marshal.PtrToStringAuto((IntPtr)result[0]));
        }
        else Console.WriteLine("修改内存数据失败");

    Console.ReadKey();
}



[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2025-1-28 20:02 被wtujoxk编辑 ,原因:
收藏
免费 43
支持
分享
最新回复 (47)
雪    币: 138
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
2
x学习学习
2025-1-21 11:26
0
雪    币: 325
活跃值: (1654)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
学习学习
2025-1-21 12:00
0
雪    币: 3360
活跃值: (4764)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4

谢谢你的细致分析,受益匪浅!
2025-1-21 12:28
0
雪    币: 9301
活跃值: (5932)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
5
学习学习 
2025-1-21 13:33
0
雪    币: 155
活跃值: (3156)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
学习学习!!!
2025-1-21 15:11
0
雪    币: 3278
活跃值: (8409)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
全网普发
2025-1-21 15:22
0
雪    币: 0
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
8
学习一下
2025-1-21 16:28
0
雪    币: 290
活跃值: (885)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
学习一下
2025-1-21 16:34
0
雪    币: 149
活跃值: (376)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
10
看看学习
2025-1-21 17:04
0
雪    币: 89
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
11
学习
2025-1-21 18:29
0
雪    币: 49
活跃值: (746)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
学习学习 
2025-1-21 19:25
0
雪    币: 9682
活跃值: (6101)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
jgs
13
感谢楼主分享,收藏备用
2025-1-21 19:42
0
雪    币: 8505
活跃值: (4571)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
学习学习 
2025-1-21 19:53
0
雪    币: 104
活跃值: (5493)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
6666
2025-1-21 22:24
0
雪    币: 3680
活跃值: (3623)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
学习学习
2025-1-21 23:37
0
雪    币: 11165
活跃值: (5381)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
17
感谢楼主分享,收藏备用
2025-1-22 06:10
0
雪    币: 3504
活跃值: (2945)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
學習學習
2025-1-22 08:20
0
雪    币: 221
活跃值: (2661)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
19
学习学习
2025-1-22 08:26
0
雪    币: 20
活跃值: (84)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
20
学习学习
2025-1-22 08:39
0
雪    币: 533
活跃值: (2958)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
21
学习学习
2025-1-22 09:25
0
雪    币: 156
活跃值: (474)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
22
2025-1-22 11:11
0
雪    币:
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
23
学习学习
2025-1-22 11:59
0
雪    币: 9530
活跃值: (4571)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
24
学习一下,谢谢分享
2025-1-22 13:05
0
雪    币: 0
活跃值: (2765)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
25
1
2025-1-22 14:13
0
游客
登录 | 注册 方可回帖
返回