Last active
December 30, 2019 05:55
-
-
Save mvrozanti/520104a6b15c655470dfb3705e01821a to your computer and use it in GitHub Desktop.
RAT-via-Telegram shell edition (shellinabox)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from distutils.spawn import find_executable | |
import argparse | |
import os | |
import psutil | |
import requests | |
import signal | |
import subprocess | |
import sys | |
import telepot | |
def start_process(): | |
if not is_online(): | |
subprocess.Popen(f'shellinaboxd {" ".join(args.args)}', shell=True) | |
return True | |
def stop_process(): | |
for proc in psutil.process_iter(): | |
if proc.name() == 'shellinaboxd': | |
proc.kill() | |
return True | |
def is_online(): | |
return any(p.name() == 'shellinaboxd' and p.status() != psutil.STATUS_ZOMBIE for p in psutil.process_iter()) | |
def is_authorized(chat_id): | |
return not args.authorize or chat_id in args.authorize | |
def message_loop(msg): | |
chat_id = msg['chat']['id'] | |
if 'text' in msg: | |
bot.sendChatAction(chat_id, 'typing') | |
if is_authorized(chat_id): | |
command = msg['text'] | |
bot.sendMessage(chat_id, '-') | |
print(f'{chat_id}: {command}') | |
response = '' | |
if command == '/start': | |
response = 'Ok' if start_process() and is_online() else 'Failed' | |
elif command == '/status': | |
response = 'Online' if is_online() else 'Offline' | |
elif command == '/stop': | |
response = 'Ok' if stop_process() and not is_online() else 'Failed' | |
elif command == '/restart': | |
response = 'Ok' if stop_process() and start_process() and is_online() else 'Failed' | |
elif command == '/ip': | |
response = requests.get('https://checkip.amazonaws.com').text.strip() | |
else: | |
response = 'Unrecognized command' | |
print(f'\t:{response}') | |
bot.sendMessage(chat_id, response) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='shellinabox via telegram') | |
parser.add_argument('-a', '--authorize', metavar='client_id', nargs='+', type=int) | |
global args | |
args, unknown_args = parser.parse_known_args() | |
args.args = unknown_args | |
if not find_executable('shellinaboxd'): | |
print('shellinaboxd not found in PATH. Install shellinabox:\n\ngit clone https://github.com/shellinabox/shellinabox\n') | |
sys.exit(1) | |
bot = telepot.Bot(os.environ['GATEKEEPER_BOT_TOKEN']) | |
bot.message_loop(message_loop, run_forever=True, relax=0.05) | |
""" | |
REGISTER THESE COMMANDS TO THE TELEGRAM BOT: | |
start - ' | |
status - ' | |
stop - ' | |
restart - ' | |
ip - ' | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment