Tuesday, December 21, 2010

Introducing Fuzzex, Generating Random Data From Regexes

Fuzzex produces sequences of random bytes using a generation language that is similar to that commonly used by regular expressions for parsing data. This similarity enables testers who are familiar with regular expressions to produce test data that can satisfy an application's superficial input validation and parsing without getting bogged down in specialized frameworks such as Sulley or Peach.

In situations where the regular expressions used for parsing and validation are available, Fuzzex enables using these expressions directly to develop tests that demonstrate potential weaknesses and exercise internal surfaces.

Example, a Very Permissive Email Address Regex:

>>> fuzzex.generate( '[^@]+@([^.]+)([.][^.]+)+' )
'\x07m\x10@\x0cI\x12%.\x1a.f.:'

Thursday, November 18, 2010

Spot the Crypto Bug

Had a fun crypto bug crop up in a discussion, today; the code in question, functions changed to protect the guilty:

   iv := read_cprng( 16 )
enc := aes_enc( key )
ciphertext := cbc_enc( iv, enc, iv + plaintext )


Where cbc_enc is a function that accepts an initialization vector, a block encryption function, and a buffer containing the plaintext to encrypt, and applies that function using the Cipher Block Chaining mode and the initialization vector.

Can you spot why, regardless of variance in the IV, given a constant plaintext and key, why the ciphertext never varies?

Sunday, November 14, 2010

Lexical Analysis of C using Python and Ply

Code reviewers fall into two camps; those who rely on grep and their favorite text editor for review, and those who rely on a sophisticated language-specific review environment or IDE with a cross-reference generator. Consultants tend to be in the former camp, as getting a customer's random code base into an IDE can be almost as miserable as getting it out.

I use a hybrid strategy, involving a simple webapp that does syntax highlighting and grep with a few simple features that lets me combine common browsing habits (history, document tabs and linking) with a minimal expectations environment. It isn't beautiful, or featureful, but it doesn't interrupt my flow.

Of course, there's always room for improvement, like a cross-reference of identifiers, and the source files that mention them. This requires simple lexical analysis which is where a smart C programmer goes to Flex. So, where does a Python programmer go? My best guess is Ply -- a Python Lexical Analyzer that merges Lex semantics with Python metaprogramming.

So, in WEPMA fashion, here is the interesting bit, a lexical analyzer that produces identifiers, line numbers, and tokens indicating the start and end of lexical scopes. It is barely smart enough to filter out comments and strings, and tolerant of unanticipated syntactic elements because, obviously, I couldn't be bothered to implement a full C lexer.

Enjoy, and no, you can't have my review tool. :)

Saturday, October 30, 2010

JavaScript, Closures, and Wasteful API's

First, read this function: later (YUI). I consider this a great example of how framework developers can overreach with abstractions, considering that JavaScript has lexical scope and closures that are fairly easily implemented. Now, read the implementation: YUI-Later.js

Yahoo has written roughly 30 lines to encapsulate and abstract the simple functionality of passing a thunk to either setInterval or setTimeout. An example, stripped from Todd Kloots' YUI 3 demo:
var args = [ 1,2 ]
Y.later( 50, gizmo, gizmo.foo, args )

Could be more simply expressed as:
setTimeout( 50, function( ){ gizmo.foo( 1, 2 ) } )

And, hey look, no CDN callout required. No need for a code reviewer to reach out for YUI's documentation to find out the special semantics of YUI, and it explains exactly what it means. And, bonus, fewer keystrokes.

Libraries like jQuery and YUI have valuable capabilities, such as concealing all of the W3C's pointless DOM verbosity behind more modern XPath-like selectors. But when these frameworks feel the need to abstract away closures, all I really see is a developer who has lost touch with the clear simplicity of JavaScript.. And start wondering if they get paid by the API function.

Monday, October 18, 2010

Long Polling with Node.JS and Express

When I write tools or algorithms that I want other people to improve or understand, I use Python. When I am writing them for myself, because I'm in a hurry, I use Lisp. (I think in closures and the application of functions, which I occasionally force myself to re-express in classes and methods.) Since Python's Lambda syntax is a great disappointment to me and its father, Guido van Rossum, I occasionally pine for the weird cousin of Lisp we call JavaScript.

