BSides SF CTF by MAD Security, Part 1

Last weekend I was at BSides SF and had the opportunity to participate in the Capture the Flag competition run by MAD Security/The Hacker Academy. I was able to clear 6 of the levels, and thought I'd write them up here to share my experience. Most of this is from my memory, so there might be a few inaccuracies, but the intent is to share the general concepts, not the specifics.

Backstory
Nick, a vocal opponent of the Absurdistani government, has disappeared, and you need to find out what happened to him.

Level 1: Information Gathering
Level one was basically just information gathering -- obtain Nick's personal email address and the password for his "myface.com" account. The first part was incredibly straightforward: go to myface.com and find the directory of members. Click on Nick's name and there's his email address. Obtaining his password is only another step away -- let's pretend we've forgotten our password, and get some password reset questions. Hrrm, what might the name of his dog be? I'm sure he guards that infor... oh, wait, what's the caption on the picture of a dog? And for bonus points, it turns out that myface.com stores passwords in plaintext, so they can just spit his out to us!

Level 2: Private Files
This one had a bit of a curve ball to it: Nick, an early adopter of Cloudbox, might have some private files stored there. (Find his private file and provide the md5sum of the file as well as part of the contents.) Let's go check out the site. We'll start with the obvious and try to login with Nick's username and password from myface.com (who doesn't love some password reuse?) -- but no such luck. Well, we can try creating an account and poking about a bit. We see that our account is at http://cloudbox.com/account/292. What happens if we change around the number at the end? Oh, look -- it's other people's accounts! Ok, so we can enumerate accounts, and find an account number "2" that has a video in it. Well, they did say he was an early adopter, so we download the file, md5sum, and... wrong answer! Crap, it's not that easy. With a little perseverance, you can discover a couple of other accounts with files -- might as well grab them all and give them a try. OK, one of the files appears to be gibberish, so it's probably encrypted, so let's try that as the secret file -- and the md5sum checks out.

Now how do we recover the secrets within?  Running "file" on the encrypted file just tells us that it's "data", which is not surprising for an encrypted file.  Maybe a hexdump of the file will give us a clue:

00000000  53 61 6c 74 65 64 5f 5f  88 36 d2 d4 a2 9f c8 58  |Salted__.6.....X|
00000010  9d 18 b9 4d d4 bc ef 20  38 a3 da 8a 4b 72 0c 1f  |...M... 8...Kr..|
00000020  e9 51 7e 95 c8 d0 f3 7e  9d dc 76 9e a1 b7 d0 67  |.Q~....~..v....g|
00000030  cd 84 ab 70 3b 43 34 24  70 d9 7c 9e d9 80 b0 13  |...p;C4$p.|.....|
00000040  46 f7 00 74 d0 ca 54 4c  27 a1 2c 24 e0 f8 a3 26  |F..t..TL'.,$...&|
00000050  77 03                                             |w.|

Salted__ looks meaningful... I seem to recall that using OpenSSL enc produces that at the beginning of the file, but I'm not sure. How can we confirm that? Let's run a small test and find out.

$ echo "foo" | openssl enc -des3 | hexdump -C
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
00000000  53 61 6c 74 65 64 5f 5f  65 33 31 b2 5c 41 6f ca  |Salted__e31.\Ao.|
00000010  9d 82 62 15 eb 9c 6c bc                           |..b...l.|

Looks like roughly the same thing, so we're probably on the right track, but we don't have a key. The level mentions that Nick's roots are from a web image hosting service, so maybe there's something interesting with that. I'd grabbed the images from Nick's myface page, so I figured I'd see if there was anything to those. "strings" is a handy utility for these sorts of things, and I quickly discovered the same thing in the EXIF portion of each of the images: Encryption Key: e82e1beefdaad9c. Could it really be that easy?

There's one more piece of information we need: what algorithm did Nick use for his encryption? I don't see anything pointing me in the right direction, but there's a handful of commonly used algorithms, so I just tried those: des3, aes-128-cbc, aes-256-cbc, aes-192-cbc and each time, I got "bad decrypt" until I tried rc4, where there was success! I found a text file containing what appeared to be 3 passwords (actual passwords munged):

aol: M0nk3yIsl@nd
tc: Fr0bb3rW!nn3r
wells fargo: Gord0nFr33m4n

I dropped the password into the scoring system, and I moved on to level 3... in the next post.


Homeland by Cory Doctorow

