Hooked on Packets: Reading PCAPs for D Students - Preview

SOURCE Boston is coming up in April, and Mike Ryan and I aregiving a presentation about making packet analysis easier for the masses. Oneof the challenges with building new protocol parsers for tools such as Ettercap and Wireshark is many of these parsers are written in C. C is very fast andpowerful, however you run the risk of introducing vulnerabilities in thesoftware. And, for folks who aren't fluent C programmers, building theseparsers can be intimidating.

So, what are we going to do to change this? One of thepieces we've been working on is trying to merge Lua into Ettercap. We did apresentation at Derbycon last year about how we planned to do this, and now wehave some practical uses. We're going to take a look at how to build easy touse scripts, similar to the Nmap NSE scripts, to allow manipulation and parsingof data that would otherwise require C code.

I wrote a quick example that we will examine in more detailduring our presentation. The example focuses on tracking HTTP traffic on thenetwork or in captures.

Why do we care about HTTP requests? On a penetration test, we may use a MitMattack to target groups of individuals and watch their requests in order toidentify interesting targets for session theft or other attacks. Another goodexample of when this is useful is when looking at malware. We may want todetermine what requests malware is making, where it's being redirected to, andeven watch for beaconing requests.

It turns out, doing these things with existing tools can bepretty cumbersome. Right now, you could try to match requests and replies intcpdump. But, if there are multiple simultaneous requests, it gets trickier. You can do the same thing in Wireshark andTshark, but you have to do a lot of clicking around-- or write some lua tore-assemble the sessions and then track down the data.

Our solution adds a basic registry to Ettercap. So, you canstore session data. To create a session we can do something like this:

-- Get session key for tracking req->reply pairs local session_id = http.session_id(p,hobj)

-- We have a session, lets get our registry space local reg = ettercap.reg.create_namespace(session_id)

Now, we have a local registry that is unique to our session.We can use this registry to store session data. One thing we may want to do is extract request data, and store it untilthe response comes in so we can match them up. To do something like this, we can parse the packet:

-- Parse the packet (p) and retrieve the http data

local hobj = http.parse_http(p)

If we are looking at a request, we want to store the requestfor retrieval in the registry for when we see the response:

if hobj.request then

reg.request = hobj

If we saw a response, then we just want to print it out:

      -- Get the status code      local code = hobj.status_code      -- Build the request URL      -- If we have a 2XX or 4XX or 5XX code, we won't need to log redirect      -- so just log the request and code      if code >= 200 and code < 300 or code >= 400 then         ettercap.log("HTTP_REQ: %s:%d -> %s:%d %s %s %d (%s)\n",            packet.dst_ip(p),            packet.dst_port(p),             packet.src_ip(p),             packet.src_port(p),             reg.request.verb ,reg.request.url , hobj.status_code, hobj.status_msg)

Dealing with the request and matching response is easy inthis model. And we can easily add parsers for other types of traffic thataren't supported by Ettercap and Wireshark. When you put it all together, itgets very easy to track requests made by a browser in a MitM attack or fromnetwork capture files. Here's an example of how the initial requests from viewingSlashdot look, using our version of Ettercap with Lua modules enabled:

Screen shot 2013-03-25 at 11.39.53 PM

Note: we are printing both source and destination, the fullrequest, status code, and in the case of a redirect, where the user wassent. This is just a brief example ofthe types of things that are going to be easy to build with this framework.

We will be releasing the source code for a patched Ettercapalong with sample modules at SOURCE Boston.

If you're interesting in helping out or learning more, comesee us and the other SpiderLabs folks at SOURCE Boston and chat with us aboutyour ideas.

Trustwave reserves the right to review all comments in the discussion below. Please note that for security and other reasons, we may not approve comments containing links.