forked from commixproject/commix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·262 lines (246 loc) · 9.76 KB
/
update.py
File metadata and controls
executable file
·262 lines (246 loc) · 9.76 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
# encoding: UTF-8
"""
This file is part of Commix Project (https://commixproject.com).
Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
For more see the file 'readme/COPYING' for copying permission.
"""
import re
import os
import sys
import time
import subprocess
from src.utils import menu
from src.utils import settings
from src.utils import requirments
from src.thirdparty.six.moves import input as _input
from src.thirdparty.colorama import Fore, Back, Style, init
"""
Check for updates (apply if any) and exit!
"""
"""
Returns abbreviated commit hash number as retrieved with "git rev-parse --short HEAD"
"""
def revision_num():
try:
start = 0
end = 0
start = time.time()
process = subprocess.Popen("git reset --hard HEAD && git clean -fd && git pull", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, _ = process.communicate()
if settings.VERBOSITY_LEVEL == 0:
info_msg = ('Updated to', 'Already at')["Already" in stdout]
process = subprocess.Popen("git rev-parse --verify HEAD", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Delete *.pyc files.
subprocess.Popen("find . -name \"*.pyc\" -delete", shell=True).wait()
# Delete empty directories and files.
subprocess.Popen("find . -empty -type d -delete", shell=True).wait()
if settings.VERBOSITY_LEVEL == 0:
stdout, _ = process.communicate()
match = re.search(r"(?i)[0-9a-f]{32}", stdout or "")
rev_num = match.group(0) if match else None
info_msg += " the latest revision '" + str(rev_num[:7]) + "'."
print(settings.SINGLE_WHITESPACE)
else:
sys.stdout.write(Fore.MAGENTA + "\n" + stdout + Style.RESET_ALL)
end = time.time()
how_long = int(end - start)
info_msg = "Finished in " + time.strftime('%H:%M:%S', time.gmtime(how_long)) + "."
print(settings.print_info_msg(info_msg))
except:
print(settings.SINGLE_WHITESPACE)
raise SystemExit()
"""
The commix's updater.
"""
def updater():
time.sleep(1)
info_msg = "Checking requirements to update "
info_msg += settings.APPLICATION + " from GitHub repository. "
sys.stdout.write(settings.print_info_msg(info_msg))
sys.stdout.flush()
if menu.options.offline:
print(settings.SINGLE_WHITESPACE)
err_msg = "You cannot update commix via GitHub without access on the Internet."
print(settings.print_critical_msg(err_msg))
raise SystemExit()
# Check if windows
if settings.IS_WINDOWS:
print(settings.SINGLE_WHITESPACE)
err_msg = "For updating purposes on Windows platform, it's recommended "
err_msg += "to use a GitHub client for Windows (http://windows.github.com/)."
print(settings.print_critical_msg(err_msg))
raise SystemExit()
else:
try:
requirment = "git"
# Check if 'git' is installed.
requirments.do_check(requirment)
if requirments.do_check(requirment) == True :
if settings.VERBOSITY_LEVEL != 0:
debug_msg = "commix will try to update itself using '" + requirment + "' command."
print(settings.print_debug_msg(debug_msg))
# Check if ".git" exists!
if os.path.isdir("./.git"):
sys.stdout.write(settings.SUCCESS_STATUS + "\n")
sys.stdout.flush()
info_msg = "Updating " + settings.APPLICATION + " to the latest (dev) "
info_msg += "version. "
sys.stdout.write(settings.print_info_msg(info_msg))
sys.stdout.flush()
revision_num()
print(settings.SINGLE_WHITESPACE)
os._exit(0)
else:
print(settings.SINGLE_WHITESPACE)
err_msg = "The '.git' directory not found. Do it manually: "
err_msg += Style.BRIGHT + "'git clone " + settings.GIT_URL
err_msg += " " + settings.APPLICATION + "' "
print(settings.print_critical_msg(err_msg))
raise SystemExit()
else:
print(settings.SINGLE_WHITESPACE)
err_msg = requirment + " not found."
print(settings.print_critical_msg(err_msg))
raise SystemExit()
except Exception as err_msg:
print("\n" + settings.print_critical_msg(err_msg))
raise SystemExit()
"""
Check for new version of commix
"""
def check_for_update():
try:
response = _urllib.request.urlopen('https://raw.githubusercontent.com/commixproject/commix/master/src/utils/settings.py', timeout=settings.TIMEOUT)
version_check = response.readlines()
for line in version_check:
line = line.rstrip()
if "VERSION_NUM = " in line:
update_version = line.replace("VERSION_NUM = ", "").replace("\"", "")
break
if (int(settings.VERSION_NUM.replace(".","")[:2]) < int(update_version.replace(".","")[:2])) or \
((int(settings.VERSION_NUM.replace(".","")[:2]) == int(update_version.replace(".","")[:2])) and \
int(settings.VERSION_NUM.replace(".","")[2:]) < int(update_version.replace(".","")[2:])):
while True:
if not menu.options.batch:
question_msg = "Do you want to update to the latest version now? [Y/n] > "
do_update = _input(settings.print_question_msg(question_msg))
else:
do_update = ""
if len(do_update) == 0:
do_update = "Y"
if do_update in settings.CHOICE_YES:
updater()
elif do_update in settings.CHOICE_NO:
break
else:
err_msg = "'" + do_update + "' is not a valid answer."
print(settings.print_error_msg(err_msg))
pass
except KeyboardInterrupt:
raise
except:
pass
"""
The updater for the unicorn tool
"""
def unicorn_updater(current_version):
APPLICATION_NAME = "TrustedSec's Magic Unicorn"
info_msg = "Checking requirements to update "
info_msg += APPLICATION_NAME + " from GitHub repository. "
sys.stdout.write(settings.print_info_msg(info_msg))
sys.stdout.flush()
if menu.options.offline:
print(settings.SINGLE_WHITESPACE)
err_msg = "You cannot update TrustedSec's Magic Unicorn "
err_msg += "via GitHub without access on the Internet."
print(settings.print_critical_msg(err_msg))
raise SystemExit()
# Check if windows
if settings.IS_WINDOWS:
print(settings.SINGLE_WHITESPACE)
err_msg = "For updating purposes on Windows platform, it's recommended "
err_msg += "to use a GitHub client for Windows (http://windows.github.com/)."
print(settings.print_critical_msg(err_msg))
raise SystemExit()
else:
try:
requirment = "git"
# Check if 'git' is installed.
requirments.do_check(requirment)
if requirments.do_check(requirment) == True :
sys.stdout.write(settings.SUCCESS_STATUS + "\n")
sys.stdout.flush()
if len(current_version) == 0:
unicorn_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../', 'thirdparty/'))
os.chdir(unicorn_path)
else:
os.chdir("../")
subprocess.Popen("rm -rf unicorn", shell=True).wait()
info_msg = "Updating " + APPLICATION_NAME + " to the latest (dev) "
info_msg += "version. "
subprocess.Popen("git clone https://github.com/trustedsec/unicorn", shell=True).wait()
os.chdir("unicorn")
sys.stdout.write(settings.print_info_msg(info_msg))
sys.stdout.flush()
revision_num()
else:
print(settings.SINGLE_WHITESPACE)
err_msg = requirment + " not found."
print(settings.print_critical_msg(err_msg))
raise SystemExit()
except Exception as err_msg:
print(settings.print_critical_msg(err_msg))
raise SystemExit()
"""
Check the latest version of unicorn
"""
def check_unicorn_version(current_version):
try:
if len(current_version) != 0:
response = _urllib.request.urlopen('https://raw.githubusercontent.com/trustedsec/unicorn/master/unicorn.py', timeout=settings.TIMEOUT)
latest_version = response.readlines()
for line in latest_version:
line = line.rstrip()
if "Magic Unicorn Attack Vector v" in line:
latest_version = line.replace("Magic Unicorn Attack Vector v", "").replace(" ", "").replace("-","").replace("\"","").replace(")","")
break
if len(current_version) == 0 or \
(int(current_version.replace(".","")[:2]) < int(latest_version.replace(".","")[:2])) or \
((int(current_version.replace(".","")[:2]) == int(latest_version.replace(".","")[:2])) and \
int(current_version.replace(".","")[2:]) < int(latest_version.replace(".","")[2:])):
if len(current_version) != 0:
warn_msg = "Current version of TrustedSec's Magic Unicorn (" + current_version + ") seems to be out-of-date."
print(settings.print_warning_msg(warn_msg))
else:
warn_msg = "TrustedSec's Magic Unicorn seems to be not installed."
print(settings.print_warning_msg(warn_msg))
while True:
if not menu.options.batch:
if len(current_version) == 0:
action = "install"
else:
action = "update to"
question_msg = "Do you want to " + action + " the latest version now? [Y/n] > "
do_update = _input(settings.print_question_msg(question_msg))
else:
do_update = ""
if len(do_update) == 0:
do_update = "Y"
if do_update in settings.CHOICE_YES:
unicorn_updater(current_version)
elif do_update in settings.CHOICE_NO:
break
else:
err_msg = "'" + do_update + "' is not a valid answer."
print(settings.print_error_msg(err_msg))
pass
except KeyboardInterrupt:
raise
except:
pass
# eof