驱动开发:内核特征码搜索函数封装

在前面的系列教程如 《驱动开发:内核枚举DpcTimer定时器》或者 《驱动开发:内核枚举IoTimer定时器》里面 LyShark大量使用了 特征码定位这一方法来寻找符合条件的 汇编指令集,总体来说这种方式只能定位特征较小的指令如果特征值扩展到5位以上那么就需要写很多无用的代码,本章内容中将重点分析,并实现一个 通用特征定位函数。

如下是一段特征码搜索片段,可以看到其实仅仅只是将上章中的搜索方式变成了一个 SearchSpecialCode函数,如下函数,用户传入一个 扫描起始地址以及搜索特征码的字节数组,即可完成搜索工作,具体的参数定义如下。

  • pSearchBeginAddr 扫描的内存(内核)起始地址
  • ulSearchLength 需要扫描的长度
  • pSpecialCode 扫描特征码,传入一个UCHAR类型的字节数组
  • ulSpecialCodeLength 特征码长度,传入字节数组长度
// By: LyShark.com
PVOID SearchSpecialCode(PVOID pSearchBeginAddr, ULONG ulSearchLength, PUCHAR pSpecialCode, ULONG ulSpecialCodeLength)
{
  PVOID pDestAddr = NULL;
  PUCHAR pBeginAddr = (PUCHAR)pSearchBeginAddr;
  PUCHAR pEndAddr = pBeginAddr + ulSearchLength;
  PUCHAR i = NULL;
  ULONG j = 0;

  for (i = pBeginAddr; i = ulSpecialCodeLength)
    {
      pDestAddr = (PVOID)i;
      break;
    }
  }
  return pDestAddr;
}

那么这个简单的特征码扫描函数该如何使用,这里我们就用 《驱动开发:内核枚举IoTimer定时器》中枚举 IopTimerQueueHead链表头部地址为案例进行讲解,如果你忘记了如何寻找链表头部可以去前面的文章中学习,这里只给出实现流程。

驱动开发:内核特征码搜索函数封装

我们首先通过 MmGetSystemRoutineAddress得到 IoInitializeTimer首地址,然后在偏移长度为 0x7e范围内搜索特征码 48 8d 0d特征,其代码可以总结为如下样子。

NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
    DbgPrint(("hello lyshark.com \n"));

    // 得到基址
    PUCHAR IoInitializeTimer = GetIoInitializeTimerAddress();
    DbgPrint("IoInitializeTimer Address = %p \n", IoInitializeTimer);

    // ---------------------------------------------------
    // LyShark 开始定位特征

    // 设置起始位置
    PUCHAR StartSearchAddress = (PUCHAR)IoInitializeTimer;

    // 设置结束位置
    PUCHAR EndSearchAddress = StartSearchAddress + 0x7e;
    DbgPrint("[LyShark 搜索区间] 起始地址: 0x%X --> 结束地址: 0x%X \n", StartSearchAddress, EndSearchAddress);

    // 设置搜索长度
    LONGLONG size = EndSearchAddress - StartSearchAddress;
    DbgPrint("[LyShark 搜索长度] 长度: %d \n", size);

    PVOID ptr;

    // 指定特征码
    UCHAR pSpecialCode[256] = { 0 };

    // 指定特征码长度
    ULONG ulSpecialCodeLength = 3;

    pSpecialCode[0] = 0x48;
    pSpecialCode[1] = 0x8d;
    pSpecialCode[2] = 0x0d;

    // 开始搜索,找到后返回首地址
    ptr = SearchSpecialCode(StartSearchAddress, size, pSpecialCode, ulSpecialCodeLength);

    DbgPrint("搜索特征码首地址: 0x%p \n", ptr);

    Driver->DriverUnload = UnDriver;
    return STATUS_SUCCESS;
}

代码运行后,您会发现它可以直接定位到我们需要的位置,如下图所示:

[En]

After the code runs, you will find that it can be located directly to the location we need, as shown in the following figure:

驱动开发:内核特征码搜索函数封装

如上图所示,签名位置函数返回内存地址,我们需要获取地址中的数据,因此需要提取以下内容。

[En]

As you can see in the figure above, the signature location function returns the memory address, and we need to get the data in the address, so we need to extract the following.

例如当指令是:

fffff80206185c00 488d0dd9ddcdff  lea rcx,[nt!IopTimerQueueHead (fffff80205e639e0)]

