-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_lib.py
More file actions
65 lines (53 loc) · 1.68 KB
/
build_lib.py
File metadata and controls
65 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import os
import struct
import subprocess
MCT_MAGIC = 0x4D435431
def build_lib(c_file, output_mct):
print(f"[*] Building Lib {c_file} -> {output_mct}")
base_name = os.path.splitext(c_file)[0]
o_file = f"{base_name}.o"
elf_file = f"{base_name}.elf"
bin_file = f"{base_name}.bin"
ld_file = "lib.ld"
with open(ld_file, "w") as f:
f.write("""
OUTPUT_FORMAT("elf32-i386")
ENTRY(_start)
SECTIONS {
. = 0x03000000;
.text : {
KEEP(*(.export_table))
*(.text*)
}
.rodata : { *(.rodata*) }
.data : { *(.data*) }
.bss : { *(.bss*) *(COMMON) }
}
""")
try:
subprocess.run(["gcc", "-m32", "-ffreestanding", "-fno-stack-protector", "-fno-pie", "-fno-pic", "-static", "-O0", "-s", "-c", c_file, "-o", o_file], check=True)
subprocess.run(["ld", "-m", "elf_i386", "-T", ld_file, o_file, "-o", elf_file], check=True)
subprocess.run(["objcopy", "-O", "binary", elf_file, bin_file], check=True)
except subprocess.CalledProcessError:
print("[!] Compilation failed!")
return
try:
with open(bin_file, "rb") as f:
code_data = f.read()
except FileNotFoundError:
print("[!] Binary file not found!")
return
code_size = len(code_data)
entry_point = 0
data_size = 4096
header = struct.pack("<IIII", MCT_MAGIC, entry_point, code_size, data_size)
with open(output_mct, "wb") as f:
f.write(header)
f.write(code_data)
print(f"[+] Lib Success! {output_mct} created.")
os.remove(o_file)
os.remove(bin_file)
os.remove(ld_file)
if __name__ == "__main__":
build_lib(sys.argv[1], sys.argv[2])