Thursday, May 20, 2010

Command Line Goodness Part IV


So, in this installment of Command Line Goodness, I am going to cover a few different utilities that I’m certain will prove useful in your investigations going forward.

Once again, I am going to use my own FireFox history as my example file. For those of you who don’t know, this file is located in C:\Documents and Settings\\Application Data\Mozilla\Firefox\Profiles\.default\places.sqlite.

Now, to make things easier on myself, I use the UnxUtils “ls” instead of the native Windows “dir”. It’s quick, has numerous command options, and I don’t have to flip flop back and forth trying to remember if I am on a *nux or DOS command prompt (something you will come to appreciate the more you use command line utilities).

So, I use ls to list all of the contents of my current working directory (frequently referred to as the CWD). And I get something that looks like this…




OK…so what…that’s not very kewl Chris…well…like I said, there are plenty of command options that will make your life easier. In particular, I am going to use the options to provide me with long listing (more information such as file permissions, ownership, and last access time) , sorted by last access time, as well as by size.




OK…so the screenshot above is a bit kewler. It provides me some additional information as well as it has sorted the files by last access time, recursively (newest files on the bottom). I can also issue the same command without the “r” (for recursive) and the newest files will appear at the top.

I can also sort by size, which is very helpful with trying to quickly locate either large or small files. In this case, my browser history is likely a pretty large file, so I am going to sort recursively by size, placing the largest file on the bottom.

OK…now I can see that my largest file is indeed my places.sqlite, which is my full browser history (and I didn’t have to eyeball file sizes…nice huh!).

Next, I want to make a copy of that file in my tools directory since that’s where all of my command line tools are located. This is NOT a necessary step if you have your command line tools either in system32, or you have added your tools directory to your default path.

So, to make the copy, I will issue this command…

> cp places.sqlite c:\tools

Now that I have a copy of my browser history in my tools directory, I am going extract the printable characters with the “strings” command…like so…

> strings places.sqlite > web_history_strings.txt

***something to remember…you have this really kewl thing called “command line completion or tab completion”…which means that once you have hit a sufficient delimiter, you can simply hit the tab key and the rest of the command will be completed for you. In this example, I typed “str” and hit tab, and “strings” was automagically filled in for me! I did the same thing with “pla” TAB…and blamo…the rest of the filename was filled in for me. Very kewl. Use this for long path names as well…like “Documents and Settings”, “Program Files”, or “Application Data”.***

Now, I JUST want to see any hits on the keyword “forensics”. To do that, I use the grep command, making sure to use the “-i” option, since I don’t want to limit my search by case. So my command would look like this…


Now you all have a window into my secret habits of web surfing…oooohhh. This is a pretty simple example, but you can see where you have the ability to grep through large text files in a very short period of time based on specific search criteria. Just remember, if you search string has spaces in it, you need to put them in double quotes. To illustrate this, I am going to use a test timeline I created of my local system. If you don’t know how to do this, please refer to my blog series (Timeline Analysis).

In this example, I want to search for all activity that took place on March 15, 2010 (totally arbitrary date). My search would look like this…


I piped my output to “more” so that it would only show me one screen at a time…I can advance per line with the “Enter” key, and one page at a time with the “Space Bar”. As you can see, I could literally do the same thing with any date, and any time. Pretty slick and INSANELY useful when parsing timelines!

Now let’s say, you JUST want to see the hits on the dates of March 15th…nothing more…just the dates. Since this output is pretty clean, it’s pretty easy to parse using gawk (Windows port of the *nix utility “awk”). If you look at the output on the screenshot above you will see that it’s separated by spaces. If you count from left to right, assign each field a number. In this example, the “day” field would be number 1, the “month” field would be number 2, the “date” field would be number 3…and so n…you get the idea. So, now, if I JUST want to see the date field, I could use gawk to print field number 3…like so…

Now you might be thinking…ok Chris, that’s neat and all, but when could I use that in a real investigation. Well, think of this….say you have a HUGE text file of credit card transactions and you know that the fraud was only committed against a certain type of card…let’s say GOLD cards. You know that the Bank Identification number is 1234 for the specific issuing bank you are working with and you know that the sub-bin for their GOLD cards is 567. You can pull out JUST the field with the card numbers, then grep out the GOLD cads based on what you know about the BIN and sub-bin..so…your command might look something like this…

