首页
社区
课程
招聘
[原创]劫持MSVC编译器编译过程(VS2019 VS2022) 利用思路
发表于: 2025-10-14 05:22 5443

[原创]劫持MSVC编译器编译过程(VS2019 VS2022) 利用思路

2025-10-14 05:22
5443

各位大侠,大家好。我是Fake77,该文主要是抛砖引玉(我是砖)。
今天为大家带来一个基于Dll劫持的MSVC编译过程劫持思路,我本来是想要研究下MSVC编译器想通过注入的方式劫持编译流,然后通过得出逆向出微软编译器的IR格式,最后通过自己的Pass实现原生的Windows下驱动混淆方案(又或者 编译期代码植入)。
我目前用的方案是自己写的LLVM替换编译工具链的方式来对驱动进行混淆的,但是该方案每次我改动LLVM的时候就需要重新编译LLVM,非常的麻烦。所以我就想着能不能通过注入的方式去做,结果在研究的过程中发现可以通过劫持的方式去做(微软偷懒没有对PASS 相关的DLL进行鉴权导致我们可以利用这个机制进行植入)换句话说这个也是一个代码植入的APT思路,虽然我们已经可以用譬如

等等方式使其编译带毒感染,但是总归都不如通过直接劫持编译器来的快。

更早的没有测过了,至少新版本是可以的

图片描述

通过替换Dll即可实现。路径如下

如下所示,有两个地方都可以加载Dll并且没有做任何的校验

首先,我们根据cl.exe的名称可以看到他载入后调用了 _InvokeCompilerPassW@16我们可以在IDA里面搜索这个签名。
图片描述
找到该函数后进入 Trap_callMain 进入编译流程
图片描述
继续进入 CallMain
图片描述
继续进入 Trap_main_complie 这里稍微还原以下如果大家是F5看的话应该最后是

图片描述
他里面还包了一层 main_compile继续
图片描述
这个非常大的函数就是编译的逻辑了,这里我给出一些大概的东西大家具体可以自己分析。
FileMap句柄(可以通过这个去写文件)
这里可以看到句柄位置
图片描述
可以通过如下类方法去研究
图片描述
然后IR的一些格式和操作需要结合 cl.exe的方法才能分析出来,这部分内容我就不写了目前暂不公开,通过如上的句柄已经可以进行劫持了,修改c1.dll通过特征或者PDB获取到句柄后在编译的时候识别标准的__crt_main函数的特征码或者__init_term等等的点都可以注入一些意想不到的东西。我给出c1的主要CFG

该方法挺通用的很多版本都可以用,具体更多用法等大家自己去开发,关于IR的一些研究我就不发了。

