@@ -84,11 +84,20 @@ def calculate_buffer_size(ea):
8484 instr = idc .GetDisasm (ea )
8585 buf = instr .split (',' )[1 ].strip ()
8686 if "+" in buf :
87- size = hex2int (buf [buf .find ('+' )+ 1 :len (buf )- 1 ])
88- return stack_offset (ea ) - size
87+ try :
88+ size = hex2int (buf [buf .find ('+' )+ 1 :len (buf )- 1 ])
89+ except ValueError :
90+ old = buf [buf .find ('+' )+ 1 :len (buf )]
91+ idc .OpHex (ea , - 1 )
92+ instr = idc .GetDisasm (ea )
93+ buf = instr .split (',' )[1 ].strip ()
94+ size = hex2int (buf [buf .find ('-' )+ 1 :len (buf )- 1 ])
95+ return size
96+ # return stack_offset(ea) - size
8997 elif "-" in buf :
9098 size = hex2int (buf [buf .find ('-' )+ 1 :len (buf )- 1 ])
91- return stack_offset (ea ) + size
99+ return size
100+ # return stack_offset(ea) + size
92101
93102BUFFER_SIZE = 0
94103def stack_calc (function ):
@@ -134,11 +143,13 @@ def test_fgets(ea):
134143 try :
135144 if "offset" in arg_dict ['s' ]:
136145 section = arg_dict ['s' ].split (' ' )[1 ]
137- arg_dict ['s' ] = bss [section ]
146+ arg_dict ['s' ] = bss [section ]
138147 except TypeError :
139148 arg_dict ['s' ] = calculate_buffer_size (arg_dict ['s' ])
140149
141150 if arg_dict ['s' ] < arg_dict ['n' ]:
151+ print 'Found vulnerable fgets'
152+ print 'buf Size: ' + str (arg_dict ['s' ])
142153 return True , hex (ea ), arg_dict
143154 else :
144155 return False , hex (ea ), arg_dict
@@ -222,9 +233,9 @@ def test_read(ea):
222233while item != BADADDR :
223234 next = idc .NextHead (item , end )
224235 if next != BADADDR :
225- bss [idc .Name (item )] = next - item
236+ bss [idc .Name (item )] = next - item
226237 else :
227- bss [idc .Name (item )] = end - item
238+ bss [idc .Name (item )] = end - item
228239 item = next
229240
230241### Populate Functions
@@ -272,4 +283,6 @@ def test_read(ea):
272283for x in find_calls ('read' ):
273284 res = test_read (x )
274285 print '\t ' , res
275- if res [0 ]: idc .SetColor (res [1 ], CIC_ITEM , 0x0000ff )
286+ if res [0 ]: idc .SetColor (int (res [1 ], 16 ), CIC_ITEM , 0x0000ff )
287+
288+
0 commit comments