-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
356 lines (268 loc) · 11.5 KB
/
test.py
File metadata and controls
356 lines (268 loc) · 11.5 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import subprocess
import os.path
subprocess.call("cp /etc/pam.d/common-password /etc/pam.d/common-password-backup".split())
subprocess.call("cp /etc/pam.d/common-auth /etc/pam.d/common-auth-backup".split())
subprocess.call("cp /etc/login.defs /etc/login_backup.defs".split())
print('read both files')
common_pass = open("/etc/pam.d/common-password","r+")
common_auth = open("/etc/pam.d/common-auth","r+")
login = open("/etc/login.defs","r+")
text = common_pass.read().strip("\n").split("\n")
print('remove potentially offending lines')
for i in range(len(text)):
line = text[i]
if ("password" in line) == True:
text[i] = ""
text.append("password [success=1 default=ignore] pam_unix.so obscure use_authtok sha512 shadow remeber=5")
text.append("password requisite pam_cracklib.so retry=3 minlen=8 difok=3 reject_username minclass=3 maxrepeat=2 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1")
text.append("password requisite pam_pwhistory.so use_authtok remember=24 enforce_for_root")
text = '\n'.join([str(x) for x in text])
common_pass.seek(0)
common_pass.write(text)
common_pass.truncate()
common_pass.close()
print('EDITING common-auth')
text = common_auth.read().strip("\n").split("\n")
text.append("auth required pam_tally.so deny=5 unlock_time=900 onerr=fail audit even_deny_root_account silent")
text = "\n".join([str(x) for x in text])
common_auth.seek(0)
common_auth.write(text)
common_auth.truncate()
common_auth.close()
print('EDITING login')
text = login.read().strip("\n").split("\n")
for i in range(len(text)):
line = text[i]
if ("PASS_MIN_DAYS" in line) == True:
text[i] = "PASS_MIN_DAYS 10"
elif ("PASS_WARN_AGE" in line) == True:
text[i] = "PASS_WARN_AGE 7"
elif ("PASS_MAX_DAYS" in line) == True:
text[i] = "PASS_MAX_DAYS 90"
text = "\n".join([str(x) for x in text])
login.seek(0)
login.write(text)
login.truncate()
login.close()
print('LOCK DOWN ON /etc/shadow')
subprocess.call("chmod o-r /etc/shadow".split())
subprocess.call("apt-get update -y".split())
subprocess.call("apt-get upgrade -y".split())
subprocess.call("apt-get autoremove -y".split())
subprocess.call("apt-get autoclean -y".split())
important_services = ["openssh-server", "samba", "telnetd"]
print("basic updates done! downloading tools!")
subprocess.call("apt-get install unattended-upgrades -y".split())
subprocess.call("dpkg-reconfigure -plow unattended-upgrades".split())
tools_array = ["libpam-cracklib", "nmap", "gufw", "rkhunter", "chkrootkit", "auditd", "clamtk"]
tools = "apt-get install " + ' '.join([str(x) for x in tools_array]) + " -y"
subprocess.call(tools.split())
print("tools downloaded! updating firefox!")
subprocess.call("killall firefox".split())
subprocess.call("apt-get remove firefox -y".split())
subprocess.call("apt-get install firefox -y".split())
subprocess.call("software-properties-gtk".split())
subprocess.call("gufw")
subprocess.call("chkrootkit")
subprocess.call("auditctl -e 1".split())
subprocess.call("freshclam".split())
#UPDATE DIST
print("updating dist....this may take a while")
subprocess.call("apt-get dist-upgrade -y".split())
#/etc/security/access.conf -:root: ALL EXCEPT LOCAL
if os.path.exists("/etc/security/access.conf") == True:
with open("/etc/security/access.conf", "a") as myfile:
myfile.write("\n-:root: ALL EXCEPT LOCAL")
print("access.conf edited!")
else:
print("/etc/security/access.conf not found!")
if os.path.exists("/etc/lightdm/lightdm.conf") == True:
with open("/etc/lightdm/lightdm.conf", "a") as myfile:
myfile.write("\nallow-guest=false")
print("guest locked!")
else:
print("/etc/lightdm/lightdm.conf does not exist")
subprocess.call("crontab -l".split())
p = subprocess.Popen("ls /etc/cron*", stdout=subprocess.PIPE, shell=True)
out,erro = p.communicate()
print (out)
p = subprocess.Popen("cat /etc/rc.local".split(), stdout=subprocess.PIPE)
output, err = p.communicate()
text = output
for line in text:
if len(line) > 0 and line.strip(" ")[0] != "#" and line.strip(" ") != "exit 0" and line.strip(" ") != "":
print("something is in /etc/rc.local ... you should check it out")
if os.path.exists("/etc/security/access.conf") == True:
with open("/etc/security/access.conf", "a") as myfile:
myfile.write("\n-:root: ALL EXCEPT LOCAL")
print("access.conf edited!")
else:
print("/etc/security/access.conf not found!")
if os.path.exists("/etc/lightdm/lightdm.conf") == True:
with open("/etc/lightdm/lightdm.conf", "a") as myfile:
myfile.write("\nallow-guest=false")
print("guest locked!")
else:
print("/etc/lightdm/lightdm.conf does not exist")
def remove_malicous_software(malwares):
attack = "apt-get purge " + ' '.join([str(x) for x in malwares]) + " -y"
subprocess.call(attack.split())
def bad(terms, directory):
for term in terms:
print("Searching for '{}'\n".format(term))
found = subprocess.Popen(str("sudo find {} -iname {}".format(directory, term)).split(), stdout = subprocess.PIPE).stdout.readlines()
if found != []:
print("'{}' found in the following locations:\n".format(term))
for item in found:
print(item)
print("\n")
attack_array = ["at","netcat","wireshark","sipcrack","sucrack","john","hydra","ophcrack","tcpdump","whoopsie","at","gnome-games-common","gbrainy"]
remove_malicous_software(attack_array);
subprocess.call("apt-get autoremove -y".split())
subprocess.call("apt-get autoclean -y".split())
not_allowed = ["john","dsniff","tcpdump","*hydra*","*kismet*","*netcat*","*wireshark*","*crack*","*hack*","*keylog*","*malware*","*sniff*", "*jp*g","*JP*G","*.mp3","*.mp4","*.tiff"]
for item in not_allowed:
bad([item], "/home")
subprocess.call("sysctl kernel.randomize_va_space=1".split())
subprocess.call("sysctl net.ipv4.conf.all.rp_filter=1".split())
subprocess.call("sysctl net.ipv4.conf.all.accept_source_route=0".split())
subprocess.call("sysctl net.ipv4.icmp_echo_ignore_broadcasts=1".split())
subprocess.call("sysctl net.ipv4.conf.all.log_martians=1".split())
subprocess.call("sysctl net.ipv4.conf.default.log_martians=1".split())
subprocess.call("sysctl -w net.ipv4.conf.all.accept_redirects=0".split())
subprocess.call("sysctl -w net.ipv6.conf.all.accept_redirects=0".split())
subprocess.call("sysctl -w net.ipv4.conf.all.send_redirects=0".split())
subprocess.call("sysctl kernel.sysrq=0".split())
subprocess.call("sysctl net.ipv4.tcp_timestamps=0".split())
subprocess.call("sysctl net.ipv4.tcp_syncookies=1".split())
subprocess.call("sysctl net.ipv4.icmp_ignore_bogus_error_responses=1".split())
subprocess.call("sysctl -p".split())
print("done!")
p = subprocess.Popen("lsb_release -r", stdout=subprocess.PIPE, shell=True)
out,erro = p.communicate()
p = subprocess.Popen("dpkg --get-selections | grep -v deinstall", stdout=subprocess.PIPE, shell=True)
out,erro = p.communicate()
installed = out
proc = subprocess.Popen("rm -rf /home/*/.ssh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, erro = proc.communicate()
subprocess.call("cp /etc/ssh/sshd_config /etc/ssh/sshd_config_backup".split())
if os.path.exists("/etc/ssh/sshd_config") == True:
file = open("/etc/ssh/sshd_config","r+")
text = file.read().strip("\n").split("\n")
for i in range(len(text)):
line = text[i]
if ("PermitEmptyPasswords" in line) == True:
text[i] = ""
if ("PermitRootLogin" in line) == True:
text[i] = ""
if ("UsePrivilegeSeparation" in line) == True:
text[i] = ""
if ("SyslogFacility" in line) == True:
text[i] = ""
if ("LogLevel" in line) == True:
text[i] = ""
if ("LoginGraceTime" in line) == True:
text[i] = ""
if ("StrictModes" in line) == True:
text[i] = ""
if ("ChallengeResponseAuthentication" in line) == True:
text[i] = ""
if ("UsePAM" in line) == True:
text[i] = ""
if ("Protocol" in line) == True:
text[i] = ""
if ("DebianBanner" in line) == True:
text[i] = ""
if ("X11Forwarding" in line) == True:
text[i] = ""
if ("Protocol 2" in line) == True:
text[i] = ""
text.append("PermitEmptyPasswords no")
text.append("PermitRootLogin no")
text.append("UsePrivilegeSeparation yes")
text.append("SyslogFacility AUTH")
text.append("LogLevel INFO")
text.append("LoginGraceTime 20")
text.append("StrictModes yes")
text.append("ChallengeResponseAuthentication yes")
text.append("UsePAM yes")
text.append("Protocol 2")
text.append("DebianBanner no")
text.append("X11Forwarding no")
text.append("Protocol 1")
text = '\n'.join([str(x) for x in text])
file.seek(0)
file.write(text)
file.truncate()
file.close()
subprocess.call("sudo /etc/init.d/ssh restart".split())
else:
print("/etc/ssh/sshd_config does not exist")
subprocess.call("ufw allow ssh".split())
subprocess.call("ufw allow http".split())
subprocess.call("ufw deny 23".split())
subprocess.call("ufw default deny".split())
subprocess.call("ufw enable".split())
subprocess.call("ufw limit OpenSSH".split())
subprocess.call("cp /etc/sysctl.conf /etc/sysctl_conf_backup".split())
if os.path.exists("/etc/sysctl.conf") == True:
file = open("/etc/sysctl.conf","r+")
text = file.read().strip("\n").split("\n")
text.append("net.ipv4.tcp_syncookies = 1")
text = '\n'.join([str(x) for x in text])
file.seek(0)
file.write(text)
file.truncate()
file.close()
subprocess.call("sysctl -p".split())
else:
print("/etc/sysctl.conf does not exist")
subprocess.call("sudo apt-get install vsftpd -y".split())
subprocess.call("cp /etc/vsftpd.conf /etc/vsftpd_backup.conf".split())
if os.path.exists("/etc/vsftpd.conf") == True:
file = open("/etc/vsftpd.conf","r+")
text = file.read().strip("\n").split("\n")
for i in range(len(text)):
line = text[i]
if ("anonymous_enable" in line) == True:
text[i] = ""
if ("local_enable" in line) == True:
text[i] = ""
if ("write_enable" in line) == True:
text[i] = ""
if ("chroot_local_user" in line) == True:
text[i] = ""
text.append("anonymous_enable=NO")
text.append("local_enable=YES")
text.append("write_enable=YES ")
text.append("chroot_local_user=YES")
text = '\n'.join([str(x) for x in text])
file.seek(0)
file.write(text)
file.truncate()
file.close()
subprocess.call("sudo /etc/init.d/vsftpd restart".split())
else:
print("/etc/vsftpd.conf does not exist!")
append_only = [".bash_history",".bash_profile",".bash_login",".profile",".bash_logout",".bashrc"]
for appends in append_only:
subprocess.call(("chattr +a ~/" + appends).split()) #set to append only
#if os.path.exists("~/.bashrc") == False:
# subprocess.call("touch ~/.bashrc".split())
#text = file.read().strip("\n").split("\n")
#text.append("shopt -s histappend")
#text.append('readonly PROMPT_COMMAND="history -a" ')
#text.append("readonly HISTFILE")
#text.append("readonly HISTFILESIZE")
#text.append("readonly HISTSIZE")
#text.append("readonly HISTCMD")
#text.append("readonly HISTCONTROL")
#text.append("readonly HISTIGNORE")
text = '\n'.join([str(x) for x in text])
#file.seek(0)
#file.write(text)
#file.truncate()
#file.close()
subprocess.call("chmod 750 csh".split())
subprocess.call("chmod 750 tcsh ".split())
subprocess.call("chmod 750 ksh".split())