Showing posts with label security. Show all posts
Showing posts with label security. Show all posts

Friday, November 20, 2015

Avoiding Data Retention in Australia

On Tuesday, October 13, 2015, Australia officially started retaining metadata. The legislation mandates that the metadata be kept for at least two years and be made available to a handful of agencies (of which the scope has changed multiple times).

Because I'm a pro-privacy kinda guy, below are a few links that hopefully makes privacy a bit more accessible, especially for the less tech-savvy people.

Take privacy seriously. Once it's gone, it'll be even harder to get it back.

Friday, October 16, 2015

A Little Assembly for the Infosec Guy

If you who grew up in the 90s with some kind of interest in computer security, there's a decent change you probably only ever attempted to learn how to program in assembly for one reason: writing shellcode.

I didn't grow up in the golden era when most programmers programmed in assembly and I only did one computer systems subject at university which had us learning assembly for the Motorola 68000 microprocessor. And these days, due to the ridiculous amounts of memory machines have, in only a few extreme circumstances, the lowest-level programming language I'll ever have to program with will be C, and it'll only be as an extension to my main programming language (Perl). Unless I end up working with embedded systems, or if I get into something like malware analysis/reverse engineering as a career, dealing directly with assembly is something I'm probably not going to do as part of my job, so it has to be pursued on the side.

Slowly over the past year or so, I've been spending a couple of hours here and there playing with some very small programs, solving reverse engineering problems for wargames and writing my own shellcode from scratch rather than stealing them from shell-storm all the time.

It's been long and challenging, and I wanted to share a few rewarding experiences.

Writing shellcode that doesn't just spawn a shell

Since shellcode was what originally exposed me to assembly all those years ago, why not further that skill?

execve(/bin/sh) shellcode is what's used in pretty much every tutorial on buffer overflows and shellcode ever (I mean, I assume that's where the name shell code came from, right?). But there are other useful kinds of shellcode to write.

An easily modifiable and totally unoriginal shellcode that I wrote to read a file and write it to stdout has come in super handy when playing online wargames, since many of these games require reading a file - that you usually know, or can guess the name of, ahead of time - to get the password to the next level. I also had to write a less easily modified version of the same read/write shellcode without using the jmp-call-pop technique.

I like writing shellcode because I'm writing a small program from scratch that does a very specific thing and isn't overly complicated. It requires reading a little documentation to figure out what syscall(s) I need and how they are called and it's often written quick enough that the pay-off is immediate and there's a practical purpose to the program, as opposed to writing a program that uppercases whatever's in argv[0].

A lot of reverse engineering is patterns

While reversing a program for a more difficult level in Blackbox, I came across some code that was indexing into an array of chars a lot, and I picked up a reoccurring pattern:

  0x804859a:    lea    0xfffffbe9(%ebp),%eax
  0x80485a0:    mov    %eax,%ecx
  0x80485a2:    add    0xfffffbe4(%ebp),%ecx

At offset 0xfffffbe8(%ebp), I knew there was a char array stored on the stack, let's call it buf, so 0xfffffbe9(%ebp) is buf + 1. At offset 0xfffffbe4(%ebp) was an int that was being used to index into the array, so because we add that value, what we essentially end up with in ecx is the address of buf[index + 1].

  0x80485a8:    lea    0xfffffbe9(%ebp),%eax
  0x80485ae:    mov    %eax,%edx
  0x80485b0:    add    0xfffffbe4(%ebp),%edx

This is the same thing, just placed into the edx register.

  0x80485b6:    lea    0xfffffbe8(%ebp),%eax
  0x80485bc:    add    0xfffffbe4(%ebp),%eax

This is almost the same thing, except it's the address of buf[index] that's placed into the eax register and the compiler used one less instruction to do it.

  0x80485c2:    movzbl (%eax),%eax
  0x80485c5:    xor    (%edx),%al
  0x80485c7:    mov    %al,(%ecx)

So if eax is the address to buf[index], ecx is the address to buf[index + 1] and edx is also the address to buf[index + 1], the first instruction puts the value at the address of buf[index] into eax, xor's it with the value at the address of buf[index + 1] and moves that value into the value at the address of buf[index + 1]. So all of this translates to the following bit of C code:

  buf[index + 1] = buf[index] ^ buf[index + 1];

There were about 30-40 instructions that all did similar things all in a row - which is intimidating until you break it down into chunks of 2 or 3 instructions - and recognising the pattern and relating it to a higher-level concept made reversing the rest of the program much easier.

The more of these patterns you find, the more you understand how compilers break down higher-level ideas.

"Indexing into an array... how exciting..."

Finding gold along the way

The first real assembly nugget that I came across earlier this year was while playing Blackbox. While checking the disassembly of a program in gdb, I came across this sequence of instructions:

  0x08048519 :    lea    0xfffffc10(%ebp),%eax
  0x0804851f :    mov    $0xffffffff,%ecx
  0x08048524 :    mov    %eax,0xfffffc00(%ebp)
  0x0804852a :    mov    $0x0,%al
  0x0804852c :    cld
  0x0804852d :    mov    0xfffffc00(%ebp),%edi
  0x08048533 :    repnz scas %es:(%edi),%al
  0x08048535 :    mov    %ecx,%eax
  0x08048537 :    not    %eax
  0x08048539 :    dec    %eax

My reaction: what the fuck was that? What the hell is repnz? And scas? Is that two opcodes in one? What's that funky addressing syntax? And cld... an opcode with no operands? What is this?!

I won't go through and explain each instruction, since - as I discovered soon after figuring out what this little nugget was doing - there are already plenty of sites that have explained it before, but it was pretty cool to step through the code while reading reference documentation to figure out what those unfamiliar instructions were doing.

