FunctionConventions - Pennsylvania State University

37

Transcript of FunctionConventions - Pennsylvania State University

Page 1: FunctionConventions - Pennsylvania State University
Page 2: FunctionConventions - Pennsylvania State University

Function  Conventions  

!  Standard  Entry  Sequence  (cdecl)  "  Save  the  old  base  pointer  "  Set  the  new  stack  base  pointer  "  Allocate  space  for  variables  __function:

push ebp ; 55

mov ebp, esp ; 8BEC

sub esp, x ; Not always present

__function:

enter ; C8

sub esp, x ; Not always present 2

Page 3: FunctionConventions - Pennsylvania State University

Function  Conventions  

!  Standard  Exit  Sequence  (cdecl)  "  Reload  old  stack  pointer  "  Reload  old  stack  base  "  Deallocate  space  for  variables  

...

mov esp, ebp ; 8BE5

pop ebp ; 5D

ret ; C3 near, CB far

...

leave ; C9

ret ; C3 near, CB far 3

Page 4: FunctionConventions - Pennsylvania State University

Function  Call  Conventions  

!  cdecl  "  Used  by  GCC  and  GNU  libraries  

!  stdcall  "  Used  by  Win32  API  "  Sometimes  incorrectly  called  “pascal”  

!  fastcall  "  Many  different  implementations  "  Not  standardized  

4

Page 5: FunctionConventions - Pennsylvania State University

Function  Call  Conventions  

! cdecl"  Parameters  pushed  right  to  left"  EAX,  ECX,  EDX  not  preserved (caller saved)"  Return  values  are  returned  in  EAX!  Floating  point  returns  in  ST0"  Caller  performs  clean-up

! stdcall"  Same  as  cdecl,  except  callee  clean-up

! RET imm  is  a  sign  of  this

! fastcall"  One  or  more  parameters  passed  in  registers"  MS  VC++,  GCC

! First  arg  #  ECX,  second  arg  #  EDX,  remainder  right  #  left on the stack

5

Page 6: FunctionConventions - Pennsylvania State University

cdecl  Function  Call  Convention  !  Push  Parameters  on  Stack  !  Call  the  Function  !  Save  and  Update  EBP  !  Save  Registers  that  Will  Be  Overwritten  !  Allocate  Local  Variables  !  Execute  Function  !  Release  Local  Storage  

6

Page 7: FunctionConventions - Pennsylvania State University

cdecl  Function  Call  Convention  !  Restore  Saved  Registers  !  Restore  EBP  !  Return  !  Clean  Up  Parameters  

7

Page 8: FunctionConventions - Pennsylvania State University

stdcall  Function  Call  Convention  !  Push  Parameters  on  Stack  !  Call  the  Function  !  Save  and  Update  EBP  !  Save  Registers  that  Will  Be  Overwritten  !  Allocate  Local  Variables  !  Execute  Function  !  Release  Local  Storage  

8

Page 9: FunctionConventions - Pennsylvania State University

stdcall  Function  Call  Convention  !  Restore  Saved  Registers  !  Restore  EBP  !  Clean  Up  Parameters  !  Return  

9

gtan
Highlight
The order is different from cdecl
Page 10: FunctionConventions - Pennsylvania State University

Function  Call  Conventions  

!  Others  "  pascal  

!  Parameters  pushed  left  to  right  !  Windows  3.*  

"  syscall  !  Parameter  size  passed  in  AL  

"  safecall  !  Encapsulated  COM  error  handling  

"  thiscall  !  Either  caller  or  callee  clean-­‐up  

"  …  

10

Page 11: FunctionConventions - Pennsylvania State University

Control  Statements  

!  If-­‐Else  !  Switch  !  For  !  While  

11

Page 12: FunctionConventions - Pennsylvania State University

If-­‐Else  Statement  

12

Page 13: FunctionConventions - Pennsylvania State University

If-­‐Else  Statement  

13

Page 14: FunctionConventions - Pennsylvania State University

Switch  Statement  

14

Page 15: FunctionConventions - Pennsylvania State University

Switch  Statement  

15

Page 16: FunctionConventions - Pennsylvania State University

For  Statement  

16

Page 17: FunctionConventions - Pennsylvania State University

For  Statement  

17

Page 18: FunctionConventions - Pennsylvania State University

While  Statement  

18

Page 19: FunctionConventions - Pennsylvania State University

While  Statement  

19

Page 20: FunctionConventions - Pennsylvania State University

