吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3008|回复: 9
收起左侧

[系统底层] DTrace二次探索

[复制链接]
BeneficialWeb 发表于 2023-3-1 10:28

DTrace 二次研究

早些时候遇到一个navagio.sys驱动,无法通过IDA直接进行静态分析。于是想到能否通过Trace手段追踪一下。

初步想法

#pragma D option quiet
#pragma D option destructive

syscall::Nt*:entry
{
    if(pid == 4)
        printf("%s [Caller %s] 0x%p, 0x%x\n",probefunc, execname, curthread, tid);
}

通过编写D语言脚本,过滤出系统进程的Nt函数调用,效果如下:

C:\Program Files\DTrace>dtrace.exe -s C:\Users\VirtualCC\Desktop\ktrace.d
NtOpenKey [Caller System] 0xffffb20d54ce1080, 448
NtQueryValueKey [Caller System] 0xffffb20d54ce1080, 448
NtQueryValueKey [Caller System] 0xffffb20d54ce1080, 448
NtQueryValueKey [Caller System] 0xffffb20d54ce1080, 448
...... 此处省略
NtClose [Caller System] 0xffffb20d54ce1080, 448
NtClose [Caller System] 0xffffb20d54ce1080, 448
NtCreateSymbolicLinkObject [Caller System] 0xffffb20d54ce1080, 448
NtClose [Caller System] 0xffffb20d54ce1080, 448
NtClose [Caller System] 0xffffb20d54ce1080, 448
NtQuerySystemInformation [Caller System] 0xffffb20d54ce1080, 448

一些BCD的配置

bcdedit /set dtrace on

安装DTrace可参考微软官网。

设置环境变量

set PATH=%PATH%;"C:\Program Files\DTrace"
mkdir c:\symbols
set _NT_SYMBOL_PATH=srv*C:\symbols*https://msdl.microsoft.com/download/symbols

最终的实现脚本

找到线程,解析调用参数。

#pragma D option quiet
#pragma D option destructive

struct ustr{uint16_t buffer[256];};

inline uintptr_t MmHighestUserAddress = 0x7FFFFFFEFFFF;

int found;
PETHREAD ethread_ptr;

BEGIN
{
    found = 0;
    ethread_ptr=0;
}

syscall::Nt*:entry
{
    if (pid == 4) {
        if (probefunc == "NtOpenKey")
        {
            if(!found) 
            {
                ethread_ptr = curthread;
                found = 1;
            }
        }

        if (ethread_ptr == curthread) {
            if(probefunc != "NtQueryDirectoryFile")
                printf("%s [Caller %s] 0x%p, 0x%x\n", probefunc, execname, curthread, tid);

            if (probefunc == "NtOpenKey")
            {
                attr = (POBJECT_ATTRIBUTES)arg2;
                if (attr->ObjectName)
                {
                    temp = ((PUNICODE_STRING)attr->ObjectName)->Buffer;
                    len = ((PUNICODE_STRING)(attr->ObjectName))->Length / 2;
                    printf("%Y: 0x%p Open RegKeyName:%*.*ws\n", walltimestamp, curthread, len, len,
                        ((struct ustr*)temp)->buffer);
                }
            }

            if (probefunc == "NtCreateFile")
            {
                attr = (POBJECT_ATTRIBUTES)arg2;
                if (attr->ObjectName)
                {
                    temp = ((PUNICODE_STRING)attr->ObjectName)->Buffer;
                    len = ((PUNICODE_STRING)(attr->ObjectName))->Length / 2;
                    printf("%Y: 0x%p Create FileName: %*.*ws\n", walltimestamp, curthread, len, len,
                        ((struct ustr*)temp)->buffer);
                }
            }

            if (probefunc == "NtQueryValueKey")
            {
                temp = ((PUNICODE_STRING)arg1)->Buffer;
                len = ((PUNICODE_STRING)arg1)->Length / 2;
                printf("%Y: 0x%p value name: %*.*ws\n", walltimestamp, curthread, len, len,
                    ((struct ustr*)temp)->buffer);
            }

            if (probefunc == "NtOpenFile")
            {
                attr = (POBJECT_ATTRIBUTES)arg2;
                if (attr->ObjectName)
                {
                    /*temp = ((PUNICODE_STRING)attr->ObjectName)->Buffer;
                    len =((PUNICODE_STRING)(attr->ObjectName))->Length / 2;
                    printf("%Y: 0x%p Open File Name: %*.*ws\n",walltimestamp,curthread,len,len,
                        ((struct ustr*)temp)->buffer);*/
                }
            }

            if (probefunc == "NtCreateSection") {
                attr = (POBJECT_ATTRIBUTES)arg2;
                if (attr->ObjectName)
                {
                    temp = ((PUNICODE_STRING)attr->ObjectName)->Buffer;
                    len = ((PUNICODE_STRING)(attr->ObjectName))->Length / 2;
                    printf("%Y: 0x%p Create Section Name: %*.*ws\n", walltimestamp, curthread, len, len,
                        ((struct ustr*)temp)->buffer);
                }
            }

            if (probefunc == "NtCreateThreadEx") {
                this->addr = (uintptr_t)arg4;
                printf("%Y: 0x%p start addr: %p\n", walltimestamp, curthread, this->addr);
            }

            if (probefunc == "NtCreateEvent") {
                attr = (POBJECT_ATTRIBUTES)arg2;
                if (attr->ObjectName)
                {
                    temp = ((PUNICODE_STRING)attr->ObjectName)->Buffer;
                    len = ((PUNICODE_STRING)(attr->ObjectName))->Length / 2;
                    printf("%Y: 0x%p Create Event Name: %*.*ws\n", walltimestamp, curthread, len, len,
                        ((struct ustr*)temp)->buffer);
                }
            }

            if (probefunc == "NtCreateSymbolicLinkObject") {
                attr = (POBJECT_ATTRIBUTES)arg2;
                if (attr->ObjectName)
                {
                    temp = ((PUNICODE_STRING)attr->ObjectName)->Buffer;
                    len = ((PUNICODE_STRING)(attr->ObjectName))->Length / 2;
                    printf("%Y: 0x%p Create SymbolicLinkObject Name: %*.*ws\n", walltimestamp, curthread, len, len,
                        ((struct ustr*)temp)->buffer);
                }
            }

            if (probefunc == "NtQuerySystemInformation") {
                printf("%Y: 0x%p system info class: 0x%x\n", walltimestamp, curthread, arg0);
            }
        }
    }

}

