Guest User

findIOSSyscallTable.py

a guest
Sep 7th, 2011
3,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.41 KB | None | 0 0
  1. # This script is part of the IDA IOS Toolkit
  2. # (C) Copyright 2011 Stefan Esser
  3.  
  4. # This script searches the iOS syscall table within the iOS kernelcache
  5. # without relying on symbols because Apple loves to either remove them
  6. # or to move them around
  7.  
  8.  
  9. from idaapi import *
  10. from idc import *
  11. import idautils
  12. import re
  13.  
  14.  
  15. def parsesyscallsmaster(filename):
  16.     '''
  17.     parsesyscallsmaster: filename
  18.    
  19.     Parses the syscalls.master file to retrieve names and arguments
  20.     for syscalls.
  21.     '''
  22.    
  23.     syscallinfo = {}
  24.    
  25.     syscalldef_re = re.compile('(?P<num>[0-9]+)\s+([a-zA-Z0-9_]+)\s+([a-zA-Z0-9_]+)\s+\{\s+([a-zA-Z0-9_]+)\s+([a-zA-Z0-9_]+)\s*\(([^)]+)\)')
  26.    
  27.     try:
  28.         with open(filename) as f:
  29.             for line in f:
  30.                 m = syscalldef_re.match(line)
  31.                 if m:
  32.                     num = int(m.group(1))
  33.                     name = m.group(5)
  34.                     params = m.group(6)
  35.                     if not syscallinfo.has_key(num):
  36.                         syscallinfo[num] = { "name" : name, "params" : params }
  37.                     else:
  38.                         # Overwrite if not nosys
  39.                         if name != "nosys":
  40.                             syscallinfo[num] = { "name" : name, "params" : params }
  41.     except IOError, err:
  42.         pass
  43.  
  44.     return syscallinfo
  45.    
  46.  
  47. def registersysentstruct():
  48.     '''
  49.     registersysentstruct:
  50.    
  51.     Registers the 'sysent' struct type in IDA.
  52.     '''
  53.  
  54.     strsysent = """
  55.  
  56. struct sysent {
  57.     unsigned short sy_narg;
  58.     unsigned char sy_resv;
  59.     unsigned char sy_flags;
  60.     void (*sy_call)();
  61.     void (*sy_arg_munge32)();
  62.     void (*sy_arg_munge64)();
  63.     unsigned int sy_return_types;
  64.     unsigned short sy_arg_bytes;
  65. };
  66.  
  67. """
  68.     r = idc.SetLocalType(-1, strsysent, 0)
  69.     r = Til2Idb(-1, "sysent")
  70.    
  71.  
  72. def findsyscalltable(syscallinfo):
  73.     '''
  74.     findsyscalltable: syscallinfo
  75.    
  76.     Searches for the syscall table inside the iOS kernel
  77.     binary. Search is performed inside the __data segment
  78.     and syscall table is recognized by a combination of
  79.     a pattern match for the first syscall handler and the
  80.     assumption that the syscall table is immediately
  81.     followed by the nsysent variable that contains the
  82.     number of syscall handlers.
  83.     '''
  84.    
  85.     # retrieve information about 'sysent' struct
  86.     sid = get_struc_id("sysent")
  87.     ssize = get_struc_size(sid)
  88.     if ssize == 0:
  89.         print "Could not load information about 'sysent' struct"
  90.         return
  91.    
  92.     # text segment
  93.     textsegment = get_segm_by_name("__text")
  94.     if not textsegment:
  95.         print "Could not find __text segment"
  96.         return
  97.        
  98.     # syscall table is assumed to be found in section ''
  99.     syscalltablesegment = get_segm_by_name("__data")
  100.     if not syscalltablesegment:
  101.         print "Could not find segment __data"
  102.         return
  103.  
  104.     curEA = syscalltablesegment.startEA
  105.     endEA = syscalltablesegment.endEA
  106.    
  107.     while curEA < endEA:   
  108.        
  109.         # signature of first syscall entry
  110.         #
  111.         # 0x00000000
  112.         # handler
  113.         # 0x00000000
  114.         # 0x00000000
  115.         # 0x00000001
  116.         # 0x00000000
  117.        
  118.         curEA += 4
  119.        
  120.         if idc.Dword(curEA) != 0:
  121.             continue
  122.         handler = idc.Dword(curEA+4)
  123.         if handler < textsegment.startEA:
  124.             continue
  125.         if handler > textsegment.endEA:
  126.             continue
  127.         if idc.Dword(curEA+8) != 0:
  128.             continue
  129.         if idc.Dword(curEA+12) != 0:
  130.             continue
  131.         if idc.Dword(curEA+16) != 1:
  132.             continue
  133.         if idc.Dword(curEA+20) != 0:
  134.             continue
  135.            
  136.         # Passed signature of syscall handler 0
  137.         # now check if we can find the end
  138.        
  139.         # Assume a syscall table longer than 32 entries
  140.         cnt = 31
  141.         innerEA = curEA + 31 * ssize   
  142.                
  143.         while innerEA < endEA:
  144.             innerEA += ssize
  145.             cnt += 1
  146.             # Assume less than 700 syscalls for now
  147.             if cnt > 700:
  148.                 break
  149.                
  150.             nsys = idc.Dword(innerEA)
  151.             if nsys == cnt:
  152.                 # Found end of syscall table
  153.                
  154.                 # mark nsysent variable
  155.                 idc.MakeDword(innerEA)
  156.                 idaapi.set_name(innerEA, "_nsysent", 0)
  157.                
  158.                 # mark syscall table as an array of struct 'sysent'
  159.                 idaapi.do_unknown_range(curEA, ssize * nsys, DOUNK_DELNAMES)
  160.                 idaapi.set_name(curEA, "_sysent", 0)
  161.                 idaapi.doStruct(curEA, ssize, sid)
  162.                 idc.MakeArray(curEA, nsys)
  163.                 idc.SetArrayFormat(curEA, AP_INDEX | AP_IDXDEC, 1, -1)
  164.                
  165.                 # loop through all syscall handlers and set functype
  166.                 i = 0
  167.                 ht = {}
  168.                 while i < nsys:
  169.                     handler = idc.Dword(curEA + ssize * i + 4)
  170.                    
  171.                     # We have to support ARM THUMB code
  172.                     addr = handler & 0xFFFFFFFE
  173.                    
  174.                     if syscallinfo.has_key(i):
  175.                         si = syscallinfo[i]
  176.                    
  177.                         # check for name collision
  178.                         if not ht.has_key(handler):
  179.                             ht[handler] = si["name"]
  180.                            
  181.                             # set name of syscall
  182.                             idaapi.set_name(addr, "_" + si["name"])
  183.                            
  184.                         if si["params"] == "void":
  185.                             typestring = "void"
  186.                         else:
  187.                             typestring = "struct %s_args" % (si["name"])
  188.                             params = si["params"].replace(",", ";")
  189.                             print typestring + "{" + params + ";};"
  190.                             r = idc.SetLocalType(-1, typestring + "{" + params + ";};", 0)
  191.                             Til2Idb(-1, "%s_args" % (si["name"]))
  192.                        
  193.                         functype = "int xxx(proc_t p, %s *uap, int *retval);" % (typestring)
  194.                         r = idc.SetType(addr, functype)
  195.                     i = i + 1
  196.                
  197.                 # notify user
  198.                 print "Found syscall table _sysent at %08x" % (curEA)
  199.                 print "Number of entries in syscall table _nsysent = %u" % (nsys)
  200.                 print "Syscall number count _nsysent is at %08x" % (innerEA)
  201.                 idc.Jump(curEA)
  202.                 return
  203.  
  204.  
  205. if __name__ == '__main__':
  206.     registersysentstruct()
  207.     fname = idc.AskFile(0, "syscalls.master", "Find /bsd/kern/syscalls.master from XNU source code")
  208.     syscallinfo = parsesyscallsmaster(fname)
  209.     findsyscalltable(syscallinfo)
  210.     print "Done."
  211.  
  212. # TODO: fix problems with unknown types
Advertisement
Add Comment
Please, Sign In to add comment