JavaScript is regarded by Lisp hackers as Lisp without parenthesis, shackled by the problem domain of browser scripting. It's a great, powerful language for people who think in closures, but until the recent introduction of libraries like jQuery, it's also shackled to really cruddy libraries. When Google released V8 under the BSD license, I think many of us immediately ran to check out the source, write a partial general purpose environment, then wandered off to do better things. Like bugfixes for MOSREF. *cough*

Ryan Dahl, unlike the rest of us, stuck with it, and fused V8 with the similarly fascinating libev to produce a JavaScript environment for I/O-centric problems that don't live solely within the browser. The resulting Node.JS strikes an interesting balance between minimalism, functionality, and performance thanks to its reliance on existing projects with great characteristics.

When I encounter a new language or framework, I fall back on a set of problems dear to my heart -- writing a MUD server. With web frameworks, lately, this has been simplified down into "can I write a long-polling message wall with it?" Simple problem, tends to break most simplistic web frameworks simply because requests are often deferred, waiting for an update.

Here it is in Node.JS, using Express, about 50 lines of overcommented code. I'm sure it could be written faster, but probably not as concisely.

Next up, making a kobold walk around a message board.. ;)

Thursday, August 12, 2010

More Fun With Nessus Reports

A common grievance for security professionals dealing with Nessus reports is the organization of the report by host or IP address. This makes it difficult for organizing findings by type of vulnerability. This script is a little more complicated than "nsfix", but probably more useful. Enjoy.

nscross.py

(I reserve the right to be somewhat embarrassed if the Nessus experts come out of the woodwork with an option to do this, too, from the Nessus GUI..)

Wednesday, August 11, 2010

Nessus False Positives Getting Underfoot?

So.. After you've run the scan, you've found yet another false positive in Nessus due to the idiosyncracies of your environment. Here is a script to purge a particular plugin from a Nessus report so you don't have to redo the scan after fixing your scan parameters.

nsfix.py

This may work on OpenVAS reports, let me know if it causes a problem. As always, improvements are welcome.

Updated: pauldotcom from Twitter makes an excellent point that this can be achieved using the "Report Filters" interface. I blame my fear of flash guis for not finding this.

Monday, July 19, 2010

Cross-Platform Raw Character Input in Python

Handy trick for Python hackers who need to grab a keypress from the terminal but don't want to get bogged down in Curses.

getch.py

Tuesday, June 22, 2010

Using AMAP to Cross-Check NMAP

So, your NMAP results gives you a good list of open ports, but it is obvious that NMAP has lost its mind, trying to figure out what service you are looking at? Sounds like a good time to fire up AMAP, but there's all these ports to type..

Well, it's a common enough problem for me that I wrote a script. (Which means it has happened at least twice; it doesn't take much to provoke me into automating a problem.)

namap.py

Use it in good health, and much thanks to the devs of both NMAP and AMAP for writing nice, orthogonal tools with bizarre interfaces that require glue scripts like this..

Sunday, June 13, 2010

It's That Time Again..

Wes and I are preparing to send off an ISO for a new version of MalNet in preparation for HitB Amsterdam. Because I am a relentless tease, here is a small screenshot of the new LiveCD:



Still using OpenBox and Conky for the desktop, we've moved to Ubuntu Lucid Lynx for the operating system, and there's a whole load of fun new goodies for malware analysts. Even better, we are also including the source in this one, so put the python decompiler down and back away slowly.

Monday, May 31, 2010

Forcing Block Devices to Re-Read Partitions Using IOCTL

Got a block device in module that stubbornly refuses to produce dependent partition devices in Linux? (Looking at you, NBD..)


#include <fcntl.h>
#include <sys/mount.h>

int main( int argc, char** argv ){
if( argc != 2 ) return -1;
int f = open( argv[1], O_RDONLY );
if( f < 1 ) return -2;
return ioctl( f, BLKRRPART );
}


That or you could just sleep for a while, checking for the existence of /dev/nbdXpX -- but I am an impatient bastard..

Wednesday, May 5, 2010

Tactical Use of Symbolic Links in Code Review

Need an index of all files that contains a given regex nice and need for review purposes? (Like, cough, strcpy?)
find . -type f -exec grep -l strcpy \{} \; | sed -s s:^./:$PWD: >STRCPY_INDEX
mkdir STRCPY
ln -sf $(cat STRCPY_INDEX) STRCPY

Monday, May 3, 2010