Determining  Signed-­‐ness  

!  Signed  and  Unsigned  Variables  "  Operations  on  signed/unsigned  variables  use  different  instructions  

"  IMUL/MUL  "  IDIV/DIV  "  Jcc  

20

Page 21: FunctionConventions - Pennsylvania State University

Determining  Signed-­‐ness  

21

Page 22: FunctionConventions - Pennsylvania State University

Tools  of  the  Trade  

!  Disassembler  "  Machine  code  to  instructions  

!  Decompiler  "  Instructions  to  code  (often  to  C  code)    

!  Debugger  "  Real-­‐time,  step-­‐thru-­‐code  debugging  

22

Page 23: FunctionConventions - Pennsylvania State University

Disassemblers  

!  Disassemblers  "  Converts  machine  code  to  instructions  

23

Page 24: FunctionConventions - Pennsylvania State University

Decompilers  

! Decompilers"  Attempt  to  convert  instructions  or  byte  codes  tohigherc level  languages    

! One way of dealing with architecture-dependentbinary code is to decompile it into somearchitecture-independent IR

24

Page 25: FunctionConventions - Pennsylvania State University

Debuggers  

!  Debuggers  "  Modes  

!  User-­‐mode  !  Kernel-­‐mode  

"  Common  features  !  Create/attach  to  a  process  !  Set/clear  breakpoint  !  Step  into/over  !  Trace  into/over  

25

Page 26: FunctionConventions - Pennsylvania State University

Debuggers  

!  Breakpoints  "  Software  breakpoints  

!  INT  3h  (\xCC)  "  Memory  breakpoints  "  Hardware  breakpoints  

!  Intel  Dr0-­‐Dr7  registers  !  Traces  

"  Records  instructions  and  execution  contexts  !  Stepping  

"  Step  into/over  

26

Page 27: FunctionConventions - Pennsylvania State University

GNU  Debugger  (gdb)  

!  Disassembler,  Debugger  "  Command-­‐line  

!  Insight  is  a  GUI  wrapper  for  gdb  

"  Not  just  for  Linux  !  Native  x86  Windows  support  !  Special  versions  for  various  architectures  

27

Page 28: FunctionConventions - Pennsylvania State University

GNU  Debugger  (gdb)  Breakpoint  Tutorial  

28

Page 29: FunctionConventions - Pennsylvania State University

GNU  Debugger  (gdb)  Breakpoint  Tutorial  

29

Page 30: FunctionConventions - Pennsylvania State University

OllyDbg  

!  Disassembler  !  Debugger  

"  Open  !  Creates  a  process  with  debug  privileges  

"  Attach  !  Attach  to  a  process  already  running  

"  Detach  (version  2.*)  !  Detaches  the  debugger  and  allows  the  process  to  continue  

"  Terminate  !  Kills  the  debuggee  

30

Page 31: FunctionConventions - Pennsylvania State University

IDA  

!  Disassembler,  Decompiler*,  Debugger  "  Commercial  debugger  

! With  freeware  and  demo  versions  

"  Now  a  Hex-­‐Rays  product  !  Formerly  Datarescue  

"  *Decompilers  sold  seperately  (and  is  expensive)  

54

Page 32: FunctionConventions - Pennsylvania State University

IDA  

55

Page 33: FunctionConventions - Pennsylvania State University

IDA  

!  Shortcuts  "  Run  (F9),  step  into  (F7),  step  over  (F8)  "  Set/clear  breakpoint  (F2)  "  Apply  name  to  an  address  (N)  "  Comment  (:),  repeatable  comment  (;)  "  Toggle  graph  view/assembly  view  (space)  "  Jump  to  name/address  (G)  "  Follow  reference  (enter)  "  Display/jump  to  cross-­‐references  (X)  "  Return  to  previous  location  (esc)  

56

Page 34: FunctionConventions - Pennsylvania State University

IDA  Patching  

!  Patching  "  Edit  “cfg/idagui.cfg”  "  Change  “DISPLAY_PATCH_SUBMENU”  to  “YES”  

57

Page 35: FunctionConventions - Pennsylvania State University

Hex-­‐Rays  Decompilers  

58

Page 36: FunctionConventions - Pennsylvania State University

WinDbg by Microsoft  

! Disassembler,  Debugger"  User/kernel-­‐mode  debugger

59

Page 37: FunctionConventions - Pennsylvania State University

Questions/Comments?  

60