>cat cc_file | gawk “{print $4}” | grep 1234567

This command would print ONLY field number 4 (which presumably would contain the CC numbers), then it would only display the numbers that met the criteria of having the numbers 1234567 in that specific order. If you wanted to get a count, you could also add this to the end of the command…

>cat cc_file | gawk “{print $4}” | grep 1234567 | sort | uniq | wc -l

By piping your output to sort, you will put all of the numbers in numerical order from lowest to highest, then by using the “uniq” command you will remove all duplicates, finally you are using “word count”…the command “wc” to count the number of lines. You could also provide a count for the number of times each card number appeared by adding the “-c” option to the uniq command. This would still remove the duplicate entries, but it would also provide the number of times that each number occurred, just to the left of the entry.

So, with this one simple command, you could parse your fatty text file by…

1. Printing JUST the field that contained the card numbers

2. Grep’ing out JUST the GOLD cards by BIN and sub-bin

3. Sort the numbers numerically

4. Remove the duplicate entries

5. Provide a count of the number of times each card appears

6. Provide the total number of GOLD cards in the file

Not too bad for a SINGLE command. Good luck doing that same thing with a GUI utility sucka!

As you can see, command line utilities like this are very powerful and can provide you with a slew of information very quickly. The key is to know what you are looking for, then map out the commands you need to do extract that data.

A good trick to getting this down in your head is to start with a small sample of your data…like 10 lines. Look at those lines and map out either on paper, or in your case notes what you are trying to do. No joke…it would be as simple as this…

“I want to extract all of the credit card numbers. Next, I ONLY want to see GOLD cards. Then I want to see how many GOLD card entries are present and provide a count of the number of times each card appears.”

Mapping your search criteria out like this can help you really zero in on exactly the data you are looking for…Sniper Forensics…it will also be helpful should you need to ask somebody like me for help. You could say…hey dood…here is my data, here is what I am looking to do, can you help me with the command structure. Then, as you begin to figure this stuff out, create a text file called, “Commands” or “Useful Commands” or whatever…and keep track of which commands do what. That way you A) Don’t have to ask the same question more than once (which really makes you look like a tool), and B) will help you to get more efficient at command line goodness…which again, will really take your investigations to an entirely new level.

Happy hunting!

Wednesday, May 12, 2010

CFP Accepted at SecTor



Once again, I will be presenting at SecTor this year! I will be giving the second installment of Sniper Forensics, "Target Acquisition". Going to be a great talk, and if this year's con is anything like last year's, I would highly recommend trying to attend.

Monday, May 10, 2010

Command Line Goodness Part III

