-
Notifications
You must be signed in to change notification settings - Fork 10
/
HP_Utility.py
318 lines (299 loc) · 11 KB
/
HP_Utility.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import sys
import subprocess
import stat
import platform
import time
import distro
from language import Language
# noinspection PyExceptClausesOrder
class Utility:
"""
Utility class holds all the working functions!
"""
@staticmethod
def is_root():
"""
if the current user has root privileges or not!
:return: boolean
"""
is_root = False
try:
if not os.path.exists('/etc/foo'):
os.makedirs('/etc/foo')
else:
os.rename('/etc/foo', '/etc/bar')
is_root = True
except IOError as e:
if 'Permission denied' in str(e):
print("You need root permissions to do this (O_o)")
Utility().terminate("Permission Denied!")
finally:
if os.path.exists('/etc/foo'):
os.rmdir('/etc/foo')
if os.path.exists('/etc/bar'):
os.rmdir('/etc/bar')
return is_root
@staticmethod
def system_upgrade(is_test=False):
"""
:param is_test -> if true, upgrade command will run in force yes mode.
run system update upgrade before doing anything.
:return:
"""
return_code = 200
error_message = ""
try:
file_status = os.stat('bash_scripts/updater.sh')
os.chmod('bash_scripts/updater.sh',
file_status.st_mode | stat.S_IEXEC)
result = subprocess.Popen(
['./bash_scripts/updater.sh', str(is_test)])
result.communicate()
return_code = result.returncode
del file_status
del result
except IOError as error:
return_code = 255
error_message = str(error)
except subprocess.CalledProcessError as error:
return_code = 255
error_message = str(error)
except OSError as error:
return_code = 255
error_message = str(error)
except KeyboardInterrupt as error:
return_code = 255
error_message = str(error)
finally:
if return_code != 0:
# error occurred in the process
Utility().terminate("Error Code --> " + str(return_code) + " " + error_message)
return return_code
@staticmethod
def install_psql(is_test=False):
"""
Install Postgresql and Metasploit Framework related stuff
:return: 0 on success
"""
return_code = 200
error_message = ""
try:
file_status = os.stat('bash_scripts/psql.sh')
os.chmod('bash_scripts/psql.sh',
file_status.st_mode | stat.S_IEXEC)
result = subprocess.Popen(['./bash_scripts/psql.sh', str(is_test)])
result.communicate()
return_code = result.returncode
del result
del file_status
except IOError as error:
return_code = 255
error_message = str(error)
except subprocess.CalledProcessError as error:
return_code = 255
error_message = str(error)
except OSError as error:
return_code = 255
error_message = str(error)
except KeyboardInterrupt as error:
return_code = 255
error_message = str(error)
finally:
if return_code != 0:
Utility().terminate("Error Code --> " + str(return_code) + " " + error_message)
return return_code
@staticmethod
def install_metasploit(is_test=False):
"""
installs metasploit framework in the system.
:param is_test: :bool
:return: :Integer | exit value
"""
return_code = 200
error_message = ""
try:
metasploit = os.stat('bash_scripts/metasploit.sh')
os.chmod('bash_scripts/metasploit.sh',
metasploit.st_mode | stat.S_IEXEC)
if distro.linux_distribution(False)[0] == 'kali':
result = subprocess.Popen(
['./bash_scripts/metasploit.sh', 'Kali', str(is_test)])
else:
result = subprocess.Popen(
['./bash_scripts/metasploit.sh', 'Linux', str(is_test)])
result.communicate()
return_code = result.returncode
del result
del metasploit
except IOError as error:
return_code = 255
error_message = str(error)
except subprocess.CalledProcessError as error:
return_code = 255
error_message = str(error)
except OSError as error:
return_code = 255
error_message = str(error)
except KeyboardInterrupt as error:
return_code = 255
error_message = str(error)
finally:
if return_code != 0:
Utility().terminate("Error Code --> " + str(return_code) + " " + error_message)
return return_code
@staticmethod
def chmod_scripts():
"""
Installing scripts and apps inside /opt/
:return:
"""
return_code = 0
error_message = ""
try:
file_status = os.stat('bash_scripts/install_scripts.sh')
os.chmod('bash_scripts/install_scripts.sh',
file_status.st_mode | stat.S_IEXEC)
del file_status
except IOError as error:
return_code = 255
error_message = str(error)
except subprocess.CalledProcessError as error:
return_code = 255
error_message = str(error)
except OSError as error:
return_code = 255
error_message = str(error)
except KeyboardInterrupt as error:
return_code = 255
error_message = str(error)
finally:
if return_code != 0:
Utility().terminate("Error Code --> " + str(return_code) + " " + error_message)
return return_code
@classmethod
def terminate(cls, message=None):
"""
force the python script before exiting
:param message: exit message
:return: none
"""
sys.exit(message)
@classmethod
def baby_step(cls, is_test=False):
"""
baby step takes care of the installing scripts
:param is_test: boolean
:return:
"""
return_code = 0
error_message = ""
language = Language()
first_time = language.main_menu_v2() # since there is no last item
print(first_time)
try:
while True:
user_input = input("Your option:: ")
if user_input == "14":
# terminate the loop
time.sleep(3)
print(language.line_apprx + '\n' + language.line_apprx
+ '\n' + language.line_thats_wrap + '\n'
+ language.line_apprx + '\n' + language.line_apprx)
sys.exit(0) # The END
elif user_input == "13":
# install all
return_code = Utility.__do_it_all(is_test)
if return_code != 0:
Utility().terminate("System terminated!")
else:
time.sleep(3)
print(language.line_apprx + '\n' + language.line_apprx
+ '\n' + language.line_thats_wrap + '\n'
+ language.line_apprx + '\n' + language.line_apprx)
else:
time.sleep(5)
result = subprocess.Popen(
['./bash_scripts/install_scripts.sh', str(is_test), user_input])
result.communicate()
return_code = result.returncode
del result
if return_code != 0:
Utility().terminate("System terminated!")
current_menu = language.main_menu_v2()
print(current_menu)
except subprocess.CalledProcessError as error:
return_code = 255
error_message = str(error)
except OSError as error:
return_code = 255
error_message = str(error)
except KeyboardInterrupt as error:
return_code = 255
error_message = str(error)
finally:
if return_code != 0:
Utility().terminate("System terminated! " + str(error_message))
@classmethod
def __do_it_all(cls, is_test=False):
"""
do it install all the scripts in a linear fashion.
:param is_test: bool
:return: integer (success = 0, failure = 255)
"""
return_code = 0
error_message = None
for user_input in range(1, 14):
try:
time.sleep(2)
result = subprocess.Popen(
['./bash_scripts/install_scripts.sh', str(is_test), str(user_input)])
result.communicate()
return_code = result.returncode
del result
if return_code != 0:
Utility().terminate("System terminated!")
except subprocess.CalledProcessError as error:
return_code = 255
error_message = str(error)
except OSError as error:
return_code = 255
error_message = str(error)
except KeyboardInterrupt as error:
return_code = 255
error_message = str(error)
finally:
if return_code != 0:
Utility().terminate("System terminated! " + str(error_message))
return return_code
def main():
language = Language()
util = Utility()
if util.is_root():
if platform.system() != 'Linux':
print(language.line_hashes + '\n' + language.line_hashes)
sys.stderr.write(language.line_non_linux_distro)
print(language.line_hashes + '\n' + language.line_hashes)
sys.exit(255)
if sys.version_info < (3, 0, 0):
print(language.line_hashes + '\n' + language.line_hashes)
sys.stderr.write(language.line_not_python3)
print(language.line_hashes + '\n' + language.line_hashes)
sys.exit(255)
if distro.linux_distribution(False)[0] != 'kali':
print(language.line_apprx + '\n' +
language.line_not_kali + '\n' + language.line_apprx)
time.sleep(5)
# call the utility functions
util.system_upgrade()
util.install_psql()
util.install_metasploit()
util.chmod_scripts()
util.baby_step()
else:
print(language.line_hashes + '\n' + language.line_hashes +
'\n' + language.line_non_superuser)
sys.exit("Permission Denied!\n{}\n{}".format(
language.line_hashes, language.line_hashes))
if __name__ == '__main__':
main()