The short of it is, as explained by this StackOverflow answer, it's an implementation of strlen, and sometimes gcc will inline it as a little optimisation.

Moving forward

I'll keep reverse engineer programs and write shellcode for wargames. It's just a lot of fun once you get into the swing of it.

Lately something else that's become a lot of fun is learning how some low-level parts of systems work. An article about building a Tetris clone in x86 assembly does a great job at introducing some parts of systems that are much lower-level than I'd usually deal with (VRAM) and some more advanced topics (bootloaders).

2015-11-04 edit: I rewrote a few paragraphs, because I hated how they read.

Thursday, September 25, 2014

Wargames

Since high school I've enjoyed playing online wargames. I can't remember which ones I started with, but the earliest web-based wargames I can remember were a good ten years ago on the Digital Evolution website. At the time I wasn't smart enough for the wargames you had to ssh into, especially when it came to levels involving buffer overflows and that weird shellcode stuff that I just copy+pasted from aleph one's famous phrack article, an article I would read through and pretend like I understood, but really didn't. But that changed over the years. I'm still no expert, but I know enough to know roughly what I need to do... just perhaps not how to do it. Yet.

Last year some time, a friend sent me a link to io.smashthestack.org. I worked my way through the first half-dozen levels before getting stuck and deciding there were way too many knowledge gaps for me to grind my way through.

I checked my ego at the door and went back to the basics of basics and treated myself like a rank beginner (since I am); I learnt some x86 assembly and played around a lot with nasm. This eventually led to writing my own shellcodes and testing them out. Now I needed to use the shellcode to make sure it was gonna work. So it's back to wargames...

I've ended up at blackbox.smashthestack.org and am working my way through. There are ten levels and I'm working on level six at the moment when I've got the spare time.

The Shellcoder's Handbook has been a great help.

After I finish this, I'll head back to io.smashthestack.org and continue working through its plethora of problems.

Tuesday, April 10, 2012

Attack of the Phone Scam?

Yesterday afternoon, my girlfriend, Cherie, and I walk into her home. Her dad was on the phone with his laptop in front of him. He's not the most computer savvy person in the world, so he asks us to help him with this person on the phone. I sit down at the laptop and take the phone. MSConfig is open (alarm bells start ringing), as is the "run program" box with "www.ammyy.com" typed in (ding ding ding).

"Hello?" No answer... "Hello?"

A man with a thick Indian accent finally speaks. "Hello, are you the manager of this laptop?" I respond, "No" The man repeats his question and I repeat my answer.

"Who are you? Where are you calling from?" Apparently he is from my Windows service provider (which I never got the name of) and apparently the laptop had made several attempts to access forbidden sites (which I never got the addresses of) and they needed to fix the problem for me.

"Ok, so what do you need me to do?"

After a bit of confusion on his part - apparently "all the programs are closed, I can only see the desktop" is a little confusing - he asks me to press the Window button and "R" at the same time. I then have to type "www.ammyy.com" and press Enter. I don't, of course, because I'm a critical thinking human being who knows that Microsoft don't have the resources to call the billions of end users that they have around the world to remove a virus, which their Microsoft Security Essentials product is supposed to take care of anyway, and certainly not via some non-Microsoft owned remote desktop software.

I Google the address, and quickly find out that the site hosts some kind of remote desktop software and that there are forums all over the internet talking about getting phone calls, having the same story spun to them, and attempting to coax the mark into downloading some software and some people even getting charged a small fee.

While stalling him, I hear him put me on mute a couple of times, I assume because he's annoyed that it's taken me a good five minutes just to press Enter.

He starts talking again. I hang up.

Cherie and I tell her dad that it's a scam and to hang up on him if he calls back, and we head out for a few minutes. When we return, her dad is on the phone with this guy again. Cherie grabs the phone.

"Hello, we have looked up the site that you asked us to go to and it's quite obviously a scam. Stop calling." She's pretty awesome like that. I would've preferred to mess around with him a little, but time is precious. The man responds with something along the lines of "I will call every day." Fine, whatever. The more time you spend failing to scam us is less time you spend successfully scamming others.

I'd always read about phone scams like this happening in other countries, but never in Australia.

Wednesday, December 1, 2010

Host Discovery via SPF Records?

So... my once-a-month posts OCD issue went unsatisfied for the past 2 months. Oh well. Back to business...

Sender Policy Framework (SPF) records are often used as one method of combatting spam; specifically, spam that wants to look like it has been sent by your company. The general idea is that your SPF records specify which hosts and IP addresses are allowed to send email using your domain(s) in the envelope 'from' address. Any other host, which is not contained within the SPF policies, that attempts to send mail from your domain will fail the SPF check and, hopefully, get picked up by some kind of spam detection software further down the track. It is highly recommended that you setup SPF records for any domains that you own.

How are SPF records requested? Via DNS. They are usually contained within the TXT record, however they can also, occasionally, be found in the SPF record aswell. The format is very easy to understand. An typical example to look at would be optus.com.au:

IN TXT "v=spf1 mx/24 include:opt01._spf.optin2.com.au ip4:180.92.216.0/21 include:rightnowtech.com include:rnmk.com include:custhelp.com ~all"

Do the hosts (and the range of IP addresses) in the policies belong to optus.com.au? Maybe. Maybe not. We cannot discern that just by looking at the host names, nor can we, within any real degree of certainty, determine it programmatically. However, the hosts' SPF records have been trusted to send mail on behalf of optus.com.au, so they remain hosts of interest for penetration testers and may not pop up in other DNS requests (A, MX, AFXR, etc..)

What is a common example of a host who would be in a domain's SPF policies but not actually part of the company who owns the policy? Any mail filtering company who filters outbound mail.

Just a thought that has been floating around in my head...