-
Notifications
You must be signed in to change notification settings - Fork 0
/
L07C01.py
51 lines (42 loc) · 1.26 KB
/
L07C01.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
#
# Find the valid png file in the /tmp directory using magic bytes.
# The code is hidden in this file.
#
import os
# Define the PNG magic bytes
png_magic_bytes = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
def is_valid_png(data):
"""
Checks if the provided data starts with the PNG magic bytes.
Args:
data: Bytes of the file to check.
Returns:
True if the data is a valid PNG, False otherwise.
"""
return data.startswith(png_magic_bytes)
def find_valid_png(directory):
"""
Searches for a valid PNG file in the provided directory.
Args:
directory: Path to the directory to search.
Returns:
Full path of the valid PNG file or None if no valid PNG is found.
"""
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path):
with open(file_path, "rb") as f:
data = f.read(len(png_magic_bytes))
if is_valid_png(data):
return file_path
return None
# Search for valid PNG file in /tmp directory
valid_png_path = find_valid_png("/tmp")
# Check if a valid PNG was found
if valid_png_path:
# Open the valid PNG file and display its contents
with open(valid_png_path, "rb") as f:
data = f.read()
print(data)
else:
print("No valid PNG file found in /tmp")