More Fun With the Malware Analysis Environment (MalNet)

Last fall I put together a LiveCD to support Wes Brown's Malware Analysis Workshop at Hack in the Box Malaysia 2009 using Debian, a lot of bailing wire, and some duct tape. The disc has attracted some attention, especially at B-Sides, but is not distributable for several reasons:
  • It is a sealed box; any updates you make disappear when someone pushes the pretty red button.

  • It requires a Windows Virtual Machine; no, we cannot give you ours.

  • If Debian Stable did not like your video card, neither did our LiveCD.

  • Ditto for your network card. Well, triple for your network card. Who in the audience did not bring a 3c905-TX NIC, please raise your hands?


The latest Ubuntu release, Lucid Lynx, fixes the last two problems. That is a big deal for me, as the lack of good NVidia and ATI support was a problem for me as well as some participants. Ubuntu's LiveCD seems to do the right thing, which is great news for me. The second problem is a big one, and comes down to a need to document the work required for building a virtual machine that can be instrumented by our tools. And, like any large and boring problem, I am going to ignore it.

But, I think the first one will be fun to solve. It starts with stealing a page from WaspVM and MOSREF and building a metacircular environment. The Malware Analysis Environment should be able to serialize itself to either an ISO9660 filesystem or a USB flash drive as needed, and boot from either of those two source. It should also be able to "checkpoint" changes to the filesystem and load them up as overlays -- a trick borrowed from my customizations of Finnix which never saw the light of day.

Combine those two tools, and it should be possible for analysts using MalNet to customize their environment, install the One True Editor, or even download updates. Maybe, if I'm really lucky, I can even factor myself out of the day to day maintenance. More time to start new pet projects is always good.

So far, I have converting from a CDROM or ISO filesystem to USB figured out and working nicely. Converting backwards should follow soon behind -- this is just flopping between syslinux and isolinux using either block devices or loop mounted files. Next up is figuring out how to trick Casper into checkpointing to the boot drive or committing the time to actually writing a serialize-to-squashfs script of my own.

Saturday, April 17, 2010

Where Scott Inserts Foot in Mouth at Notacon

So.. Preview night.. I am a little twitchy after previewing NoSpex without a slide stack.. There is a really off the wall preview for a presentation on "Building the Digital City" by er.. Some guy. I didn't catch the name; the premise is very 40,000 foot, and as a pragmatic hacker, I had no clue what he was getting at. There was, however, a question about why the flat encyclopedia model took over the digital media world.

So, I had two immediate ideas. The first was that "article content" is really low barrier to entry. Anyone who paid attention in English class knows how to compose paragraphs and express an idea in bare text. I sat on that one, defending ASCII text seemed like a losing proposition. So the other, which I thought would be sympathetic, was decrying the death of HyperCard, which was the first moderately successful rich authoring environment in my mind. (Doesn't hurt that there was an "Apple is Evil" comment earlier stuck in my head.)

It wasn't until the next day, in a conversation with Mark Schumann that I understood why the presenter gave me an odd look. Turns out he was Marc Canter, one of the bright minds from the original Macromedia. So.. Ahem.. Making friends at Notacon 7!

Where Scott Whines About SecDev Burnout..

So, I spent last 45 days spending all of my out-of-band coding time working on NoSpex -- a realtime graphing library for process display and analysis. That is my karmic punishment for jokingly suggesting "Hey, I could graph threads talking to each other in my recent reverse engineering project" for a proposal in response to Notacon 7.

The presentation was way too early for me, a west coaster in Ohio, and seemed too early for the con in general. I appreciate N7's staff for having me, I was not too friendly in the whole proposal process, so I deserved that "first slot on the first day" spot. That said, having spent 45 days working on something almost as complicated as the first rounds of Mosquito or IPAF to an audience of 20 was pretty disappointing.

I am going to dedicate my out-of-band time for the next month or two to game development; maybe a Seven Day Roguelike. It is not very well timed, with Blackhat and DefCon's Call for Papers windows opening up, but a little fun is in order.

Where Scott Creates a New Blog..

.. again. With my current work load at IOActive, it is obvious that I'm not going to have time to touch WaspVM for a while. This means that my usual outlet, WaspVM Developments has gone stale. I want to keep WaspDev focussed on improvments to WaspVM and MOSREF, so I have decided to create a new journal for my other projects and personal commentary.