forked from GDGVIT/ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
93 lines (81 loc) · 2.76 KB
/
receiver.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
__author__ = 'Ujjwal'
import subprocess
import os
import signal
import socket
from SSHconfigure import sshConfig
try:
__USER__ = os.environ.copy()['SUDO_USER']
if __USER__ == 'root':
__USER__ = '../root'
except:
print('Run as sudo')
exit(0)
FNULL = open(os.devnull, 'w')
def socket_create():
try:
global host
global port
global s
host = ''
port = 9999
s = socket.socket()
except socket.error:
print("Socket creation error")
def socket_bind():
try:
global host
global port
global s
s.bind((host, port))
s.listen(5)
except socket.error:
print("Socket binding error")
exit(0)
def recvfile():
if ('sshd' not in str(subprocess.check_output(['ls', '/usr/sbin']))):
sshConfig()
if ('arcfour' not in str(subprocess.check_output(['cat', '/etc/ssh/sshd_config']))):
sshConfig()
sshServ = subprocess.Popen(['sudo', '/usr/sbin/sshd', '-p', '2222', '-D'], preexec_fn=os.setsid)
socket_create()
socket_bind()
print('Waiting...')
try:
conn, address = s.accept()
except (KeyboardInterrupt, EOFError):
print(' Keyboard Interrupt')
os.killpg(os.getpgid(sshServ.pid), signal.SIGTERM)
return
print(str(conn.recv(1024), encoding='utf-8'))
confirmation = input("Do you want to accept the connection? ")
if confirmation == 'y' or confirmation == 'Y':
conn.send(str.encode("Yes"))
else:
conn.close()
os.killpg(os.getpgid(sshServ.pid), signal.SIGTERM)
return
public_key = str(conn.recv(2048), encoding='utf-8')
if (subprocess.call(['cp', '/home/' + __USER__ + '/.ssh/authorized_keys', '/home/' + __USER__ + '/.ssh/authorized_keys_Backup'], stdout=FNULL,
stderr=subprocess.STDOUT)):
subprocess.call(['touch', '/home/' + __USER__ + '/.ssh/authorized_keys_Backup'])
with open('/home/' + __USER__ + '/.ssh/authorized_keys', 'a') as f:
f.write(public_key)
try:
conn.send(str.encode(__USER__.lstrip('../')))
conn.recv(256)
conn.close()
os.remove('/home/' + __USER__ + '/.ssh/authorized_keys')
os.rename('/home/' + __USER__ + '/.ssh/authorized_keys_Backup', '/home/' + __USER__ + '/.ssh/authorized_keys')
os.killpg(os.getpgid(sshServ.pid), signal.SIGTERM)
except:
try:
conn.close()
except:
pass
os.remove('/home/' + __USER__ + '/.ssh/authorized_keys')
os.rename('/home/' + __USER__ + '/.ssh/authorized_keys_Backup', '/home/' + __USER__ + '/.ssh/authorized_keys')
os.killpg(os.getpgid(sshServ.pid), signal.SIGTERM)
print('Error encountered')
if __name__ == '__main__':
recvfile()