Those who know me will not be surprised to learn that I have stayed up until 1:45 AM reading Cory Doctorow's new book, Homeland. Homeland is the sequel to Little Brother, Cory's first novel about a dystopian near-future/present of the American Surveillance State, which was one of my favorite novels of all time. Homeland doesn't disappoint -- it's realistic enough to be scary, but sufficiently fictional to not be downright terrifying. Little Brother and Homeland are the Nineteen Eighty-Four of the 21st century -- a warning of an issue that society is largely ignoring, and that will affect every one of us.

Minor spoilers below.

I read Little Brother before my move to the Bay Area, and so the descriptions of San Francisco felt like they were talking about any other city I had visited or about which I had watched a movie. Reading Homeland now, I am immensely familiar with many of the areas described. Perhaps most excitingly, Noisebridge, the well-known San Francisco hackerspace, is featured prominently in the novel. Perhaps even more exciting to the geek in me are some of the early characters in the novel: John Perry Barlow, Mitch Kapor, and John Gilmore of EFF fame; and Wil Wheaton of Star Trek: TNG, The Big Bang Theory, and Internet fame.

Significantly, Cory gets the technology just right. Describing technologies like TOR in a correct but not boring manner is no small feat in a novel, and he pulls it off perfectly. Even more so, describing the terrifying capabilities of the modern police/surveillance state is something that only Orwell and Doctorow have done so convincingly. This, of course, is where the book gets all too real -- descriptions of police actions towards protesters, of pervasive surveillance, of spyware gone bad, and of politicians bought by "big business" -- this is the world we live in, and only the slightest details separate Homeland from reality. Doctorow hits it right on the head, and addresses the very things that keep me awake at night, and keep reminding me of why organizations like the EFF are so important.

Despite the plight of the protagonist, Marcus Yallow, the hardest part of Homeland for me to read was the 2nd afterword. Written by Aaron Swartz, the afterward is a reminder that Homeland, like Little Brother before it, is not just a novel, but actually describes many of the aspects of our society of today and tomorrow. This was especially hard to read when you realize that it is this very same society (and likely many of the same aspects of society) that led to Aaron taking his own life recently. Homeland serves as a reminder of all the things that Aaron fought for, and that the rest of us must continue to fight for.

Like Little Brother, Homeland must be read by anyone who cares about privacy, civil liberties, technology, or their intersection. Not only does the book address serious issues, it does so in a manner that makes it impossible to put it down until the very end. You'll be left actually thinking about social, legal, technological, and ethical issues, and that's exactly what society needs so desperately.


Playing with the Patriot Gauntlet Node (Part 1)

I recently picked up a Patriot Gauntlet Node just to take a look at it. Playing with the device, it seemed to be a pretty straightforward wireless SoC with a hard drive interface. Many, if not most, of these embedded SoCs use Linux as their operating system, so I decided to go a bit further and see what was going on.

I headed over to the Patriot website and downloaded the firmware for the Gauntlet Node, unzipped the file, and ran binwalk against it. (Binwalk is an awesome tool that essentially runs 'file' with a special magic file against every possible byte offset to find the parts of a firmware image.)

DECIMAL   	HEX       	DESCRIPTION
-------------------------------------------------------------------------------------------------------
0         	0x0       	uImage header, header size: 64 bytes, header CRC: 0xE7CCD2D7, created: Mon Nov 19 21:53:24 2012, image size: 5646212 bytes, Data Address: 0x80000000, Entry Point: 0x803DC000, data CRC: 0xCC23D5F0, OS: Linux, CPU: MIPS, image type: OS Kernel Image, compression type: lzma, image name: Linux Kernel Image
64        	0x40      	LZMA compressed data, properties: 0x5D, dictionary size: 65536 bytes, uncompressed size: 8244219 bytes
138771    	0x21E13   	JFFS2 filesystem (old) data big endian, JFFS node length: 411382
1909438   	0x1D22BE  	Broadcom header, number of sections: 25170859,
5051328   	0x4D13C0  	JFFS2 filesystem (old) data big endian, JFFS node length: 391617

