def alloc(size, name): '''Allocates a segment of the given size.''' # first lets get the last segment in this binary last_seg_end = idaapi.get_last_seg().endEA # and the first first_seg_start = idaapi.get_first_seg().startEA # now see how many bytes we have from there to 0xFFFFFFFF bytes_high = 0xFFFFFFFF - last_seg_end # now see how many bytes we have from 0x0 to the first segments start bytes_low = first_seg_start # check where we have more room if bytes_high > bytes_low: print "[*] segment.py: there is room above current segments" new_seg_start = last_seg_end + 0x10000 new_seg_start = new_seg_start & 0xFFFF0000 else: print "[*] segment.py: there is room below current segments" new_seg_start = 0 + 0x1000 idc.SegCreate(new_seg_start, new_seg_start+size, 0, True, 3, 2) idc.SegRename(new_seg_start, name) return new_seg_start