-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbin2lang.py
141 lines (126 loc) · 4.22 KB
/
bin2lang.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
from pathlib import Path
from enum import IntEnum
from os.path import isfile
from io import BytesIO, StringIO
from argparse import ArgumentParser
from typing import Union, BinaryIO, TextIO
lowercase = lambda s: s.lower()
class Language(IntEnum):
PYTHON = 0
C = 1
CPLUSPLUS = 2
CSHARP = 3
PHP = 4
PHP_NEW = 5
PHP_OLD = 6
def lang_format(din: Union[str, bytes, bytearray], dout: Union[str, TextIO] = None, language: Language = Language.CPLUSPLUS, var_name: str = "data", byte_count: int = 16) -> str:
sin: BinaryIO = None
if type(din) == str:
sin = open(din, "rb")
elif type(din) in [bytes, bytearray]:
sin = BytesIO(din)
with StringIO() as sio:
if language == Language.PYTHON:
print(f"{var_name} = bytearray([", file=sio)
lines = []
while True:
data = sin.read(byte_count)
if not data:
break
lines.append("\t" + ", ".join([f"0x{x:02X}" for x in data]) + ",")
lines[-1] = lines[-1].rstrip(",")
[print(x, file=sio) for x in lines]
print("])", file=sio)
elif language in [Language.C, Language.CPLUSPLUS]:
print("#ifndef BYTE", file=sio)
print("typedef unsigned char BYTE", file=sio)
print("#endif", file=sio)
print(file=sio)
print(f"#ifndef __{var_name}__", file=sio)
print(f"#define __{var_name}__", file=sio)
print(f"BYTE {var_name}[] = {{", file=sio)
lines = []
while True:
data = sin.read(byte_count)
if not data:
break
lines.append("\t" + ", ".join([f"0x{x:02X}" for x in data]) + ",")
lines[-1] = lines[-1].rstrip(",")
[print(x, file=sio) for x in lines]
print("};", file=sio)
print("#endif", file=sio)
elif language == Language.CSHARP:
print(f"#region {var_name}", file=sio)
print(f"byte[] {var_name} = {{", file=sio)
lines = []
while True:
data = sin.read(byte_count)
if not data:
break
lines.append("\t" + ", ".join([f"0x{x:02X}" for x in data]) + ",")
lines[-1] = lines[-1].rstrip(",")
[print(x, file=sio) for x in lines]
print("};", file=sio)
print("#endregion", file=sio)
elif language in [Language.PHP, Language.PHP_NEW]: # using fast arrays
print(f"${var_name} = [", file=sio)
lines = []
while True:
data = sin.read(byte_count)
if not data:
break
lines.append("\t" + ", ".join([f"0x{x:02X}" for x in data]) + ",")
lines[-1] = lines[-1].rstrip(",")
[print(x, file=sio) for x in lines]
print("];", file=sio)
elif language == Language.PHP_OLD: # using slow arrays
print(f"${var_name} = array(", file=sio)
lines = []
while True:
data = sin.read(byte_count)
if not data:
break
lines.append("\t" + ", ".join([f"0x{x:02X}" for x in data]) + ",")
lines[-1] = lines[-1].rstrip(",")
[print(x, file=sio) for x in lines]
print(");", file=sio)
data = sio.getvalue()
# close input stream
sin.close()
if type(dout) == str:
Path(dout).write_text(data)
elif type(dout) == TextIO:
dout.write(data)
else:
print(data)
return data
def main() -> None:
parser = ArgumentParser(description="A script to make embedding binaries in code a breeze")
parser.add_argument("input", type=str, help="The binary to embed")
parser.add_argument("output", type=str, help="A file to write the output to")
parser.add_argument("-l", "--language", type=lowercase, default="python", help="The programming language to use")
parser.add_argument("-b", "--bytes", type=int, default=16, help="The number of bytes per line")
parser.add_argument("-v", "--variable", type=str, default="output", help="The name of the variable")
args = parser.parse_args()
assert isfile(args.input), "The specified input file doesn't exist"
lang: Language = None
if args.language in ["python", "py"]:
lang = Language.PYTHON
elif args.language == "c":
lang = Language.C
elif args.language in ["c++", "cpp", "cplusplus"]:
lang = Language.CPLUSPLUS
elif args.language in ["csharp", "cs", "c#"]:
lang = Language.CSHARP
elif args.language in ["php", "php-new"]:
lang = Language.PHP
elif args.language == "php-old":
lang = Language.PHP_OLD
else:
raise Exception("Invalid language specified!")
print(lang_format(args.input, args.output, lang, args.variable, args.bytes))
if __name__ == "__main__":
main()
# exports
__all__ = ["Language", "lang_format"]