Skip to content

Commit d19b15c

Browse files
committed
reusing existing PLT table with symbolization
This is for analyzing statically-linked binaries. After recompilation, the PLT table size and entries may change, making it unreliable to use the new PLT table.
1 parent d86579c commit d19b15c

17 files changed

Lines changed: 410 additions & 196 deletions

.github/workflows/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ on:
55
branches:
66
- dev
77
- main
8-
- instrument-framework
98
jobs:
109
execute-tests-ubuntu22:
1110
runs-on: ubuntu-22.04

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
all:
44
test/test_all.py -a -c
55
test/test_instrument.sh 2>&1 | tee test.instrument
6-
rm src/points.ins src/fun.o || true
6+
rm src/points/* src/fun.o || true
77
test/test_action.sh 2>&1 | tee test.all
88
test/test_coreutils.sh 2>&1 | tee test.coreutils
99

src/ail.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ object (self)
145145
let module S = Symbol_table_get in
146146
let module I = Instrumentation in
147147
let _ = self#pre_process in
148+
(* .text section is in instrs.info *)
148149
let il, fl, re = D.disassemble f funcs secs arch in
149150

150151
print_endline "3: analysis";

src/analysis_process.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ module Analysis = struct
1010
open Type
1111
open Reassemble_symbol_get
1212

13+
let instrument_got_plt () =
14+
let filelines = read_file "got_plt.info" in
15+
let help acc l =
16+
let items =
17+
Str.split (Str.regexp " +") l
18+
|> List.filter (fun i -> i <> "00000000")
19+
|> List.map (fun i ->
20+
let addr = String.sub i 6 2
21+
^ String.sub i 4 2
22+
^ String.sub i 2 2
23+
^ String.sub i 0 2
24+
in
25+
"S_" ^ (dec_hex (int_of_string ("0x" ^ addr)))
26+
)
27+
in
28+
items @ acc
29+
in
30+
List.fold_left help [] filelines
31+
1332
let global_bss () =
1433
let filelines = read_file "globalbss.info"
1534
and help acc l =

src/flow_insensitive_analysis.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ let got_rewrite_instr
2727
prefix,
2828
tags )
2929
when got_reg = (p_exp (Reg reg1)) ->
30-
(*let point = "INSERT BEFORE [" ^ (dec_hex loc.loc_addr)
31-
^ "] SELF [] \"\" asm x \"add $_GLOBAL_OFFSET_TABLE_, "
32-
^ (p_reg reg1) ^ "\";" in
33-
U.write_file [point] "points0.ins";*)
3430
(* reg1 contains GOT pointer,
3531
* reg2 contain index,
3632
* const1 contains multipler,
3733
* const2 contains offset
3834
* EX: 808a4d6: jmp *-0x2514(%ebp,%eax,4) ---> jmp *S_0x80F7508(,%eax,0x4)
3935
*)
40-
let got_plus_offset = got_addr + const2 in
36+
let got_plus_offset = got_addr - const2 in
4137
Hashtbl.replace
4238
result
4339
loc.loc_addr

src/init.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ object (self)
8686
> got.info");
8787
let module EU = ELF_utils in
8888
if EU.elf_static () then begin
89+
ret := Sys.command(objdump_command ^ " -s -j \
90+
.got.plt "^f^" | grep \"^ \" | cut -d \" \" -f3,4,5,6 \
91+
> got_plt.info");
8992
ret := Sys.command(objdump_command ^ " -d -j \
9093
.plt "^f^" | grep jmp | wc -l > plt_entries.info");
9194
ret := Sys.command(objdump_command ^ " -s -j \
@@ -134,8 +137,13 @@ object (self)
134137
else failwith "unsupported architecture for fields" in
135138
ignore (Sys.command("cat "^f^".disassemble | grep \"^ \" | cut -f"^fields^" \
136139
> instrs.info"));
140+
let module EU = ELF_utils in
141+
if EU.elf_static () then
142+
( ignore (Sys.command("cat plt_whole.info | grep \"^ \" | cut -f1,3 \
143+
> plt_whole2.info"));
144+
ignore (Sys.command("cat plt_whole2.info >> instrs.info")) );
137145
ignore (Sys.command("python3 filter_nop.py"));
138-
ignore (Sys.command("cut -f 1 instrs.info > text_mem.info"))
146+
ignore (Sys.command("cut -f 1 instrs.info > text_mem.info"));
139147

140148
method user_func_process (f : string) : unit =
141149
ignore (Sys.command("cat "^f^".disassemble | grep \"<\" | grep \">:\" \
@@ -147,12 +155,12 @@ object (self)
147155
(ignore (Sys.command("readelf -SW " ^ f ^ " | awk \'FNR<15\' | \
148156
awk \'/data|bss|got|__libc_IO_vtables|\
149157
__libc_freeres_ptrs/ {print $3,$5,$6,$7} \' | \
150-
awk \' $1 != \".got.plt\" {print $1,$2,$3,$4}\' \
158+
awk \' {print $1,$2,$3,$4}\' \
151159
> sections.info"));
152160
ignore (Sys.command("readelf -SW " ^ f ^ " | awk \'FNR>14\' | \
153161
awk \'/data|bss|got|__libc_IO_vtables|\
154162
__libc_freeres_ptrs/ {print $2,$4,$5,$6} \' | \
155-
awk \' $1 != \".got.plt\" {print $1,$2,$3,$4}\' \
163+
awk \' {print $1,$2,$3,$4}\' \
156164
>> sections.info")))
157165
else
158166
(ignore (Sys.command("readelf -SW " ^ f ^ " | awk \'FNR<15\' | \
@@ -300,8 +308,8 @@ let main () =
300308
let _ = Random.self_init () in
301309
let init = new ailInit in
302310
( init#init arch bit_mode;
303-
init#disassemble elf arch;
304-
init#process elf arch bit_mode;
311+
init#disassemble elf arch; (* create text and data files *)
312+
init#process elf arch bit_mode; (* create other text files *)
305313
init#ail_process(elf) )
306314
else
307315
print_string ("binary file "^elf^" doesn't exist\n")

src/parser.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ class parse =
7878
{loc_label=""; loc_addr=(int_of_string s); loc_visible = true;}
7979

8080
and const_symb s =
81-
if s.[0] = '$' then
81+
if contains ~str:s ~sub:" # " && not (contains ~str:s ~sub:"*") then
82+
let parts = String.split_on_char '#' s in
83+
let abs_addr = String.trim (List.hd (List.rev parts)) in
84+
Point (int_of_string abs_addr)
85+
else
86+
( if s.[0] = '$' then
8287
let s' = String.sub s 1 ((String.length s)-1) in
8388
Normal (int_of_string s')
8489
else
85-
Point (int_of_string s) in
90+
Point (int_of_string s) ) in
8691

8792
(*
8893
and ptrtyp_symb = function

src/post_process.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
with open("final.s") as f:
99
lines = f.readlines()
1010

11+
def is_static():
12+
lines = []
13+
with open("elf.info") as f:
14+
lines = f.readlines()
15+
if "statically linked" in lines[0]:
16+
return True
17+
else:
18+
return False
19+
1120
def check_32():
1221
lines = []
1322
with open("elf.info") as f:
@@ -27,11 +36,12 @@ def check_32():
2736
if is_32:
2837
for i in range(ll):
2938
m = re.search(r'jmp\s+\*(%e\w{2})',lines[i])
30-
if m:
31-
if re.search(r'add\s+%e\w{2},'+m.group(1),lines[i-1]):
32-
lines[i-1] = "nop\n"
33-
elif re.search(r'add\s+%e\w{2},'+m.group(1),lines[i-2]):
34-
lines[i-2] = "nop\n"
39+
# TODO: why this...
40+
# if m:
41+
# if re.search(r'add\s+%e\w{2},'+m.group(1),lines[i-1]):
42+
# lines[i-1] = "nop\n"
43+
# elif re.search(r'add\s+%e\w{2},'+m.group(1),lines[i-2]):
44+
# lines[i-2] = "nop\n"
3545
else:
3646
for i in range(ll):
3747
m1 = re.search(r'jmp\s+\*(%r\w{2})', lines[i])
@@ -172,10 +182,14 @@ def check_exe():
172182

173183
main_symbol1 = mains[0].strip()
174184

175-
176185
def help(l):
177186
if main_symbol1 != "" and l.startswith(main_symbol1):
178187
l = ".globl main\nmain:\n"+l
188+
if os.path.exists("plt_handler.txt") and is_static():
189+
with open("plt_handler.txt") as f:
190+
plt_handler = f.readlines()
191+
l = l + ''.join(plt_handler)
192+
os.remove("plt_handler.txt")
179193
return l
180194
#print lines
181195
lines = list(map(lambda l : help(l), lines))

src/pp_print.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ let pp_print_list instr_list =
398398
match l with
399399
| h::t ->
400400
let s = pp_print_instr h in
401-
help (s::acc) t
401+
help (s :: acc) t
402402
| [] -> List.rev acc in
403403
help [] instr_list
404404

0 commit comments

Comments
 (0)