8000 Add chapter 1 py scripts · andreffs18/violent-python@da8527d · GitHub
[go: up one dir, main page]

Skip to content

Commit da8527d

Browse files
committed
Add chapter 1 py scripts
1 parent dc4bd84 commit da8527d

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

chapter1/network_scanner.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
import socket
4+
import optparse
5+
6+
7+
def get_banner(ip, port, timeout):
8+
"""
9+
Open socket connection to given ip:port and
10+
read first 1024 bytes from open socket connection
11+
"""
12+
socket.setdefaulttimeout(timeout)
13+
s = socket.socket()
14+
try:
15+
s.connect((ip, port))
16+
ans = s.recv(1024)
17+
return ans
18+
except Exception, e:
19+
print("[-] Error {}:{} = {}".format(ip, port, e))
20+
return None
21+
22+
def check_vulnerabilities(banner, filename):
23+
"""
24+
From given banner, check if from "filename" theres is any match
25+
"""
26+
with open(filename, 'r') as f:
27+
for line in f.readlines():
28+
if line.strip('\n') in banner:
29+
print "[+] Server is vulnerable: {}".format(banner.strip('\n'))
30+
31+
32+
if __name__ == '__main__':
33+
parser = optparse.OptionParser('usage %prog -n <network> -t <type of probe>')
34+
parser.add_option('-n', dest='network', type='string', default="192.168.1.X", help='specify network to search on (default: "192.168.0.X"')
35+
parser.add_option('--start_subnet', dest='start_subnet', type='int', default=1, help='specify which subnet should the scan start (default: "1"')
36+
parser.add_option('--end_subnet', dest='end_subnet', type='int', default=254, help='specify which subnet should the scan stop (default: "254"')
37+
# test telnet, ssh, smtp, http, imap and https ports
38+
parser.add_option('-p', dest='ports', type='string', default="21, 22, 25, 80, 110, 443", help='specify list of ports, separed by comma (default: "21, 22, 25, 80, 110, 443"')
39+
parser.add_option('--vul_filename', dest='vulnerabilities_filename', type='string', default="banners.txt", help='default file with list of vulnerabilities to compare (default: "banners.txt"')
40+
parser.add_option('--socket_timeout', dest='socket_timeout', type='int', default=2, help='default socket connection timeout (default: "2" seconds')
41+
(options, args) = parser.parse_args()
42+
43+
# generate list of all possible ip's on subnet 192.168.1.0/24
44+
subnet = options.network.lower()
45+
subnet_string = subnet.replace("x", "{}")
46+
ip_list = map(lambda ip: subnet_string.format(ip), range(options.start_subnet, options.end_subnet))
47+
# test ports telnet, ssh, smtp, http, imap and https
48+
port_list = map(int, filter(None, map(lambda p: p.strip(), options.ports.split(","))))
49+
50+
print("[*] Testing subnet of {} for {} ports: {}".format(subnet_string, len(port_list), options.ports))
51+
for ip in ip_list:
52+
for port in port_list:
53+
banner = get_banner(ip, port, timeout=options.socket_timeout)
54+
if banner:
55+
print("[+] Checking {}:{}".format(ip, port))
56+
check_vulnerabilities(banner, filename=options.vul_filename)
57+

chapter1/open_secure_zip.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
import zipfile
4+
import optparse
5+
from tqdm import tqdm
6+
from threading import Thread
7+
8+
9+
def extract_file(zfile, password):
10+
"""
11+
Try to extract files from secured zip file. Print password if that works
12+
"""
13+
try:
14+
zfile.extractall(pwd=password)
15+
print("[+] Found password \"{}\"".format(password))
16+
except:
17+
pass
18+
19+
20+
if __name__ == '__main__':
21+
parser = optparse.OptionParser('usage %prog --zipfile <secure zipfile> --test_passwords <file list of possible passwords>')
22+
parser.add_option('--zipfile', dest='zipfile', type='string', default="evil.zip", help='specify zip filename to crack (default: "evil.zip"')
23+
parser.add_option('--test_passwords', dest='test_passwords', type='string', default="dictionary.txt", help='specify file that contains list of possible passwords (default: "dictionary.txt"')
24+
(options, args) = parser.parse_args()
25+
26+
zfile = zipfile.ZipFile(options.zipfile)
27+
with open(options.test_passwords) as dictionary_file:
28+
for possible_password in tqdm(dictionary_file.readlines()):
29+
password = possible_password.strip()
30+
t = Thread(target=extract_file, args=(zfile, password))
31+
t.start()

chapter1/password_hash_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
import crypt
4+
import optparse
5+
from tqdm import tqdm
6+
7+
8+
def test_password(crypt_pass, dictionary_filename):
9+
"""
10+
From given cryptografic password we can find a match on our "database"
11+
"""
12+
salt = crypt_pass[:2]
13+
with open(dictionary_filename, 'r') as dictionary:
14+
for word in tqdm(dictionary.readlines()):
15+
word = word.strip()
16+
crypt_test = crypt.crypt(word, salt)
17+
if crypt_pass == crypt_test:
18+
print("[+] Found password: \"{}\"".format(word))
19+
return word
20+
print("[-] Password not found.")
21+
return
22+
23+
24+
if __name__ == '__main__':
25+
parser = optparse.OptionParser('usage %prog --unknown_passwords <file list of hashed passwords> --test_passwords <file list of possible passwords>')
26+
parser.add_option('--unknown_passwords', dest='unknown_passwords', type='string', default="passwords.txt", help='specify file that contains list of unknown hashed passwords (default: "passwords.txt"')
27+
28+
parser.add_option('--test_passwords', dest='test_passwords', type='string', default="dictionary.txt", help='specify file that contains list of possible passwords (default: "dictionary.txt"')
29+
(options, args) = parser.parse_args()
30+
31+
with open(options.unknown_passwords) as unknown_passwords:
32+
for line in unknown_passwords.readlines():
33+
if ":" in line:
34+
user = line.split(':')[0]
35+
crypt_pass = line.split(':')[1].strip(' ')
36+
print("[*] Cracking Password For: {}".format(user))
37+
test_password(crypt_pass, options.test_passwords)

0 commit comments

Comments
 (0)
0