那么就需要 RtlCopyMemory跳过前三个字节,并在第四个字节开始取数据,并将读入的数据放入到 IopTimerQueueHead_LyShark_Code变量内。

// 署名
// PowerBy: LyShark
// Email: me@lyshark.com

    // 开始搜索,找到后返回首地址
    ptr = SearchSpecialCode(StartSearchAddress, size, pSpecialCode, ulSpecialCodeLength);

    DbgPrint("搜索特征码首地址: 0x%p \n", ptr);

    // 提取特征
    // fffff80206185c00 488d0dd9ddcdff  lea     rcx,[nt!IopTimerQueueHead (fffff80205e639e0)]
    ULONG64 iOffset = 0;
    ULONG64 IopTimerQueueHead_LyShark_Code = 0;

    __try
    {
        // 拷贝内存跳过lea,向后四字节
        RtlCopyMemory(&iOffset, (ULONG64)ptr + 3, 4);

        // 取出后面的IopTimerQueueHead内存地址 LyShark.com
        IopTimerQueueHead_LyShark_Code = iOffset + (ULONG64)ptr + 7;

        DbgPrint("提取数据: 0x%p \n", IopTimerQueueHead_LyShark_Code);
    }
    __except (1)
    {
        DbgPrint("[LySHark] 拷贝内存异常 \n");
    }

这样,我们就可以获得我们需要的地址,如以下结果所示:

[En]

In this way, we can get the address we need, as shown in the following result:

驱动开发:内核特征码搜索函数封装

Original: https://www.cnblogs.com/LyShark/p/16798318.html
Author: lyshark
Title: 驱动开发:内核特征码搜索函数封装



相关阅读

Title: conda 环境迁移, 修改conda路径(复制文件夹 + 软连接)

文章目录

前言

root空间不够,需要把conda移到其他路径。

操作步骤

假设你原来的conda安装在 /root/anaconda3路径下,要把它挪到 /home/me/anaconda3.

1. 移动文件夹

先把文件夹挪过去:

mv /root/anaconda3 /home/me/anaconda3

3. 创建软连接

然后创建一个软连接,相当于一个快捷方式:

[En]

Then create a soft connection, which is equivalent to a shortcut:

ln -s /home/me/anaconda3 /root/anaconda3

第一个参数真正实现了一些功能,第二个参数是一条捷径。

[En]

The first parameter really puts something, and the second is a shortcut.

这样相当于每次系统找/root/anaconda3的时候就会去/home/me/anaconda3找,但/root/anaconda3路径本身不占存储空间。

驱动开发:内核特征码搜索函数封装

3. 测试是否成功

如果你的订单没有出错,基本上没问题。

[En]

If you don’t make a mistake with an order, it’s basically fine.

conda -V
pip list
source activate my_env
pip
conda deactivate

补充资料:conda路径在哪些地方设置

https://zhuanlan.zhihu.com/p/265660902
以下文件都包含了conda路径:

  • ~/.bashrc # 改完需要执行 source ~/.bashrc 使其生效
  • /…/archiconda3/etc/profile.d/conda.sh
  • /…/archiconda3/bin/conda
  • /…/archiconda3/bin/activate
  • /…/archiconda3/bin/deactivate
  • /…/archiconda3/bin/pip
  • 以上是常用的,其实conda3/bin底下所有文件都有配置。
  • 如果您之前安装了虚拟环境,还需要修改
    [En]

    if you have previously installed a virtual environment, you also need to modify*

  • /…/archiconda3/envs/env_name/bin/pip
    综上所述,还有很多变化逐一出现。当然,您也可以编写脚本。我们的软连接应该是最简单、最方便的方式。
    [En]

    To sum up, there are still a lot of changes one by one. Of course, you can write a script. Our soft connection should be the easiest and most convenient way.

其他资料

https://www.cnblogs.com/baiyiqingxiang/p/15701992.html
整个文件夹迁移

单个环境迁移:
https://blog.csdn.net/qq_41967982/article/details/115867230

.bashrc 文件:
https://blog.csdn.net/qq_30214939/article/details/72638202

Original: https://blog.csdn.net/qq_34342853/article/details/123020957
Author: YuQiao0303
Title: conda 环境迁移, 修改conda路径(复制文件夹 + 软连接)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/363337/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总