-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshcap.py
executable file
·162 lines (131 loc) · 4.36 KB
/
sshcap.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
import json
import logging
import socket
import sys
from datetime import datetime, timezone
from threading import Thread
from typing import Callable, Tuple, TypedDict
import click
from paramiko import ServerInterface
from paramiko.common import AUTH_FAILED
from paramiko.rsakey import RSAKey
from paramiko.transport import Transport
logger = logging.getLogger(__name__)
_DEFAULT_BANNER = "SSH-2.0-OpenSSH_8.2p1 Ubuntu-4"
class CapturedCredential(TypedDict):
timestamp: str
src_ip: str
src_port: int
username: str
password: str
CredentialCallback = Callable[[CapturedCredential], None]
class JSONLogger:
def __init__(self, log_file: str) -> None:
self._log_file = log_file
def __call__(self, credential: CapturedCredential):
line = json.dumps(credential)
with open(self._log_file, "a") as f:
f.write(f"{line}\n")
class CaptureServer(ServerInterface):
def __init__(
self, banner: str, src_ip: str, src_port, callback: CredentialCallback
) -> None:
self._banner = banner
self._src_ip = src_ip
self._src_port = src_port
self._callback = callback
def get_allowed_auths(self, username: str) -> str:
return "password"
def check_auth_password(self, username: str, password: str) -> int:
logger.info(
f"Captured credential for {username} from {self._src_ip}:{self._src_port}"
)
capture = CapturedCredential(
username=username,
password=password,
src_ip=self._src_ip,
src_port=self._src_port,
timestamp=datetime.now(tz=timezone.utc).isoformat(),
)
self._callback(capture)
return AUTH_FAILED
def get_banner(self) -> Tuple[str, str]:
return (self._banner, "en-US")
class ClientHandler(Thread):
def __init__(
self,
server: ServerInterface,
socket: socket.socket,
addr: Tuple[str, int],
banner: str,
host_key: RSAKey,
) -> None:
super().__init__()
self._server = server
self._socket = socket
self._addr = addr
self._banner = banner
self._host_key = host_key
def run(self) -> None:
try:
with Transport(self._socket) as t:
t.add_server_key(self._host_key)
t.local_version = self._banner
t.start_server(server=self._server)
t.accept(30)
except Exception:
pass
finally:
logger.info(f"Connection closed to {self._addr[0]}:{self._addr[1]}")
@click.command()
@click.option("--banner", default=_DEFAULT_BANNER, help="SSH banner to advertise")
@click.option("--port", default=22, help="Port to listen on")
@click.option(
"--log-file", default="sshcap.log", help="File to log captured auth attempts"
)
@click.option("--server-key", default="sshcap.key", help="SSH server key file")
def main(banner: str, port: int, log_file: str, server_key: str):
host_key = RSAKey(filename=server_key)
callback = JSONLogger(log_file)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", port))
except Exception:
logger.exception("Failed to bind to socket")
sys.exit(1)
logger.info(f"Bound to port {port}")
sock.listen(100)
while True:
try:
client, addr = sock.accept()
except Exception:
logger.exception("Error while waiting for client")
sys.exit(1)
logger.info(f"Connection established to {addr}")
server = CaptureServer(
banner=banner,
src_ip=addr[0],
src_port=addr[1],
callback=callback,
)
handler_thread = ClientHandler(
server=server,
socket=client,
addr=addr,
banner=banner,
host_key=host_key,
)
handler_thread.start()
if __name__ == "__main__":
root = logging.getLogger()
root.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
handler.setFormatter(formatter)
root.addHandler(handler)
try:
main()
except KeyboardInterrupt:
sys.exit(0)