-
Notifications
You must be signed in to change notification settings - Fork 4
/
runtests.py
executable file
·150 lines (112 loc) · 3.88 KB
/
runtests.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
#!/usr/bin/env python
import json
import logging
import os
import socket
import sys
from contextlib import suppress
from threading import Thread
import paramiko
import blacknet.console # noqa: F401
from blacknet.master import BlacknetMasterServer
from blacknet.scrubber import BlacknetScrubber
from blacknet.sensor import BlacknetSensor
from blacknet.server import BlacknetServer # noqa: F401
from blacknet.updater import BlacknetGeoUpdater
SCRUBBER_STATS_FILE = "tests/generated/stats_general.json"
HONEYPOT_CONFIG_FILE = "tests/blacknet-honeypot.cfg"
MASTER_CONFIG_FILE = "tests/blacknet.cfg"
CLIENT_SSH_KEY = "tests/ssh_key"
def runtests_ssh_serve(bns: BlacknetSensor) -> None:
"""Thread entry point, runs the sensor."""
bns.do_ping()
bns.serve()
def runtests_main_serve(bns: BlacknetMasterServer) -> None:
"""Thread entry point, runs the master server."""
bns.serve()
def runtests_ssh_client() -> None:
"""Simulate a real SSH client trying to authenticate."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 2200))
t = paramiko.Transport(sock)
t.start_client()
with suppress(BaseException):
ssh_key = paramiko.RSAKey(filename=CLIENT_SSH_KEY)
t.auth_publickey("blacknet", ssh_key)
for suffix in ["0", "1", "a", "b", "c", "é", "&", "L", ")", "€", "\xfe", "\xa8"]:
with suppress(Exception):
password = "password_%s" % suffix
t.auth_password("blacknet", password)
t.close()
def runtests_update() -> None:
"""Update database geolocation from local samples."""
bnu = BlacknetGeoUpdater(MASTER_CONFIG_FILE)
bnu.update()
def runtests_scrubber() -> None:
"""Run the database scrubber."""
bns = BlacknetScrubber(MASTER_CONFIG_FILE)
bns.verbosity = 2
bns.do_fix = True
bns.check_attackers()
for table in ["attacker", "session"]:
bns.check_attempts_count(table)
bns.check_attempts_dates(table)
bns.check_geolocations()
bns.database_optimize()
bns.generate_targets()
bns.generate_stats()
bns.generate_minimaps()
bns.generate_map_data()
def runtests_checker() -> bool:
"""Check that stat file is consistent."""
with open(SCRUBBER_STATS_FILE) as f:
d = json.load(f)
d = d["data"][0]
print(d)
return d[0] > 10
def runtests_ssh() -> None:
"""Run both servers and run a few login/passwd attempts."""
servers = [] # type: list[BlacknetServer]
threads = []
# Create master server instance
print("[+] Creating main server instance")
bn_main = BlacknetMasterServer(MASTER_CONFIG_FILE)
servers.append(bn_main)
# Create SSH sensor instance
print("[+] Creating SSH server instance")
bn_ssh = BlacknetSensor(cfg_file=HONEYPOT_CONFIG_FILE)
servers.append(bn_ssh)
# Check configuration reloading
for s in servers:
s.reload()
# Prepare to serve requests in separate threads
t = Thread(target=runtests_main_serve, args=(bn_main,))
threads.append(t)
t = Thread(target=runtests_ssh_serve, args=(bn_ssh,))
threads.append(t)
for t in threads:
t.daemon = True
t.start()
print("[+] Running SSH login attempts")
# Simulate a SSH client connecting
runtests_ssh_client()
# Close servers
bn_ssh.shutdown()
bn_main.shutdown()
print("[+] Closed servers")
if __name__ == "__main__":
# Log paramiko warnings to stdout.
logger = logging.getLogger("paramiko")
logger.setLevel(logging.WARNING)
logger.addHandler(logging.StreamHandler(sys.stdout))
# Update geolocation database with minimal sample
runtests_update()
# Main SSH Test
runtests_ssh()
# DB scrubber and cache generator
runtests_scrubber()
# Check number of attempts from database
success = runtests_checker()
if success:
sys.exit(os.EX_OK)
sys.exit(os.EX_SOFTWARE)