Spare Clock Cycles Hacking is freedom.

10Jun/1011

Sergio Proxy – Injecting, Modifying, and Blocking HTTP Traffic

Edit: You can grab new releases of this tool here: https://code.google.com/p/sergio-proxy/downloads/list

I've gotten all settled in my new place (finally), so I figured I should get caught up on my blog again. Lots of posts coming soon, I promise!

Today, I'm releasing a tool that I'll be working on (and with) a lot this summer that I'm calling Sergio Proxy: a Super Effective Regexer of Gathered Inputs and Outputs (download here). Yeah, yeah, I know: there are a billion other HTTP proxies out there that are way better than mine and I should use. I know all about them; I just wanted to make one myself. It's an interesting project, and I've learned a lot about HTTP proxies and the Twisted networking framework in the process. In addition, this tool has made it *much* easier for me to use Python to work with data captured from MITM'd HTTP connections than the other tools that I experimented with. I also blindly stumbled into an awesome topic for my next post too, to be released in the next few days (I'm particularly excited for this one). A big fat warning before we go on though: this is a very alpha release, so don't have your hopes way up. It still has a long way to go (like adding HTTPS support...:P).

So why did I originally start on this project? Mainly because Ettercap filters suck. I mean, really suck. Now, don't get me wrong, they can be useful in some situations, but automatically injecting data into HTTP sessions is not one of them.  So why did I need Ettercap filters to inject data into HTTP sessions? Why, to attack SMB servers by means of challenge-hash cracking of course.

The attack works by injecting specially crafted HTML into a page that the victim is requesting that references a file located on a local samba server.  The browser will then automatically try to authenticate with the remote server using its current user's credentials, exposing challenge hashes of the user's password on the wire, the first 7 characters of which can then be cracked with rainbow tables.  To do this, we obviously need to first be in a position where we can modify the network traffic, which we can easily do using various methods (the most popular probably being ARP poisoning, but Ettercap has a number to chose from). However, once we have our MITM attack working, we are still presented with the problem of injecting our content into the HTML file.

In all the examples I have seen, the attack used Ettercap filters to do this injection, or an email with embedded HTML. Unfortunately, neither is very unreliable.  All the filters that I tried seemed to corrupt the pages in the best case. In the worst case, it even prevented the victim from accessing certain servers, as the attack would try to kill gzip support on servers that required it. Obviously, this kind of degradation would be noticed even by the common computer user. As for the email, you still generally need to trick the user into viewing it before the attack will work. Not incredibly hard, but slower and still less reliable than a forced HTML injection.

After looking through some Wireshark captures, it seemed that the root problem in the Ettercap method was that the HTTP content length wasn't being modified, confusing the browser when it got more  data than it was expecting. While it was probably possible to work around that in a similar manner as the gzip compression was broken, it wouldn't solve the heart of the problem: that trying to modify HTTP traffic at the TCP level is neither effective nor powerful.

Enter Sergio. It was obvious that what I was looking for was a forced, transparent HTTP proxy. I looked at some other proxies, and I probably could have adapted them to my purposes pretty easily. However, I decided I would rather code one myself, and get familiar with the inner workings of these tools, and to let me do everything in Python. I also have had my eye on the Twisted framework for awhile, and figured that this would be the perfect opportunity to familiarize myself with it.

To run this attack, all you need to do is run the included start_smbchall.py file. However, Sergio isn't just limited to evoking SMB authentication attempts of course. Sergio can inject, modify, and delete any content going through a victim's HTTP sessions, meaning that we can do much, much more with it than just this attack. In addition to this SMB fun, just for kicks, I also implemented my version of the classic Upsidedownternet :P . Beyond these implemented attacks, Sergio makes it easy to insert some malicious Javascript,  replace all the links on the page with links to a malicious site, prevent the victim from accessing any update sites, redirect them to malicious update sites (more on this later), monitor/record all the traffic going over the connection, or (a fun one) replace any exe file being downloaded with our own, backdoored, malicious executable. Unfortunately, I haven't gotten a chance to actually implement these attacks yet (lame), but I'll have a release soon enough with them included.

Anyway, now that you know the capabilities, here's how to use it. I promise, it's easy. You'll only need two files, start_proxy.py and UserMITM.py, and the module sergio_proxy installed somewhere in your PYTHONPATH (when in doubt, just throw it in a subdirectory named sergio_proxy). Example start_proxy and UserMITM files are included in the tarball in the examples folder. Basically, you just create a subclass of the included MITM class in UserMITM and add your own attacks into it. Then, when creating and starting your transparent proxy, you set UserMITM as transparent_proxy's new MITM instance, and you're ready to go. Pretty straightforward.

If you want to know more about what my proxy does and does not do, read the README. Here's the short of it though: does HTTP 1.0, does not do 1.1 (yet), does not MITM HTTPS (yet). Sorry if this is disappointing, but they weren't critical to my initial attack, and I will of course get these things fixed ASAP.

So that's Sergio Proxy. I could talk more, but it's probably easier for you to just download it and mess around. And please, if you implement some attacks with it, submit them! I'd be happy to add them in. As I mentioned earlier, I should be back in a few days will an interesting application of my tool. Until then, keep hacking.

Comments (11) Trackbacks (1)
  1. Glad to see you finally got a working product going.

  2. What do you mean, finally? I’ve had it working for forever, just hadn’t gotten around to posting it…cause I’m lazy like that…

  3. Interesting tool, did you ever update this.

    I get the following error when I run this:

    root@ubuntu:~/Downloads/sergio_proxy# python start_smbchall.py
    Imported Request Functions:
    Imported Reply Functions:
    Traceback (most recent call last):
    File “start_smbchall.py”, line 21, in
    from sergio_proxy import transparent_proxy
    File “/root/Downloads/sergio_proxy/sergio_proxy/transparent_proxy.py”, line 18, in
    import proxy
    File “/root/Downloads/sergio_proxy/sergio_proxy/proxy.py”, line 35, in
    class TransparentProxyClient(proxy.ProxyClient):
    AttributeError: ‘module’ object has no attribute ‘ProxyClient’
    root@ubuntu:~/Downloads/sergio_proxy#

    Ubuntu 10.10

  4. @james,

    Thanks for letting me know about this! Not sure what I did exactly, but at some point a slightly older copy got uploaded. Should have been doing “from twisted.web import proxy” rather than “import proxy”, as I fixed the portions where I needed to override the proxy class. Let me know if the new source works for you, just use the same link to grab it.

    I haven’t really updated Sergio Proxy though since this post sadly, didn’t have as much time over the summer as I had hoped. I will be getting back around to it at some point though, it was a fun project. If you do anything cool with it, let me know!

  5. I did not mention this early, but I find all your articles extremely interesting and I am very surprised that you are not getting a lot more mention for your work from other sources..
    I was able to get the script working properly. Just like you I have been frustrated with ettercap filters.
    I am not a programmer so it will take me some time to figure out how to adapt it for various needs.
    But I think you already came up with all the good ideas, which is basically to manipulate network traffic so that all kinds of attacks are possible.
    Whether it is a Java script injection for example the BeEf framework, Evilgrade, Metasploit, these should all be a lot more effective with the work that you have done.

    For now I just want to inject a simple invisible iframe that redirects traffic to the metasploit server. Have not been able to do that with ettercap.

    For the SMB example (start_smbchall.py) to make it easier for others use, you might want to have a place to declare the network interface so that other interfaces can be used:

    iface =”wlan0″

    iptbl_enable = “iptables -t nat -A PREROUTING -i %s -p tcp –dport 80 -j REDIRECT –to-port 8080″ % (iface)

    ettr_start = “%s -T -o -i %s -M arp /%s/ /%s/” % (etter_loc,iface,target1,target2)

    Thank you for your excellent work!

  6. For Backtrack Users:

    Install Python 2.6 on Ubuntu 8.04 LTS

    – Add to /etc/apt/sources.list:

    deb http://ppa.launchpad.net/python-dev/ppa/ubuntu hardy main
    deb-src http://ppa.launchpad.net/python-dev/ppa/ubuntu hardy main

    – Import the pub key:

    apt-key adv –keyserver keyserver.ubuntu.com –recv-keys D81367B9

    – Install python2.6:

    apt-get install python2.6

    – You need to install the distribution packages
    – I just copied mine from /etc/lib/python2.5/site-packages to /etc/lib/python2.6/dist-packages

    – To run a script: put python2.6 in front, for example:
    python2.6 ./UserMITM.py

  7. hi im having error like this

    /usr/lib/python2.6/dist-packages/twisted/internet/_sslverify.py:4: DeprecationWarning: the md5 module is deprecated; use hashlib instead
    import itertools, md5
    Imported Request Functions:
    Imported Reply Functions:

    how to fix this?

  8. sorrrrry i forgot to mention im using backtrack 4 r2

    i follow what james did.. but getting error like that

  9. @akshah Code should still work, that’s just a warning. Python is just letting you know that the md5 module (which they’re using in the twisted libs) is deprecated and that the code should be migrated to use hashlib instead.

  10. Correction the above should be:

    – You need to install the distribution packages
    – I just copied mine from /usr/lib/python2.5/site-packages to /usr/lib/python2.6/dist-packages

  11. Nice job! Just what I was looking for. I managed to get it to work in tandem with ettercap (cause you apparently kinda forgot to do the actual MITM part per se) and kernel redirection of packages so that the proxy can intercept them.
    By the way, using this it’s really easy to trick facebook.com into changing the login form-action to http instead of https. They should really fix that.


Leave a comment

(required)