Exploiting the Ubiquisys/SFR femtocell webserver (wsal/shttpd/mongoose/yassl embedded webserver)Posted by Nico Golde in
As a part of our research on the SFR femtocell I had the pleasure to look for a vulnerability
that might assist us in compromising remote devices. One of the obvious software targets of the box has been the webserver (wsal) that is used to serve some web pages used for configuring the device. As all other services on the box, it runs with root privileges. The device itself runs a Linux 2.6.18-ubi-sys-V2.0.17 on an ARM926EJ (ARMv5). The bug (CVE-2011-2900): I started reversing the binary when at some point Kevin pointed out a string in the binary that hinted towards the Open Source project shttpd (which has been relabeled in mongoose at some point and that is also the basis for the yassl embedded webserver. So this made things a lot easier. As the web service is fairly powerful (including CGI, SSI support) I first looked for non-software related bugs. From shttpd.c/defs.h:
Hmm, that's already more methods than expected. So it made sense to look at those methods. As the webserver can execute CGI I assumed PUT might be interesting in order to push stuff onto the device and execute it. However, it turned out that the web directory is mounted read-only (and the code gracefully handles path traversal attempts). DELETE died for the same reason and it seemed unlikely that this would result in code execution anyway. Back to software vulnerabilities and the PUT functionality. Let's have a look at the function handling PUT requests (io_dir.c/put_dir()):
The function is pretty simple. It loops over the URL path and tries to create each directory of the complete path (Similar to mkdir -p). To do that, the path chunk is copied into the stack buffer buf before it is passed to stat and mkdir. The len argument of the memcpy operation is determined by the distance between two consecutive / characters. Assuming that path can be longer than FILENAME_MAX (+/- a few bytes overhead for the rest of the URL), this is a classical stack-based buffer overflow and seemed like a nice candidate for code execution. In this code snippet the len argument is guarded to not overflow (assert statement). However, assert is only in place if the binary was not compiled with -DNDEBUG, right? But let's check this... The following output is generated using the radare. If you're on linux, you need a multi-arch reversing tool chain (with unix philosophy in mind) and you can't or don't want to use IDA, I can highly recommend looking at this tool (even though it's still work-in-progress).
As we can see, we see nothing. In particular, no comparison and no call to __assert_fail. So we're lucky, looks like we found our candidate for code execution. A pretty simple standard buffer overflow. Interestingly, the shttpd Makefile even mentions -NDEBUG in order to save ~5kB binary size (remember, this is an embedded device). Let's look at how put_dir returns so we can get control over the program flow. At the function entry registers r4-r7 and the link-register are pushed onto the stack. Leaving looks similar with the difference that the link-register isn't used, but the return value is directly popped into pc.
The pc register is equivalent to EIP on x86 with the difference that you can directly read and write to it. As it is popped from our overflown stack-buffer, this would give us direct control over the program flow. Now the interesting question was, does wsal also support this request type or is it not calling this function?
This made it clear that the wsal binary also supports PUT. Looking at shttpd.c, it seems that PUT as well as DELETE should only be enabled for authorized users (which probably wouldn't be a big problem), but funnily the Makefile also states: # -DNO_AUTH - disable authorization support (-4kb) which was of course also set by wsal Exploitation: Exploitation of this seemed rather straight forward given the nature of this bug. The stack was marked non-executable in the ELF binary, but fortunately the ARMv5 doesn't support the XN bit yet. However, experimenting with this bug I noticed fairly quickly that ASLR is enabled on the device and our stack address is randomized. As a result, I couldn't just place my shellcode into buf and jump right to it. ROP would've been an option, but as my ARM knowledge was limited before playing with this bug, I didn't like this option (even though as we will see, I need it anyway, but not for the actual payload). Return-to-libc, by e.g. returning to system(), was no interesting option either, as the there is no network binary such as netcat installed on the box. So I had to find something else. And as it turned out, the support for heap randomization as well as library randomization starts pretty late on ARM. As Kees points out this started in 2.6.37. This nails down one possible problem. As path was not the original request buffer, but only a copy of it, I started looking for copies of my input or the possibility to put the payload somewhere else (e.g. a POST body, HTTP headers...). First, I checked where path is coming from (shttpd.c/decide_what_to_do()):
There we go, path originates from c->uri which is an url-decoded form of itself. One important thing we have to take into account at this point is that the URL can't be of arbitrary length, but is checked against URI_MAX. We have to overflow a buffer in put_dir() with a length of FILENAME_MAX... However, we are lucky, URI_MAX is defined as 16384 (config.h) while FILENAME_MAX from put_dir is an alias for MAX_PATH which is defined as 4096. So where is c->uri coming from? Again we look at shttpd.c, this time the parse_http_request() function:
As we can see, c->uri is allocated on the heap and as I mentioned, heap randomization was introduced pretty late on ARM/Linux, I assumed I can just jump right into the heap copy of my input. There is a nice side-effect of using the heap copy of the buffer to place our shellcode. Because url_decode() is called on the complete uri length, we have no restrictions whatsoever regarding the bytes we can include in our final shellcode, it can include zeros and the-like in url-encoded form. Anyway, few minutes later it became clear that I can't just jump right to it
While the leading zero itself was no a problem for the input itself (because I can just urlencode this), put_dir has a problem with that. If we recall, the loop is using So if we include a zero before the terminating / in the URL to jump to our heap buffer, our buffer overflow will actually never happen. However, the path copy that is passed to put_dir() is created using snprintf() and this is little-endian. Therefore, we can include one zero in the url-decoded, stack-based path buffer (in decide_what_to_do()) and pop the address including the zero from there. It just has to be past the / character that we need to get a large len value. How do we pop it from there after our buffer was overwritten and the stack frame of put_dir() was teared down? Here is where some ROP is needed (or call it jump-oriented). When the put_dir() function is left, the stack pointer is below the path stack buffer that was passed as an address to the put_dir() function (from where it was copied into the stack buffer over put_dir) and is as well already url-decoded. So if we can lift our stack pointer back up, it should be possible to pop an address with a leading zero from this buffer. Looking at the mentioned program map output, it is visible that libc and libgcc are mapped at addresses without a leading zero. Their base is also not randomized. I didn't have any particular tool to find ROP snippets, but as on ARM all instructions are word aligned, it was easy to find proper instructions with objectdump and grep. In particular objdump -d /lib/libc-2.3.6.so | grep -A 2 -E 'add sp, sp,.*' | grep -B 2 -E 'pop.*(pc|lr)' (can also be done with radare if you're more advanced in usin it than i am This way I searched for stack lifting instructions followed by an instruction that pops stack buffer content to pc or the link register in order to regain control. I found a good candidate:
This was perfect. Now I could just make my first jump to this snippet, lift the stack pointer back into my buffer, place the address of sigprocmask+108 url-encoded in my buffer (together with fake r4-r7 values) and lift the stack until I'm past the / character and pop my zero-address from there. The goal was still to jump to the shellcode in the heap copy of the buffer. The ARM-stacle: This would work well, if the target architecture wouldn't be ARM. There is an important constraint on ARM when writing exploits. Unlike x86, ARM is based on the Harvard Architecture. This means that code and data cache are separated. I didn't know this first. A result of this was that when hitting my heap shellcode, the program crashed with a SIGILL. However, analyzing the coredump and the pc at that time always showed correct instructions. Due to the Harvard Architecture, my shellcode is copied into the data cache. But in order to execute it, it needs to land in the data cache and thus written back to main memory. Because it wasn't the coredump displayed instructions that weren't actually in the data cache and thus resulting in SIGILL due to whatever was executed as instructions at this point. It turns out that there are two solutions two this problem. The first one is a simple instruction (MCR). However, it is limited to kernel mode. The other option is a clear cache syscall that takes 3 arguments, a start address, a range and flags. This seemed nice. What was even more nice is that the wsal links against libgcc which provides a wrapper to do that:
Crafting the 0x009f0002 by ROP would've been a bit painful I suppose so this wrapper was nice. So before jumping to our shellcode, we need to call this syscall. A small excerpt from linux-2.6/arch/arm/traps.c to better understand this syscall:
Some places suggest that you can pass 0 as a start and -1 (0xffffffff) as a range to this syscall and flush everything. However, this doesn't seem to work and looking at this function I also don't understand why it should. find_vma()(from mmap.c) will traverse the internal tree representation of the kernel until it finds the first virtual memory area that satisfies start < vma->vm_start. So if the start address is zero, this should hardly ever end up in the area of attacker controlled payload (unless you are very lucky). Also flushing the complete memory range doesn't work. As we see end will be set to vma->vm_end if it is bigger than the actual vma end. To sum up, we really need proper values. We need a heap address lower or equal than our shellcode address in r1 and a length larger than our payload in r2. As __clear_cache() returns using the link register, we furthermore have to fill that with a proper value to regain control after flushing the cache. So the plan is: overflow the buffer, lift our stack to a place where we can pop arbitrary addresses (these two steps could also be exchanged), flush the cache, jump to shellcode. The following shows the required ROP sequences to perform this. Searching these instructions was also simply done using objdump and grep:
Mission accomplished. The used shellcode then executes a connect-back shell! As a result, this is a remote root for SFR femtocells. The complete exploit is available here It needs slight modification in case you modified your firmware e.g. with library hooking.... As mentioned before, depending on how shttpd/mongoose/yassl embedded webserver have been compiled, they may be affected by the problem itself. The exact code for them differs slightly, but all of them contain the same bug if compiled with the right options. Slides of our presentation: http://femto.sec.t-labs.tu-berlin.de/bh2011.pdf UPDATE: it seems they have fixed the issue in the latest firmware release (V2.0.24.1) by disabling the PUT functionality completely So what happened recently...Posted by Nico Golde in
I felt the need to do a short writeup of what I actually did in the last time since I became fairly quiet in some parts of the net.
During the time I was mostly busy with working on my diploma thesis (I will hopefully rework my homepage soon and also upload the thesis pdf then) titled SMS Vulnerability Analysis on Feature Phones. During this study I was working on a modified version of OpenBSC (thanks to the great people developing this at this point!) that allows me to do over-the-air fuzzing of the short message service on so-called feature phones. The study aimed to not only look at one specific phone model for testing but also do a large scale analysis of the big players in that market section. This has been interesting to us as SMS is known to be problematic from the past, feature phones are widely deployed on the market (compared to only ~16% smartphones, even though uprising), and it is not possible (or let's say not feasible if you want to test a large number of devices without patching the firmware blobs) to modify the underlying operating system for testing. The application platforms are less integrated into the operating system, have less abilities to interact with other applications on the phone, and have far less advanced APIs compared to open APIs on smartphones. Smartphones often provide the ability to run native code. During the work I found bugs for all tested manufacturers (Nokia,Motorola,LG,Sony Ericsson,Samsung,Micromax (3rd biggest manufacturer in india)). A large part of this work is the result of a talk with my colleague Collin Mulliner at the 27C3 congress and CanSecWest. SMS-o-Death: from analyzing to attacking mobile phones on a large scale (slides) Both conferences have been excellent (even though pretty different). Thanks to Dragos for organizing CSW, it was a blast! I also had the chance to visit TROOPERS. Although being a fairly young and small security conference (organized by ERNW), a pretty good one (in terms of people, overall atmosphere and also talks) and definitely worth a visit! Being finished with my studies (well I don't have the official certificate yet) I will now look forward to work in a PhD position at the department I already work at, SecT. I will probably look into mobile handset security, system security and security of "modern" mobile communication systems (such as GSM,UMTS,...). I'm not really interested in the title at the end of the PhD, but working in this area and especially at university has been lots of fun to me (recently playing with femtocells) so far, so I try to keep it that way That's it for the update on what has been going on. P.S. I finally failed to resist and you can now as well follow me on twitter @iamnion
Comments (0)
- Trackbacks (0)
Defined tags for this entry: conferences, mobile phones, personal, random blurb, security, sms, software
Debian 6.0 ‘squeeze’Posted by Nico Golde in
exim remote vulnerabilityPosted by Nico Golde in
It appears there is a remote vulnerability in exim with the possibility to escalate privileges to root, some details on http://www.exim.org/lurker/message/20101207.215955.bb32d4f2.en.html. Security teams are currently looking into the issue.
SCNR, but this is your chance to switch to a better alternative UPDATE: patch for the buffer overflow (CVE-2010-4344): http://git.exim.org/exim.git/commitdiff/24c929a27415c7cfc7126c47e4cad39acf3efa6b A few additional patches will be applied to fix the privilege escalation and other things. Will my Phone Show An Unencrypted Connection?Posted by Nico Golde in
The GSM security hype is all over the place and certainly the specifications are currently totally ripped in pieces. Some of the common attacks against mobile phones for example man-in-the-middle scenarios using an IMSI-catcher base on an attacker forcing you to downgrade to a weaker cipher mode or a mode with no ciphering at all. Now the question arises, is a user noticing this change? According to GSM 02.07 there seems to be an indicator that should allow the user to see if ciphering is turned off or on. Dieter Spaar did some tests to find out which mobile phones indicate this and which not. The results are actually pretty interesting (and shocking), a lot of them don't.
The list is not that huge so far but I think it's a pretty good start and from what I've seen lately the manufacturs are more interesting than a specific phone model. A lot stuff besides the typical user interfaces, eye-candy and hardware does not change between different models. It would be also interesting to see how those phones actually indicate it. I personally haven't seen such an indicator yet so I'm not sure if it's some unknown tiny symbol which is probably meaningless to a user or not. Results are now also collected at: http://security.osmocom.org/trac/wiki/WillMyPhoneShowAnUnencryptetConnection which is part of a new wiki page that aims to collect all the known GSM security problems. This is also a part of the awesome osmocom-BB project. smpCTF 2010 quals writeupsPosted by Nico Golde in
I participated together with some friends in this years edition of the smpCTF quals (actually it took place for the first time). Since we also qualified for the finals we had to submit a writeup of all challenges. For those who are interested, our submission is located on: http://nion.modprobe.de/smpctf/smpctf.html.
All in all I had fun during this weekend but I also have to say that I've had more at other CTFs in the past. What disappointed me especially is that I'm aware of at least 2 challenges that seem to be only slight alterations of challenges from the DEFCON and Codegate quals. I also missed creativity when it comes to the binary exploitation challenges, most of them have not been challenging. But as said, I enjoyed this weekend, had lots of fun and a big plus was the radio stream during the competition with support from dubstep.fm Anyway, congrats to team nibbles who've won the CTF protocol design fail: MMS notificationPosted by Nico Golde in
I was just looking into some specifications of the openmobilealliance when I got the content for todays WTF moment.
An MMS notification is usually sent over SMS and encodes various fields including the location of where the MMS content is located so the mobile phone can download it via e.g. WAP. Now looking at WAP-209-MMSEncapsulation-20020105-a chapter 6.2. (Multimedia Message Notification) there's an interesting header field included in these notifications, X-Mms -Message-Size Mandatory.Clearly the people who developed this must have taken some bad drugs. Adding a length field value to a header and allow it to be based on an approximation rather than an exact value just doesn't explain itself to me. acrobat reader stealing my passwordsPosted by Nico Golde in
I know there is some setting in adobe acrobat reader to switch of monitoring of the X paste buffer (which I couldn't find now) and it seems one really wants that. I was very surprised today when I tried to paste a password using pwsafe and observed the following:
$ pwsafe -p fandango Enter passphrase for /home/nion/.pwsafe.dat: You are ready to paste the password for hosts.fandango from PRIMARY and CLIPBOARD Press any key when done Sending password for hosts.fandango to acroread@hostname via CLIPBOARD So apparently acrobat reader is stealing my password from the X paste buffer if the application is running. Especially given all the javascript, malicious pdf file kungfu that is around these days I of course don't find this very amusing. Lesson learned: Use xpdf whenever I can even though it really lacks features :/ UnrealIRCd backdooredPosted by Nico Golde in
The UnrealIRCd team has just published an advisory advisory stating their release has been backdoored. From the advisory:
We found out that the Unreal3.2.8.1.tar.gz file on our mirrors has been I'm personally not using this software but this is probably a shock for lots of sysadmins as this is one of the most popular IRC server applications. The last sentence of this quote is the most shocking to me. This slipped through the cracks for about 8 months without being noticed! This shows yet another time that upstream developers need to think about providing ways to allow users to properly verify the integrity of their releases and (which is probably more important) users need to verify what they download. There is no point in md5 and friends being broken if nobody cares for hashes anyway. The UnrealIRCd people seemed to have learned their lesson and will start PGP/GPG signing their releases from now on. Hopefully their users verify their tarballs then. So what was the backdoor exactly about? It didn't take me much time to find a backdoored tarball, "gladly" there are still lots of websites mirroring backdoored tarballs. The backdoor is pretty small, simple and efficient, a full diff can be found here. Only two files have been modified, the first one is the important one: s_bsc.c, function read_packet():
This is the important function to handle client connection data and processes all client data. the modification are the 4 lines at the end. The code is simple. The first two bytes of readbuf are compared with DEBUGMODE3_INFO. readbuf is used a few lines before to read data from the client connection. So basically this introduces a new irc "command" DEBUGMODE3_INFO. DEBUGMODE3_INFO is defined as AB in include/struct.h. If the received bytes match AB DEBUG3_LOG is called with the read buffer as argument. DEBUG3_LOG is just another macro that resolves to DEBUG3_DOLOG_SYSTEM (defined in the same file) which looks like:
So this allows an attacker to connect to the irc server and execute arbitrary commands by using the AB comment. This is probably the most simple backdoor one can think of but it's rather efficient and unlikely to be hit by accident from a client. Bad days for UnrealIRCd and there are still many servers out there which are probably backdoored this way, at least it didn't cost me much time to find some :/ fail of the day: operaPosted by Nico Golde in
I occasionally make use of the report function in opera in case it crashes (which happens quite often on 64bit for me), but if it crashes right when receiving the response
of the crash reporting website you really start to HATE that piece of software. ![]() (notice Last visited URL) FAIL! (using 0.60-6351)
(Page 1 of 48, totaling 478 entries)
» next page
|
Calendar
QuicksearchSupportRecent EntriesExploiting the Ubiquisys/SFR femtocell webserver (wsal/shttpd/mongoose/yassl embedded webserver)
Wednesday, August 3 2011 So what happened recently... Wednesday, April 6 2011 Debian 6.0 ‘squeeze’ Sunday, February 6 2011 exim remote vulnerability Thursday, December 9 2010 Will my Phone Show An Unencrypted Connection? Wednesday, September 8 2010 smpCTF 2010 quals writeups Sunday, August 8 2010 protocol design fail: MMS notification Wednesday, July 28 2010 acrobat reader stealing my passwords Tuesday, June 29 2010 UnrealIRCd backdoored Saturday, June 12 2010 fail of the day: opera Wednesday, June 9 2010 Syndicate This BlogCategoriesTag cloud23c3 acpi advertising annouce announce argh art awards bash blogging bugs c cli code conferences config configuration copyright data mining debconf debian dell dns documentation email errm? events exploit fail fail2ban filesharing films flame fun gcc google graphs grml hacking hacks hardware heise images information installation internet irc knowledge libacpi links linux mobile phones monitoring network networking news newsbeuter openoffice open source opera passwords php phrack piratebay power programming qa random blurb rant release releases rss scripts security service setup shell software spam ssh stfl stuff tests text mode tip tips tools troubleshooting unix user video vim.editing web websites wikipedia wordpress wtf www youtube zsh
|
|||||||||||||||||||||||||||||||||||||||||||||||||