Now...I am still going to be working with my local web logs for the sake of protecting NDAs and whatnot (that's for Harlan). So, I go to my Mozilla profile and run a strings on my places.sqlite...which is case you don't know, the command "strings" displays all printable characters from a file. To see the difference, run the "cat" command on the same file (provided you are using FireFox) and see what happens (HINT...DON'T do this at 0630 when your wife and kids are still sleeping!). Then, use strings against the same file...ahhhh...much better.

So I redirect the output to a file I simply called, "example_web_history.txt"...so my command looks like this...

>strings places.sqlite > example_web_history.txt"

Now that I have my working file, I decide that I only want to see hits on the keyword "Facebook". SO, using grep I can do this in one of two ways...

>cat example_web_history.txt | grep -i facebook
or
>grep -i facebook example_web_history

Now, I redirect to an output file called, "face_out.txt" and I have ONLY hits with the word "Facebook" somewhere in the URL.

In this post, I am going to show you how to use the "cut" command, so while this example is completely benign, it does illustrate how useful this command can be.

OK, so in the screenshot below, you have some information about my Facebook habits...ohhh...how interesting. But let's say for the sake of the post, that this is some important data that is surrounded by garble. Let's say I JUST want the numbers that come between the two equals "=" signs.

First step is I figure out some data reduction. In this example, I only want the lines with the word "photo" in them...which s quick line count shows me is 615 lines...still way too much data!

(C:\Documents and Settings\User\Application Data\Mozilla\Firefox\Profiles\fdbj8kp8.default>cat face_out.txt | grep photo | wc -l)

So now, I want to suck out my numbers that come between those equals signs.



I would use cut like this...

>cat face_out.txt | grep photo | cut -d= -f2

The -d is for "delimiter" and indicates what I want to use as my identifying mark of where to separate out the lines. Next, I tell it that the delimiter that I want to use is the equals "=" sign, and finally I tell it to print field number 2. The cut will literally CUT the lines into two fields...the one before my chosen delimiter (1) and the one after my chosen delimiter (2). So now I redirect my output to a file called, "just_nums.txt", but I still have some work to do to get JUST the numbers.

I see that I have a bunch of lines with the word "home" in it...so to get rid of those, I can use grep with the -v option, which tells it to show me everything that does NOT have those lines...it's called a inverse search. I can repeat this as many times as I like...

>cat just_nums.txt | grep -v home | grep -v feed | grep -v dopp | grep -v photo

my resulting output is only 262 lines, and is JUST numbers...nice data reduction and I didn't have to monkey around with spreadsheet or eyeball anything. As you can see, cut is a pretty kewl command with a lot of options. The key to being able to use it in your cases is KNOWING what you are going after. The more specific you can be, the more efficiently you can extract your data.

Pretty neat huh...

Thursday, April 22, 2010

Command Line Goodness Part II

OK...so fortunately for me (and you), I am working a case right now that will help me to illustrate my point about using the command line perfectly. Now, let me preface these next few posts, and simply state that this is MY way of using the command line to parse data. There are SO many tools, and SO many different command switches, that you could literally skin the proverbial cat a dozen different ways. My way works for my purposes. If you have a command syntax that is more efficient, please let us know! I'm all about getting better!

Now, In this case, I have six images from a Point of sale (POS) network. My first goal is to find if I have track data in the clear on any of the images. Since this the fraud in this case is "card present", meaning that the bad guys jacked track data and used it to create new cards, I am anticipating finding some. For the purposes of data reduction, I JUST want to work with the systems with clear text track. So...I mount my images read only with ImDisk (This is pretty basic, so I am not going to cover how to do this in this post. Basically, you just need a single raw image, since ImDisk will not work with split images.) Once mounted, I use this command:

z:\>egrep -r "\;?[0-9]{12,19}[\=][0-9]{12,32}\.?" * > c:\cases\track2hits

This regex will provide me with all of the matches, along with their full paths. Now, I anticipate finding data in log files, or backup files. However, what I am looking for is a totally abnormal location. For example, in this case, I found a file we'll call "winprc.dll". Now, dll files should NOT contain track data...at least none that I have ever seen, so this is automatically extremely suspect and likely a dump file for a credit card sniffing malware. How do I know this?

Well...think about it for a second. You have track data...in a dll...which should absolutely not be there. Something put it there...it had to to get there in the first place. So, in less than 30 minutes, I know can make the logical ju
mp to the fact that I have an intruder, I have malware (since I have a dump file), and I will logically have to have an exfiltration point....bad guy has to get his goodies off the network so he can make his counterfeit cards right? All this from a single regex...pretty kewl huh.

Now, I can start creating my "dirty word list"
or set of keywords to search on later. The name of this dll is now at the top of my list.

OK...so next, I am interested in who connected to this box. So, I export my event logs in hopes of finding some IP addresses. Once I export them, I convert them into flat text files with Event Log Explorer and stick in them my working case directory.

Now, I JUST want the IPs from the log
s...nothing else. Now, since I am working with customer data, I am not going to show the actual event logs, but here is a screenshot of the event logs from my local machine.


OK...a quick check with word count and I see that I have 2394 lines in JUST this one log. (cat filename | wc -l). Now, I could export the logs to a csv and go through it in a spreadsheet, but that would require me to manually pick out IPs. WAY too time consuming. I want answers now. So...back to my regexes. I can use the regex for IP addresses and let's see how much smaller we can make this sample.

egrep -o "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" | sort | uniq

SO...this regex pulls out anything that matches an IP address, the "-o" option shows me ONLY the hits for the regex (as opposed to the entire matching line), "sort" puts them in numerical order, and uniq removes the duplicates. Leaving me with this...

0.0.0.0
10.32.8.162
10.96.33.21
10.96.33.23
192.168.40.2
2.6.1.62
255.255.255.0
5.3.1.0
82.0.174.0
82.0.188.0

A whopping 10 IP addresses...or at least what appear to be IP addresses, but that' for me to figure out. Now again, this is not the data from the images, but from my local machine, but it clearly illustrates my point. I went from over 2000 lines of log files, to 10 in a few seconds by using the command line. Now, if you want to get a count say of the number of time each IP appears, simply add the -c (for count) option. In this example, my results looked like this...

12 0.0.0.0
2 10.32.8.162
3 10.96.33.21
2 10.96.33.23
6 192.168.40.2
3 2.6.1.62
6 255.255.255.0
1 5.3.1.0
1 82.0.174.0
1 82.0.188.0

The number off to the left shows the number of times each IP appears in the log. Pretty slick.

Now, you can use these commands to parse anything...not just IPs! In my current case, working with these two commands gave me an idea of what was going on, and which IPs may have interacted with this system, all in under 30 minutes. Not going to solve the case by any stretch, but as you can see I can perform significant data reduction using these methods.

In my next post I will cove how to use cut and awk to carve more complex log files to extract JUST the data you want.




Monday, April 19, 2010

Command Line Goodness Part 1

OK...so if you don't know how to use the command line effectively in your investigations, you are REALLY limiting yourself. Sure, there are plenty of tools that will do what I am going to cover in the next few posts, but I would argue that using the command line is MUCH faster, more efficient, and if done properly, can help you perform data reduction with more accuracy...not to mention that these methods and tools are FREE.

First of all, you will need to download UnxUtils. This is a suite of Windows port overs of the most popular Unix command line utilities. After you unzip the file, you can either drop them into the system32 directory, or add their path to your profile. You will also want to grab, grep v2.5.4, MD5deep and SSDeep, as well as perl and python v2.6.5. Once you have these command line utilities installed and part of your path, you should be able to use them from any location...which is WAY better than having to navigate to each respective directory just to issue a command.

So...not to rush, but I am getting on a plane shortly, so I am going to provide you with my list of regexes and a few commands. We will use these for the throughout the duration of this blog series, so if you are going to follow along, get your box set up, and we will begin analysis in part two!

All credit cards:
egrep -r "(4[0-9. -]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})" * > :\outfile.txt


Generic CC Numbers :
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$

CC Numbers by brand:
Visa - ^4[0-9]{12}(?:[0-9]{3})?$
MC - ^5[1-5][0-9]{14}$
AMEX - ^3[47][0-9]{13}$
Discover - ^6(?:011|5[0-9]{2})[0-9]{12}$
JCB - ^(?:2131|1800|35\d{3})\d{11}$
Diners [^0-9](30[0-5]{1,1}[0-9]{11,11})|(3[68][0-9]{12,12})[^0-9]
JCB [^0-9]35[2-8]{1,1}[0-9]{1,1}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[^0-9]
MasterCard [^0-9](36[0-9]{12,12})|(5[1-5]{1,1}[0-9]{2,2}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[^0-9])
Visa-13 [^0-9]4[0-9]{12,12}[^0-9]
Visa-16 [^0-9]4[0-9]{3,3}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[^0-9]
AMEX [^0-9]3[47][0-9]{2,2}[\x20\-]{0,3}[0-9]{6,6}[\x20\-]{0,3}[0-9]{5,5}[^0-9]
Discover [^0-9](6011)|(6[245][0-9]{2,2})[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[\x20\-]{0,3}[0-9]{4,4}[^0-9]

Track 1:
\%?B[0-9 ]{12,19}\^[^\^]+\/[^\^]+\^[0-9]{7,7}[^\?]+\.?

Track 2:
\;?[0-9]{12,19}[\=][0-9]{12,32}\.?

IP address: '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
egrep -o "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"

Emails:
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$

URLs:
^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?


awk '{sub(/^/, "", $1); print }'

awk '{sub(/=(.)*/, "", $1) ; print }'

cut -d':' -f2 <-- This command cuts all before the ":" See you soon!

Thursday, April 8, 2010

The Next HOPE Conference



Sniper Forensics was just picked up by The Next HOPE (Hackers On Planet Earth) conference in New York City from July 16 - 18. Sweet!

Monday, April 5, 2010

ITB 0x1 Released



Issue 0x1 of ITB was posted this morning! There are some great articles by Don Weber, Harlan Carvey, Jamie Levy, Didier Stevens, and Me. Please give it a read, and give us some feedback! We really want this to be a success so like anything in the open source community, your input and assistance is needed.

Enjoy!