-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
146 lines (102 loc) · 2.99 KB
/
utils.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
142
143
144
145
146
import subprocess
import time
import sys
_compressed = "last_flag"
_wl = None
_it = 0
_posix_tar = "POSIX tar archive (GNU)"
_bzip2 = "bzip2 compressed data"
_zip = "Zip archive data"
_gzip = "gzip compressed data"
_xz = "XZ compressed data"
_ascii = "ASCII"
def type(compressed):
type_str = shell_cmd_output("file " + compressed)
if _posix_tar in type_str:
return 0
elif _bzip2 in type_str:
return 1
elif _zip in type_str:
return 2
elif _gzip in type_str:
return 3
elif _xz in type_str:
return 4
elif _ascii in type_str:
return 5
else:
return 6
def shell_cmd(command):
subprocess.call(command.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def shell_cmd_process(command):
return subprocess.Popen(command.split(),
stdout=subprocess.PIPE)
def shell_cmd_output(command):
return subprocess.Popen(command.split(),
stdout=subprocess.PIPE).stdout.read().decode()
def shell_cmd_output_lines(command):
return subprocess.Popen(command.split(),
stdout=subprocess.PIPE).stdout\
.read().decode().split('\n')
def shell_cmd_raw(command):
subprocess.Popen(command, shell=True)
def position_first_flag(name):
shell_cmd("mv " + name + " " + _compressed)
def position_new_flag(content, _content):
# naive method,
# file that was not there when the script
# started is treated like potential compressed file
# _content is the dir at the start of the script
# content is the modified dir
found = 0
for _file in content:
if _file not in _content:
found = 1
if _file != "last_flag":
shell_cmd("mv " + _file + " last_flag")
break
if not found:
print("ERROR: DID NOT FIND THE FLAG FILE\n")
def has_wordlist():
global _wl
if "-w" in sys.argv:
_wl = parse_wordlist_name()
def parse_wordlist_name():
argc = len(sys.argv)
w_index = sys.argv.index('-w')
if w_index >= argc - 1:
print("Did you provide a wordlist?")
exit()
return sys.argv[w_index + 1]
def compressed_file():
return sys.argv[1]
def redefine_type(new_type):
shell_cmd("mv last_flag " + new_type)
def read_flag():
return shell_cmd_output("cat last_flag")
def print_flag(flag):
separator = '<' + '=' * (len(flag) - 2) + '>'
print(separator)
if "\n" in flag:
print(flag, end="")
else:
print(flag)
print(separator)
def print_ascii():
print("It looks like the flag :)")
flag = read_flag()
time.sleep(3)
print_flag(flag)
print(f"nº iterations needed: {_it}")
exit()
def print_unknown_type():
print("unknown format... exiting")
print("Take a look:")
redefine_type("unknown_file")
type_str = shell_cmd_output("file unknown_file")
print(f"{type_str[:-1]}")
exit()
def exit():
sys.exit()