.text:00426C20 ; int __fastcall execute(int, int, const WCHAR *lpLibFileName, wchar_t *Name, _DWORD *)
.text:00426C20 ?execute@@YAHW4driver_phases@@PAUpassinfo_t@@PBG2PAPAG@Z proc near in this methods this line
.text:00426E16 call ds:__imp__LoadLibraryExW@12 ; LoadLibraryExW(x,x,x)
they call the loadlibraryExW without check and after that
.text:00426E1C mov ebx, eax
.text:00426E1E test ebx, ebx
.text:00426E20 jz short loc_426E5A
.text:00426E22 push offset aInvokecompiler_0 ; "_InvokeCompilerPassW@16"
.text:00426E27 push ebx ; hModule
.text:00426E28 mov esi, ds:__imp__GetProcAddress@8 ; GetProcAddress(x,x)
.text:00426E2E call esi ; GetProcAddress(x,x) ; GetProcAddress(x,x)
.text:00426E30 mov [ebp+var_28], eax
.text:00426E33 push offset aAbortcompilerp_0 ; "_AbortCompilerPass@4"
.text:00426E38 push ebx ; hModule
.text:00426E39 call esi ; GetProcAddress(x,x) ; GetProcAddress(x,x).text:00426E1C mov ebx, eax (pollute _AbortCompilerPass)
.text:00426E1E test ebx, ebx
.text:00426E20 jz short loc_426E5A
.text:00426E22 push offset aInvokecompiler_0 ; "_InvokeCompilerPassW@16"
.text:00426E27 push ebx ; hModule
.text:00426E28 mov esi, ds:__imp__GetProcAddress@8 ; GetProcAddress(x,x)
.text:00426E2E call esi ; GetProcAddress(x,x) ; GetProcAddress(x,x)
pollute _InvokeCompilerPassW
.text:00426E30 mov [ebp+var_28], eax
.text:00426E33 push offset aAbortcompilerp_0 ; "_AbortCompilerPass@4"
.text:00426E38 push ebx ; hModule
.text:00426E39 call esi ; GetProcAddress(x,x) ; GetProcAddress(x,x)
.text:00426C20 ; int __fastcall execute(int, int, const WCHAR *lpLibFileName, wchar_t *Name, _DWORD *)
.text:00426C20 ?execute@@YAHW4driver_phases@@PAUpassinfo_t@@PBG2PAPAG@Z proc near in this methods this line
.text:00426E16 call ds:__imp__LoadLibraryExW@12 ; LoadLibraryExW(x,x,x)
they call the loadlibraryExW without check and after that
.text:00426E1C mov ebx, eax
.text:00426E1E test ebx, ebx
.text:00426E20 jz short loc_426E5A
.text:00426E22 push offset aInvokecompiler_0 ; "_InvokeCompilerPassW@16"
.text:00426E27 push ebx ; hModule
.text:00426E28 mov esi, ds:__imp__GetProcAddress@8 ; GetProcAddress(x,x)
.text:00426E2E call esi ; GetProcAddress(x,x) ; GetProcAddress(x,x)
.text:00426E30 mov [ebp+var_28], eax
.text:00426E33 push offset aAbortcompilerp_0 ; "_AbortCompilerPass@4"
.text:00426E38 push ebx ; hModule
.text:00426E39 call esi ; GetProcAddress(x,x) ; GetProcAddress(x,x).text:00426E1C mov ebx, eax (pollute _AbortCompilerPass)
.text:00426E1E test ebx, ebx
.text:00426E20 jz short loc_426E5A
.text:00426E22 push offset aInvokecompiler_0 ; "_InvokeCompilerPassW@16"

[培训]Windows内核深度攻防:从Hook技术到Rootkit实战!

收藏
免费 87
支持
分享
最新回复 (46)
雪    币: 40
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
2
6666
2025-10-14 08:50
0
雪    币: 3789
活跃值: (4090)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
6
2025-10-14 09:18
0
雪    币: 2660
活跃值: (7274)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
谢谢分享
2025-10-14 09:41
0
雪    币: 2410
活跃值: (6759)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
学习一下
2025-10-14 10:25
0
雪    币: 598
活跃值: (362)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
6
学习一下
2025-10-14 13:12
0
雪    币: 2790
活跃值: (5496)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
7
学习下。
2025-10-14 15:24
0
雪    币: 491
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
8
学习一下
2025-10-14 15:40
0
雪    币: 208
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
9
mb_nmgtjmus 
  8 楼
学习一下
5小时前
2025-10-14 21:09
0
雪    币: 11882
活跃值: (3950)
能力值: ( LV5,RANK:71 )
在线值:
发帖
回帖
粉丝
10
学习一下
2025-10-15 14:18
0
雪    币: 402
活跃值: (1251)
能力值: ( LV7,RANK:105 )
在线值:
发帖
回帖
粉丝
11
学习一下
2025-10-15 16:28
0
雪    币: 14623
活跃值: (7274)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12

多谢楼主分享哟,学习了
2025-10-15 19:54
0
雪    币: 142
活跃值: (438)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
13
699999999999999999
2025-10-15 20:03
0
雪    币: 8965
活跃值: (3679)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
学习一下
2025-10-15 22:44
0
雪    币: 71
活跃值: (1778)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
666
2025-10-16 09:07
0
雪    币: 108
活跃值: (1494)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
6666
2025-10-16 09:47
0
雪    币: 1662
活跃值: (2117)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
17
学习一下
2025-10-16 10:30
0
雪    币: 5634
活跃值: (9457)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
有空好好学习下
2025-10-17 09:18
0
雪    币: 0
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
19
谢谢分享
2025-10-17 13:52
0
雪    币: 9393
活跃值: (5611)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
20
谢谢分享
2025-10-18 10:11
0
雪    币: 5368
活跃值: (9614)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
21
厉害
2025-10-18 12:21
0
雪    币: 0
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
23
强大,看看文章
2025-10-20 12:13
0
雪    币: 144
活跃值: (1993)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
24
强大,看看文章
2025-10-20 16:10
0
雪    币: 556
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
25
学习一下
2025-10-20 17:05
0
游客
登录 | 注册 方可回帖
返回