-
Notifications
You must be signed in to change notification settings - Fork 18
/
ssh_server.py
65 lines (57 loc) · 1.9 KB
/
ssh_server.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
import os
import paramiko
import socket
import sys
import threading
CWD = os.path.dirname(os.path.realpath(__file__))
HOSTKEY = paramiko.RSAKey(filename=os.path.join(CWD, '.test_rsa.key'))
class Server (paramiko.ServerInterface):
def __init__(self):
self.event = threading.Event()
def check_channel_request(self, kind, chanid):
if kind == 'session':
return paramiko.OPEN_SUCCEEDED
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
def check_auth_password(self, username, password):
if (username == 'tim') and (password == 'sekret'):
return paramiko.AUTH_SUCCESSFUL
if __name__ == '__main__':
server = '192.168.1.103'
ssh_port = 2222
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((server, ssh_port))
sock.listen(100)
print('[+] Listening for connection ...')
client, addr = sock.accept()
except Exception as e:
print('[-] Listen failed: ' + str(e))
sys.exit(1)
else:
print(f'[+] Got a connection! from {addr}')
bhSession = paramiko.Transport(client)
bhSession.add_server_key(HOSTKEY)
server = Server()
bhSession.start_server(server=server)
chan = bhSession.accept(20)
if chan is None:
print('*** No channel.')
sys.exit(1)
print('[+] Authenticated!')
print(chan.recv(1024).decode())
chan.send('Welcome to bh_ssh')
try:
while True:
command = input("Enter command: ")
if command != 'exit':
chan.send(command)
r = chan.recv(8192)
print(r.decode())
else:
chan.send('exit')
print('exiting')
bhSession.close()
break
except KeyboardInterrupt:
bhSession.close()