Escolar Documentos
Profissional Documentos
Cultura Documentos
%e&ore we 'egin...
(his )resentation assumes you can re!erse x*6 code +ou might learn something e!en i& you can,tso don,t lea!e I& I go to &ast- yell at me .ind a mista/e- I drin/ THERE
SummerCon 2011
Intro to x64
1genda
Intro 2 3istory o& x64 (he x64 4lat&orm 5icroso&t x64 1%I Sys6 x64 1%I (ools &or re!ersing x64
SummerCon 2011
Intro to x64
I& you,re used to re!ersing 02 'it x*6 code- x64 can 'e con&using at &irst 7asy )arts
Instructions are mostly the same as you,re used to (here are a &ew more registers
3ard )arts
Calling con!ention is totally di&&erent 8e'ugging o)timi9ed code can 'e tric/y
Intro to x64 e!ersing " #on $arimer 4
SummerCon 2011
;ame sou)<
158
%S8 " am,64 $inux /ernel " x'6-64 >CC " am,64 8e'ian2?'untu " am,64 .edora2SuS7 " x'6-64 Solaris " am,64
Intel
=racle25icroso&t
SummerCon 2011
1111 " 158 announces x*6"64 2000 " 158 releases s)ecs 2001 " .irst x*6"64 $inux /ernel a!aila'le 200* " .irst 15864 =)erton released 2004 " Intel announces I1"02e27564(- releases &irst x64 @eon )rocessor 2002 " x64 !ersions o& Aindows @4 and Ser!er 2000 released 2001 " 5ac =S 10.6 BSnow $eo)ardC includes x64 /ernel 2001 " Aindows Ser!er 200* 2 only a!aila'le in x64 !ersion
2010 " :0D o& Aindows E installs running the x64 !ersion 2011 " 40D o& Steam users in 1)ril 2011 3A sur!ey use AinE x64
Intro to x64 e!ersing " #on $arimer 6
SummerCon 2011
SummerCon 2011
Intro to x64
Ahat is x64F
Can address u) to 64 'its B16E!C o& !irtual memoryI Can address u) to :2 'its B43!C o& )hysical memoryII
* new >4 registers BR8"R15C * new 12* 'it @55 registers BXMM8"XMM15C
;ew 64 'it instructions: cdqe- lodsq- stosq- etc 1'ility to re&erence data relati!e to instruction )ointer B ripC
I $imited 'y )rocessor im)lementation- most only su))ort 4* 'its now... II Intel currently su))orts 40 'its o& )hysical memory
SummerCon 2011 Intro to x64 e!ersing " #on $arimer *
$ong mode
Segment 'ase is always 0 exce)t &or FS and GS Stac/ BSSC- Code BCSC- 8ata BDSC always in the same segment
64 'it o)erands BRAX- RBX- ...C are s)eci&ied with G 7@ )re&ixH in the o)code encoding
SummerCon 2011
Canonical addresses
%it 60
%it 0
Current im)lementations only su))ort 4* 'it linear addresses Canonical &orm means most signi&icant 'it o& address is extended to 'it 60
0xFFFFFFFFFFFFFFFF
Canonical 3igh 4art
0xFFFF800000000000 0xFFFF7FFFFFFFFFFF
;on"canonical 1ddress ange
%its 0"4E are the address- 'its 4*"60 are the same as 'it 4E
Aindows uses high addresses &or /ernel- low addresses &or user mode ;on"canonical address access results in K>4
0x0000800000000000 0x00007FFFFFFFFFFF
Canonical $ow 4art
0x0000000000000000
4* 'it canonical address ranges
SummerCon 2011
Intro to x64
10
x64 registers
x!!8- x!! - ... x!!15 ?sed &or !ector and &loating )oint arithmetic
Intro to x64 e!ersing " #on $arimer 11
SummerCon 2011
Intel2158 16@
"!!0""!!7
$ow 12* 'its o& 16@ registers o!erla) with @55 BSS7C registers
x!!0"x!!7
1lso a &ew new instructions .irst C4?s with 16@ were the Intel Sandy %ridge )rocessors released L1 2011
$5% 1$8 #MM0 XMM0 0
SummerCon 2011
Intro to x64
12
x64
%' RAX RBX RCX RDX RBP RSI RDI RSP R8 R R10 R11 R1$ R1' R1( R15
egisters
'1 &AX &BX &CX &DX &BP &SI &DI &SP 0 RIP RF)AGS %' '1 &IP &F)AGS 0
SummerCon 2011
%'
15
R8D R8+
R8B,R8)
SummerCon 2011
Intro to x64
14
3ow many 'its is R DF 3ow many 'its is RSPF 3ow many 'its is R1$+F 3ow many 'its is R10BF 3ow many 'its is R1%F
SummerCon 2011
Intro to x64
1:
3ow many 'its is R DF *2 3ow many 'its is RSPF 3ow many 'its is R1$+F 3ow many 'its is R10BF 3ow many 'its is R1%F
SummerCon 2011
Intro to x64
16
3ow many 'its is R DF *2 3ow many 'its is RSPF 64 3ow many 'its is R1$+F 3ow many 'its is R10BF 3ow many 'its is R1%F
SummerCon 2011
Intro to x64
1E
3ow many 'its is R DF *2 3ow many 'its is RSPF 64 3ow many 'its is R1$+F 16 3ow many 'its is R10BF 3ow many 'its is R1%F
SummerCon 2011
Intro to x64
1*
3ow many 'its is R DF *2 3ow many 'its is RSPF 64 3ow many 'its is R1$+F 16 3ow many 'its is R10BF ' 3ow many 'its is R1%F
SummerCon 2011
Intro to x64
1J
3ow many 'its is R DF *2 3ow many 'its is RSPF 64 3ow many 'its is R1$+F 16 3ow many 'its is R10BF ' 3ow many 'its is R1%F Not a register???
SummerCon 2011
Intro to x64
20
SummerCon 2011
Intro to x64
21
Ahat,s in RAX a&ter each instructionF 00M230RAX40111111111111111150 RAX0700x1111111111111111 00I6C0A)000000000000000000000 00I6C0AX 00I6C0&AX
SummerCon 2011
Intro to x64
22
Ahat,s in RAX a&ter each instructionF 00M230RAX40111111111111111150 RAX0700x1111111111111111 00I6C0A)000000000000000000000 RAX0700x111111111111111$ 00I6C0AX 00I6C0&AX
SummerCon 2011
Intro to x64
20
Ahat,s in RAX a&ter each instructionF 00M230RAX40111111111111111150 RAX0700x1111111111111111 00I6C0A)000000000000000000000 RAX0700x111111111111111$ 00I6C0AX RAX0700x111111111111111' 00I6C0&AX
SummerCon 2011
Intro to x64
24
Ahat,s in RAX a&ter each instructionF 00M230RAX40111111111111111150 RAX0700x1111111111111111 00I6C0A)000000000000000000000 RAX0700x111111111111111$ 00I6C0AX RAX0700x111111111111111' 00I6C0&AX RAX0700x000000001111111(
SummerCon 2011
Intro to x64
2:
64 'it instructions
CD8& Con!ert dou'leword to Nuadword Bsign"extend &AX into RAXC CMPS8 Com)are Nword at RSI with Nword at RDI CMPXC*G1%B Com)are RDX:RAX with m128 )2DS8 $oad Nword at address RSI into RAX M23S8 5o!e Nword &rom address RSI to RDI M239X 9ero"extend dou'leword to Nuadword S:2S8 Store RAX at address RDI S#SCA))0.ast system call- re)lacement &or0S#S&6:&R S#SR&:0.ast system call- re)lacement &or S#S&XI:
Intro to x64 e!ersing " #on $arimer 26
SummerCon 2011
I4"relati!e addressing
!o;0rax40q<ord0ptr0=rip>0x1000?
Aindows: .ewer 'ase relocations in 47 &iles $inux: ;o >=( )ointer setu) in &unction )rologue ;o )re"lin/ing and no )er&ormance hit &or 1S$ on x64
SummerCon 2011
Intro to x64
2E
I4"relati!e addressing
I81 has G7x)licit I4 addressingH mode in analysis o)tions so you can see when rip"relati!e addresses are used:
SummerCon 2011
Intro to x64
2*
5icroso&t,s x64 1%I BAindowsC Sys6 x64 1%I B$inux- %S8- 5acC
Intro to x64 e!ersing " #on $arimer 2J
SummerCon 2011
SummerCon 2011
Intro to x64
00
(here,s only one calling con!ention Bno cdecl2stdcall2&astcallC Calling con!ention modeled a&ter &astcall
Some registers are considered !olatile across &unction calls- some are not
SummerCon 2011
eturn
RCX- RDX- R8- R &or integers XMM0- XMM1- XMM$- XMM' &or &loats
.or !aria'le arguments B!arargsC- &loating )oint !alues are stored in the &loating )oint and integer registers< i.e.- 4arameter 2 is always RDX or XMM1 1ny )arameter P * 'ytes )assed 'y re&erence Bno s)littingC
XMM0 used &or &loats- dou'les- and 12* 'it ty)es B__m128C
SummerCon 2011
Intro to x64
02
1ll str@cts o!er * 'ytes are )assed 'y re&erence Caller allocates s)ace and co)ies the str@ct 'e&ore )assing to the callee
(his is to a!oid )ro'lems with the callee modi&ying the caller,s co)y
Intro to x64 e!ersing " #on $arimer 00
SummerCon 2011
SummerCon 2011
Intro to x64
04
SummerCon 2011
Intro to x64
0:
SummerCon 2011
Intro to x64
06
4=4 L?IM K0
Ahat registers are used &or the &irst &our integer )arameters o& a &unctionF (rue2.alse: I& a structure has two 64 'it !alues- it can 'e )assed to a &unction s)lit across two registers Bi.e.- r8 and r C
SummerCon 2011
Intro to x64
0E
4=4 L?IM K0
Ahat registers are used &or the &irst &our integer )arameters o& a &unctionF
&CX- &DX- R8- R
(rue2.alse: I& a structure has two 64 'it !alues- it can 'e )assed to a &unction s)lit across two registers Bi.e.- r8 and r C
SummerCon 2011
Intro to x64
0*
4=4 L?IM K0
Ahat registers are used &or the &irst &our integer )arameters o& a &unctionF
&CX- &DX- R8- R
(rue2.alse: I& a structure has two 64 'it !alues- it can 'e )assed to a &unction s)lit across two registers Bi.e.- r8 and r C
:"LSE&
SummerCon 2011
Intro to x64
0J
Some registers are !olatile and can 'e destroyed 'y &unctions
RAX- RCX- RDX- R8- R - R10- R11 +ou can,t rely on them 'eing the same a&ter calling a &unction Bthe com)iler might 'e a'le to...C
Some registers are non"!olatile and must 'e sa!ed 'y &unctions that use them
RBX- RBP- RDI- RSI- R1$- R1'- R1(- R15 +ou can rely on them 'eing the same a&ter calling a &unction 1 &unction that needs these registers must sa!e them to the stac/ and )o) them o&& 'e&ore returning
Intro to x64 e!ersing " #on $arimer 40
SummerCon 2011
.unction )rologue needs to allocate stac/ s)ace &or sa!ed registers- local !aria'les- arguments to callees 4arameters are always at 'ottom o& stac/- right a'o!e return address
(here,s always s)ace &or 4 )arameters- e!en i& they,re not used Bhome s)aceC
(his means address ends in 9ero hex 7xce)t within )rologue ?nless the &unction doesn,t call any other &unctions
1ll memory 'eyond RSP is !olatile Bcould 'e used 'y the =S or a de'uggerC ;o &rame )ointer Bi.e.- no !o;0rbp40esp in )rologueC unless stac/ is dynamically allocated BallocaC
Intro to x64 e!ersing " #on $arimer 41
SummerCon 2011
Caller,s )rologue allocates stac/ s)ace &or arguments to callees .or non"lea& &unctions- s)ace &or &our arguments is always allocated B4 I * 'ytes R 02 R 0x20C
s@b0esp400x$0
See) in mind that a&ter this instruction- stac/ needs to 'e aligned on 16 'yte 'oundary Bend in 0 hexC
In de'ug code- the callee usually )uts the register )arameters there in the )rologue In o)timi9ed- code- all 'ets are o&&- callee can do whate!er it wants
Intro to x64 e!ersing " #on $arimer 42
SummerCon 2011
Intro to x64
40
0FD0 0FD8 0F&0 0F&8 0FF0 0FF8 1000 1010 1018 10$0 10$8
ArspF00005o!e0space Arsp>08F05o!e0space Arsp>10F05o!e0space Arsp>18F05o!e0space Arsp>$0F0lpC!d)i/e Arsp>$8F0III Arsp>'0F0III Arsp>(0F05I/sta/ce Arsp>(8F05Pre;I/sta/ce Arsp>50F0lpC!d)i/e Arsp>58F0/S5o<C!d
1008 ret@r/0addr
Arsp>'8F0Aret@r/0to0Ht!ai/CR:EEF
SummerCon 2011
Intro to x64
4:
ArspF00005o!e0space Arsp>08F0arJHa Arsp>10F0arJHb Arsp>18F0arJHc Arsp>$0F0arJHd Arsp>$8F0lpC!d)i/e Arsp>'0F0III Arsp>'8F0III Arsp>(0F0Aret@r/0to0Ht!ai/CR:EEF Arsp>(8F05I/sta/ce Arsp>50F05Pre;I/sta/ce Arsp>58F0lpC!d)i/e Arsp>%0F0/S5o<C!d
Intro to x64
46
=)timi9ed code ;ote that the +i/Mai/ )arameters are not sa!ed in their home s)ace 1lso note that 0x2* 'ytes o& stac/ s)ace are still reser!ed &or the )arameters to 5essage%ox1
SummerCon 2011
Intro to x64
4E
SummerCon 2011
Intro to x64
4*
?sed 'y $inux- %S8- 5ac- others (otally di&&erent than 5S x64 1%I
Some registers considered !olatile and can change across &unction calls- others must 'e sa!ed 'y the callee
Intro to x64 e!ersing " #on $arimer 4J
SummerCon 2011
.irst a!aila'le register &or the )arameter ty)e is used 6 registers &or integer )arameters
XMM0"XMM7
;o o!erla)- so you could ha!e 14 )arameters stored in registers str@ct )arams can 'e s)lit 'etween registers 7!erything else is on the stac/ RAX holds num'er o& !ector registers BXMMxC
Intro to x64 e!ersing " #on $arimer :0
SummerCon 2011
7xam)les< i/t01@/c1Ai/t0a401loat0b40i/t0cF
rax01@/c1Ardi40x!!040rsiF
1loat01@/c$A1loat0a40i/t0b401loat0cF
x!!001@/c$Ax!!040rdi40x!!1F
1loat01@/c'A1loat0a40i/t0b40i/t0cF
x!!001@/c'Ax!!040rdi40rsiF
SummerCon 2011
SummerCon 2011
Intro to x64
:2
;othing new here- exce)t changes due to 64 'it )lat&orm 1ligned on 16 'yte 'oundaries >CC still uses
SummerCon 2011
Intro to x64
:4
x64
e!ersing (ools
SummerCon 2011
Intro to x64
::
e!ersing: I81
e!ersing: Aind'g
e!ersing: ed'
8ynamic instrumentation
4I; 8ynamo I=
6irtual machines
%=C3S L75?
(a/e a 'inary- any 'inary- 'ut smaller is )ro'a'ly easier e!erse it all
;ame e!ery &unction- )arameter- and !aria'le Comment almost e!ery line o& assem'ly 8o this without running it- unless you a'solutely ha!e to
+ou,ll 'e a )ro in no time< 1lso- read the 00: ol& olles inter!iew in 3I(%
SummerCon 2011
Intro to x64
61
x64
e&erences<
x64 architecture
Intel 1rchitecture So&tware 8e!elo)ment 5anuals: htt):22www.intel.com2)roducts2)rocessor2manuals2 158 1rchitecture 4rogrammer,s 5anuals: htt):22de!elo)er.amd.com2documentation2guides2)ages2de&ault.as)x
5S x64 1%I
SummerCon 2011
LuestionsF
Contact in&o:
SummerCon 2011
Intro to x64
60