def
add_symbol():
binary
=
lief.parse(TARGET_BIN)
add_bin
=
lief.parse(
"libadd.so"
)
add_got
=
add_bin.get_section(
".got"
)
add_data
=
add_bin.get_section(
".data"
)
add_plt
=
add_bin.get_section(
".plt"
)
add_text
=
add_bin.get_section(
".text"
)
before_add_load_num
=
0
for
i
in
binary.segments:
if
i.
type
=
=
ELF.SEGMENT_TYPES.LOAD:
before_add_load_num
+
=
1
print
(before_add_load_num)
add_RE_seg
=
lief.ELF.Segment()
add_RE_seg.alignment
=
0x1000
add_RE_seg.
type
=
ELF.SEGMENT_TYPES.LOAD
add_RE_seg.add(ELF.SEGMENT_FLAGS.X)
add_RE_seg.add(ELF.SEGMENT_FLAGS.R)
add_RE_seg.content
=
add_plt.content
+
add_text.content
print
(
"add_RE_seg.content :"
, add_RE_seg.content)
binary.add(add_RE_seg)
add_RW_seg
=
lief.ELF.Segment()
add_RW_seg.alignment
=
0x1000
add_RW_seg.
type
=
ELF.SEGMENT_TYPES.LOAD
add_RW_seg.add(ELF.SEGMENT_FLAGS.W)
add_RW_seg.add(ELF.SEGMENT_FLAGS.R)
add_RW_seg.content
=
add_got.content
+
add_data.content
binary.add(add_RW_seg)
print
(add_got.content)
addbin_relplt
=
add_bin.pltgot_relocations
add_sym_value
=
list
()
for
add_entry
in
addbin_relplt:
if
binary.has_symbol(add_entry.symbol.name):
sym
=
binary.get_symbol(add_entry.symbol.name)
else
:
sym
=
binary.add_dynamic_symbol(add_entry.symbol)
add_sym_value.append(add_entry.symbol.value
-
add_text.virtual_address)
print
(
hex
(add_entry.symbol.value),
hex
(add_entry.symbol.value
-
add_text.virtual_address))
add_reloc
=
ELF.Relocation()
add_reloc.
type
=
add_entry.
type
add_reloc.symbol
=
sym
add_reloc.address
=
add_entry.address
-
add_got.virtual_address
add_reloc.purpose
=
ELF.RELOCATION_PURPOSES.PLTGOT
add_reloc
=
binary.add_pltgot_relocation(add_reloc)
binary.write(
"intermediate.so"
)
inter
=
lief.parse(
"intermediate.so"
)
add_RE_seg_virtual_address
=
0
add_RW_seg_virtual_address
=
0
after_add_load_num
=
0
for
i
in
binary.segments:
if
i.
type
=
=
ELF.SEGMENT_TYPES.LOAD:
after_add_load_num
+
=
1
if
after_add_load_num > before_add_load_num
and
after_add_load_num <
=
before_add_load_num
+
2
:
if
i.has(ELF.SEGMENT_FLAGS.X):
add_RE_seg_virtual_address
=
i.virtual_address
if
i.has(ELF.SEGMENT_FLAGS.W):
add_RW_seg_virtual_address
=
i.virtual_address
print
(
hex
(add_RE_seg_virtual_address))
print
(
hex
(add_RW_seg_virtual_address))
new_dynsym_content
=
inter.get_section(
".dynsym"
).content
add_dynsym_start
=
len
(new_dynsym_content)
-
len
(add_sym_value)
*
16
print
(
"add_dynsym_start:"
,add_dynsym_start)
modify_dynsym_content
=
[]
inx
=
0
for
entry_content
in
[new_dynsym_content[i:i
+
16
]
for
i
in
range
(add_dynsym_start,
len
(new_dynsym_content),
16
)]:
entry
=
DynSymEntry.parse_from_content(entry_content)
if
(entry.sym_value !
=
0
):
print
(
hex
(entry.sym_value))
entry.sym_value
=
add_sym_value[inx]
+
add_RE_seg_virtual_address
+
len
(add_plt.content)
print
(
hex
(entry.sym_value))
inx
+
=
1
modify_dynsym_content
+
=
entry.content
patch_file(
"intermediate.so"
, inter.get_section(
".dynsym"
).offset
+
add_dynsym_start, modify_dynsym_content)
modify_rel_content
=
[]
relplt
=
binary.get_section(
".rel.plt"
)
add_rel_start
=
binary.get_section(
".rel.plt"
).size
-
len
(add_sym_value)
*
8
print
(
"add_rel_start :"
,
hex
(add_rel_start))
add_entry_ndx
=
0
for
rel_content
in
[relplt.content[i:i
+
8
]
for
i
in
range
(add_rel_start,
len
(relplt.content),
8
)]:
rel
=
RelEntry.parse_from_content(rel_content)
if
(rel.offset !
=
0
):
print
(
"offset :"
,
hex
(rel.offset))
rel.offset
=
rel.offset
+
add_RW_seg_virtual_address
print
(
"offset :"
,
hex
(rel.offset))
modify_rel_content
+
=
rel.content
add_entry_ndx
+
=
1
patch_file(
"intermediate.so"
, inter.get_section(
".rel.plt"
).offset
+
add_rel_start, modify_rel_content)