下面是navagio.sys的trace结果:

```json
C:\Windows\system32>dtrace.exe -s C:\Users\Virtual-PC\Desktop\ktrace.d
NtOpenKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 Open RegKeyName:\Registry\Machine\System\CurrentControlSet\Services\navagio.sys
NtQueryValueKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 value name: ImagePath
NtQueryValueKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 value name: ObjectName
NtQueryValueKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 value name: Type
NtQueryKey [Caller System] 0xffffd20f01799040, 0x1618
NtOpenFile [Caller System] 0xffffd20f01799040, 0x1618
NtCreateSection [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtQuerySystemInformation [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 system info class: 0x95
NtOpenKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 Open RegKeyName:\Registry\Machine\System\CurrentControlSet\Control\Compatibility\Driver\navagio.sys
NtOpenFile [Caller System] 0xffffd20f01799040, 0x1618
NtOpenThreadTokenEx [Caller System] 0xffffd20f01799040, 0x1618
NtOpenProcessTokenEx [Caller System] 0xffffd20f01799040, 0x1618
NtQueryInformationToken [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtCreateSection [Caller System] 0xffffd20f01799040, 0x1618
NtOpenFile [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtOpenThreadTokenEx [Caller System] 0xffffd20f01799040, 0x1618
NtOpenProcessTokenEx [Caller System] 0xffffd20f01799040, 0x1618
NtQueryInformationToken [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtQueryValueKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 value name: ImagePath
NtOpenFile [Caller System] 0xffffd20f01799040, 0x1618
NtOpenThreadTokenEx [Caller System] 0xffffd20f01799040, 0x1618
NtOpenProcessTokenEx [Caller System] 0xffffd20f01799040, 0x1618
NtQueryInformationToken [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtCreateSection [Caller System] 0xffffd20f01799040, 0x1618
NtMapViewOfSection [Caller System] 0xffffd20f01799040, 0x1618
NtCreateFile [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 Create FileName: \??\C:\Users\Virtual-PC\Desktop\navagio.sys
NtQueryInformationFile [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtOpenFile [Caller System] 0xffffd20f01799040, 0x1618
NtUnmapViewOfSection [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtQueryValueKey [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:50:58: 0xffffd20f01799040 value name: PnpFlags
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtCreateSymbolicLinkObject [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:51:03: 0xffffd20f01799040 Create SymbolicLinkObject Name: \DosDevices\NavagioDevice
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtQuerySystemInformation [Caller System] 0xffffd20f01799040, 0x1618
2023 Mar  1 09:51:03: 0xffffd20f01799040 system info class: 0x67
NtUpdateWnfStateData [Caller System] 0xffffd20f01799040, 0x1618
NtUpdateWnfStateData [Caller System] 0xffffd20f01799040, 0x1618
NtUpdateWnfStateData [Caller System] 0xffffd20f01799040, 0x1618
NtUpdateWnfStateData [Caller System] 0xffffd20f01799040, 0x1618
NtOpenFile [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618
NtQueryVolumeInformationFile [Caller System] 0xffffd20f01799040, 0x1618
NtClose [Caller System] 0xffffd20f01799040, 0x1618

进一步的想法是看到了创建的符号链接\DosDevices\NavagioDevice,如果说找到通信进程,然后进行对相应的进程的线程进行Trace,或许会有其他发现。以后有机会再看!

免费评分

参与人数 3吾爱币 +9 热心值 +3 收起 理由
1718022432 + 1 + 1 我很赞同!很好
debug_cat + 1 + 1 用心讨论,共获提升!
willJ + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hahhahah 发表于 2023-3-1 10:36
学习大佬分析Dtrace的思路
时光纪Bound 发表于 2023-3-1 12:43
謌丶无心留恋 发表于 2023-3-2 01:58
zjh889 发表于 2023-3-2 08:42
很好的技术贴,俺见识了!
debug_cat 发表于 2023-3-2 09:42
感谢大佬分享!
Luker 发表于 2023-3-2 16:45
持续前进
xiong779 发表于 2023-4-13 17:04
学习大佬分析Dtrace的思路
zjy131458 发表于 2023-5-29 09:02
感谢大佬分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-11 00:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表