This confirmed my suspicions -- a Linux OS resides on the device. (And no, before you look, there's no GPL sources to be found on the Patriot site...) So, let's see what's inside. Since the uImage header identified the compression as lzma, and the section at 0x40 is LZMA compressed (and first), it seems likely to be a kernel image. A quick dd if=PA21_1.2.4.5.bin of=kernel.lzma bs=64 skip=1;lzma -d kernel.lzma left me with a kernel image, confirmed by running strings on the file. The kernel by itself isn't very interesting -- userspace can tell us a lot more about how the device works. Let's look for a filesystem -- binwalk has identified two JFFS2 (common on embedded devices) filesystems. Unfortunately, it turns out that neither of those is actually a JFFS2 filesystem (binwalk does get the occasional false positive). So let's look at the kernel some more, maybe we can find some clues. Let's look for the kernel command line:

$ strings img.1.2.4.5 | grep 'root='
Please append a correct "root=" boot option
console=ttyS1,57600n8 root=/dev/ram0
console=ttyS1,57600n8 root=/dev/ram0

/dev/ram0 suggests we're booting from an initrd of some sort, maybe in the same lzma image? Let's try binwalk on the kernel image itself... That nets us a lot of noise, dozens of lzma and JFFS2 signatures, mostly obvious false positives (massive dictionary sizes, uncompressed sizes, etc.). But the very last image found, an lzma image, has a dictionary size of exactly 1048576 (1MB), so seems like a likely candidate. Another dd and lzma later, and I've got a file that 'file' identifies as a cpio archive (standard for an initrd). cpio -idv --no-absolute-filenames gets me a nice directory structure of a linux root filesystem. Finally something to work with!

I poked around the filesystem a little, and I'll have more on that later, but the most interesting thing I've found so far is a file named killprocess.cgi in /etc_ro/web/cgi-bin. It turns out that given the right argument, this triggers a busybox telnetd. That's right, it just takes a web request to get telnet! http://10.10.10.254/cgi-bin/killprocess.cgi?service (using the default IP) starts telnetd right up. admin/admin logs you right in.


Social Engineering: The Art of Human Hacking

I just got done reading Christopher Hadnagy's Social Engineering: The Art of Human Hacking. If you are interested in the social aspects of information security, this provides an in-depth view of the actual techniques and science behind social engineering. While books like Kevin Mitnick's The Art of Deception and The Art of Intrusion tell amusing and noteworthy stories of social engineering hacking, Hadnagy's book tells you why and how it works. Hadnagy's exposure all reveals the most important lesson -- how to defend against the attacks.

Hadnagy takes you through all the steps of a social engineering exploit -- from information gathering to the exploit. He discusses techniques like elicitation (extracting information from a target), influence (getting them to do what you want), pretexting (developing the back story that makes the attack believable), micro-expressions (control the subtle muscle movements that can give you away), and neuro-linguistic programming (the exact way you say things can make a big difference).

It doesn't matter if you're blue team, trying to protect your valuable assets against attack, or red team, trying to get in there, but it's critical you know how to exploit the human element of security. After all, the devil you know is better than the devil you don't.


The segmentation fault occurred where?!?

I recently ran into a C++ problem where a segfault was occurring in code in a stable library that hadn't been changed in a while. For a while, I couldn't figure out what would have broken in that library, and the call site looked perfectly fine. Before I give away the answer, let's take a quick quiz. What does the following code output? (And yes, this is somewhat compiler dependent, so let's pretend we're talking about how g++ works.)

#include <cstdio>
 
class Foo {
  private:
    char *name;
  public:
    void whatami() {
      printf("I am a Foo.\n");
    }
    void whoami() {
      printf("I am %s.\n", name);
    }
};
 
int main(int argc, char **argv){
  Foo *f = NULL;
  f->whatami();
  f->whoami();
}

My first instinct was to say "Segmentation Fault" and nothing else, because line 17 is going to dereference a NULL pointer. It turns out, of course, it's not that simple -- it'll actually print the "I am a Foo" before segfaulting. Clearly, then, the segfault must be on line 18, right? Wrong. Line 11 is where we give up, as f is not dereferenced until then. To see why this is, let's think about the equivalent C code:

#include <stdio.h>
 
typedef struct {
  char *name;
} Foo;
 
void Foo_whatami(Foo *self) {
  printf("I am a Foo.\n");
}
 
void Foo_whoami(Foo *self) {
  printf("I am %s.\n", self->name);
}
 
int main(int argc, char **argv) {
  Foo *f = NULL;
  Foo_whatami(f);
  Foo_whoami(f);
}

As we can see here, the pointer is actually unused by the method "Foo_whatami". But wait, you say, don't we need the address of Foo to resolve the location of the method? No, as whatami and whoami are not virtual methods! Their addresses can be determined by the compiler at compile time. Virtual methods, on the other hand, need a pointer from within the data area of the object to the vtable to resolve addresses. Change whatami to a virtual method, and you'll crash much more efficiently.

So remember, even if the code looks like you're dereferencing the pointer, it may well not be dereferenced until much later!