-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacropalypse.py
More file actions
70 lines (57 loc) · 1.97 KB
/
acropalypse.py
File metadata and controls
70 lines (57 loc) · 1.97 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
66
67
68
69
70
import struct
import sys
import requests
from urllib.parse import urlparse
# acropalypse.py - Check PNG files for trailing data - Usage:
# python acropalypse.py your_image.png
# python acropalypse.py https://example.com/your_image.png
def fetch_data(input_source):
if is_url(input_source):
return fetch_remote_data(input_source)
else:
return fetch_local_data(input_source)
def is_url(input_source):
try:
result = urlparse(input_source)
return result.scheme in ('http', 'https')
except ValueError:
return False
def fetch_local_data(filepath):
try:
with open(filepath, 'rb') as file:
data = file.read()
return data
except FileNotFoundError:
print(f"File not found: {filepath}")
return None
def fetch_remote_data(url):
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to fetch the PNG file from the URL: {url}")
return None
return response.content
def main(input_source):
data = fetch_data(input_source)
if not data:
return
png_signature = b'\x89PNG\r\n\x1a\n'
iend_chunk = b'IEND'
if not data.startswith(png_signature):
print(f"The file at {input_source} is not a valid PNG file.")
return
iend_index = data.find(iend_chunk)
if iend_index == -1:
print(f"The file at {input_source} is not a valid PNG file.")
return
iend_length = 4
crc_length = 4
iend_full_chunk_length = iend_length + crc_length
if len(data) > iend_index + iend_full_chunk_length:
print(f"The file at {input_source} has data beyond the IEND marker.")
else:
print(f"The file at {input_source} does not have data beyond the IEND marker.")
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <local_file_path_or_url_to_png_file>")
else:
main(sys.argv[1])