-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhp-ssh-server.py
More file actions
81 lines (64 loc) · 2.49 KB
/
hp-ssh-server.py
File metadata and controls
81 lines (64 loc) · 2.49 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
#!/usr/bin/env python3
import socket, sys, threading, _thread
import time
import paramiko
#generate keys with 'ssh-keygen -t rsa -f server.key'
HOST_KEY = paramiko.RSAKey(filename='server.key')
SSH_PORT = 2222
LOGFILE = 'sshlogins.txt' #File to log the user:password combinations to
LOGFILE_LOCK = threading.Lock()
# Function to write a string to the logfile
def log(logstr):
LOGFILE_LOCK.acquire()
try:
logfile_handle = open(LOGFILE,"a")
logfile_handle.write( logstr )
logfile_handle.close()
finally:
LOGFILE_LOCK.release()
# Class to handle SSH requests
class SSHServerHandler (paramiko.ServerInterface):
# constructor
def __init__(self, logprefix):
self.event = threading.Event()
self.logprefix = logprefix
# log username and password and return AUTH_FAILED on any request
def check_auth_password(self, username, password):
log( self.logprefix + "\"" + username + "\";\"" + password + "\"\n" )
return paramiko.AUTH_FAILED
# return allowed authentication methods = password
def get_allowed_auths(self, username):
return 'password'
# Function to handle a new connection
def handleConnection(client, logprefix):
transport = paramiko.Transport(client)
transport.add_server_key(HOST_KEY)
# we use an old SSH banner from Ubuntu
transport.local_version = 'SSH-2.0-OpenSSH_7.7p1 Ubuntu-4'
server_handler = SSHServerHandler( logprefix )
transport.start_server(server=server_handler)
channel = transport.accept(1)
if not channel is None:
channel.close()
# Main loop for hp ssh server
def main():
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', SSH_PORT))
server_socket.listen(100)
paramiko.util.log_to_file ('paramiko.log', level= "WARN")
while(True):
try:
client_socket, client_addr = server_socket.accept()
ct = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime() )
logprefix = ct + ";" + str(client_addr[0]) + ";" + str(client_addr[1]) + ";"
_thread.start_new_thread(handleConnection,(client_socket, logprefix,))
except Exception as e:
print("ERROR: Client handling")
print(e)
except Exception as e:
print("ERROR: Failed to create socket")
print(e)
sys.exit(1)
main()