Author: mark

  • Technical details and scripts of the WordPress Timthumb.php hack

    Big News [April 24th, 2012]: I’ve launched Wordfence to permanently fix your WordPress site’s security issues. Read this now.

    UPDATE: WordThumb has now been merged into TimThumb and has become TimThumb 2.0. Please head over to the TimThumb site now for updates and to get the code.

    As I mentioned yesterday my WordPress blog was hacked. The security hole has been picked up by hacker news and from there, The Register, ZDNet, PCWorld, and Geek.com among others. The publicity will hopefully get Theme developers to update timthumb.php or switch to a different thumbnail generator.

    I’ve been contacted with requests for detailed info, so I’m going to post the technical details of how my site was hacked along with the scripts that the hacker used to get in. This is targeted at a technical audience.

    The server that served you this web page is the one that was hacked. It runs Ubuntu 10.10 with all security updates installed. It is a virtual server hosted by Linode.

    I also run the latest version of WordPress.org.

    My WordPress root directory was writable, but making it read only would not have prevented the hack.

    Timthumb.php in it’s default configuration allows site visitors to load images from a predefined set of remote websites for resizing and serving. Timthumb offers a caching mechanism so that it doesn’t have to continually re-process images. The cache directory lives under the wordpress root and is accessible by visitors to the website.

    The ability for a site visitor to load content from a remote website and to make the web server write that remote content to a web accessible directory is the cause of the vulnerability in timthumb.php.

    To be clear, timthumb.php does not actually execute any remote malicious code that causes this vulnerability. This was a point of confusion among some commenters in my blog post yesterday. It simply gets a remote file and places it in a web accessible directory.

    Timthumb only allows remote content from a small range of websites to be loaded remotely. In it’s default configuration these included Blogger, WordPress.com and other sites that are writeable by the general public.

    Timthumb’s verification that remote content was only being loaded by these domains was also broken. You could for example load content from hackersiteblogspot.com or from blogspot.com.hackersite.com.

    I’ve submitted a patch that fixes the pattern matching and removed all default public hosting sites from the allowed sites list. The developer has opted to keep a small list in which I’m not in favor of.

    In my case the hacker uploaded a script to my cache directory which timthumb.php stores as “external_<md5 hash>.php”. He/she then accessed this script directly in my timthumb cache directory as something like https://markmaunder.com/wp-content/themes/Memoir/scripts/cache/external_md5hash.php

    The script uploaded was Alucar shell which is base64 encoded and decodes when it executes. That makes it a little harder to find it using grep or similar tool. You can see the encoded version of Alucar here and the decoded version of Alucar here (without the username and password preamble at the top).

    Here’s a screenshot of the UI:

    Alucar UI

    This script which gives a web based shell access was then used to inject base64 code to one of my core wordpress files wp-blog-header.php which lives in the wordpress root directory. The file with injected code looked like this.

    The decoded version of this base64 code is this. The code executes whenever a blog page is visited. It fetches a file from a URL and writes it to /tmp. Then it executes the php code that is contained in this file. In my case it simply echo’d some javascript code that would show ads. Here is the code contained in the file in /tmp.

    Again, this file is periodically updated with new PHP code, so the attacker could have his way with my server until I found out about it. The code could be altered to instead become a spam system and work it’s way through a long list of spam emails.

    The way I tracked this to conclusion was:

    • Heard audio on my blog telling me I’d won something.
    • Checked Chrome network tools and saw ad content loading and I don’t serve ads.
    • Grepped wordpress source and themes for hostname I saw in ad. Nothing.
    • Dumped mysql databases on server (all of them) and grepped for hostname. Nothing.
    • Confusion reigns.
    • Started working my way through nginx (which is my front end proxy to apache) and apache access and error logs.
    • Spotted lines in apache error log like this: “[Mon Aug 01 11:09:12 2011] [error] [client 127.0.0.1] PHP Warning: file_get_contents(http://blogger.com.zoha.vn/db/load.php): failed to open stream: HTTP request failed! in /usr/local/markmaunder/wp-content/themes/Memoir/timthumb.php on line 675”
    • Checked timthumb’s cache directory and found Alucar.
    • Realized base64 encoding is why I didn’t find anything with grep.
    • Regrepped wordpress source and database and found injection in wp-blog-header.php
    • Decoded base64 stuff and played with Alucar
    • Found tmp file in /tmp
    • Cleaned everything and fixed permissions. Ran chkrootkit and other utils on machine to see if anything else was compromised. Changed passwords, etc.

     

  • Zero Day Vulnerability in many WordPress Themes

    Big News [April 24th, 2012]: I’ve launched Wordfence to permanently fix your WordPress site’s security issues. Read this now.

    Update: WordThumb has now been merged into TimThumb and has become TimThumb 2.0. Please head over to the TimThumb site now for updates and to get the code.

    Update 4: I’ve also added the ability to screenshot websites to WordThumb.

    Update 3: I have forked timthumb.php into a new secure thumbnailer project called WordThumb. It’s a complete rewrite of timthumb and is fully backwards compatible. The only code that is recognizable is the image processing code. All file handling has been rewritten from scratch and I’ve fixed quite a few bugs. The project is now live on Google Code and version 1.0 of WordThumb is up for download. You can read more details about the changes in this blog entry about WordThumb.

    Update 2: After evaluating timthumb.php I’ve decided the best solution to the security problem is to fork the project and do a line-by-line rewrite. I started work on this a day ago and it will be published on this blog later today. (This was posted on Wednesday at 11am Pacific Time). Please check my blog’s home page this evening (in about 8 hours) and it should be done.

    Update: Ben, the developer of timthumb has been in contact and is working on a fix. His own site was hacked Friday using the same method. I’ve submitted a tiny patch and if you’re a solid PHP hacker it’d be great if you could eyeball the code with us and submit a patch (really easy to do on Google code) if you spot any other opportunities for cleanup (there are many). Given enough eyeballs… you know the quote.

    The Exec summary: An image resizing utility called timthumb.php is widely used by many WordPress themes. Google shows over 39 million results for the script name. If your WordPress theme is bundled with an unmodified timthumb.php as many commercial and free themes are, then you should immediately either remove it or edit it and set the $allowedSites array to be empty. The utility only does a partial match on hostnames allowing hackers to upload and execute arbitrary PHP code in your timthumb cache directory. I haven’t audited the rest of the code, so this may or may not fix all vulnerabilities. Also recursively grep your WordPress directory and subdirs for the base64_decode function and look out for long encoded strings to check if you’ve been compromised.

    How to fix:
    Update: As per several requests I’m posting hopefully easy to use instructions on how to fix this. This is for the latest version of timthumb.php version 1.33 available here. Check your version because there are many much older versions floating around.

    NOTE: timthumb.php is inherently insecure because it relies on being able to write files into a directory that is accessible by people visiting your website. That’s never a good idea. So if you want to be truly secure, just delete the file using “rm timthumb.php” and make sure it didn’t break anything in the theme you’re using.  If you still want to use it but want to be a bit more secure, you can follow the instructions below.

    This will disable timthumb.php’s ability to load images from external sites, but most bloggers only use timthumb.php for resizing local images:

    1. SSH into your web server. You can use “putty” if you use windows and you’ll need to know your username and password.
    2. cd into your wordpress installation directory. That is going to vary according to which host you’re using or how you’ve installed it.
    3. You need to find every copy of timthumb.php on your system. Use the following command without double quotes: ” find . -name ‘timthumb.php’ “
    4. It will show you a list of where timthumb.php is located. You may want to repeat this command using “thumb.php” as some users have reported that’s what it’s called on their systems.
    5. Edit timthumb.php using a text editor like pico, nano or (if you know what you’re doing) vim. You would type (without double quotes) ” nano directory/that/tim/thumb/is/in/timthumb.php ” for example.
    6. Go down to line 27 where it starts $allowedSites = array (
    7. Change it to remove all the sites listed like “blogger.com” and “flickr.com”. Once you’re done the line should look like this from $allowedSites to the semi-colon:
    8. $allowedSites = array();
    9. Note the empty parentheses.
    10. The next line should be blank and the following line will probably say “STOP MODIFYING HERE”
    11. That’s it. Save the file and you’re done.

    Full post:

    Earlier today this blog was hacked. I found out because I loaded a page on my blog and my blog spoke to me. It said “Congratulations, you’re a winner”.

    After a brief WTF? I loaded up the dev tools in Chrome and checked what network requests were going out. Ad content was loading and I don’t run ads on my blog. For some reason the content was hidden, perhaps someone gets paid per impression.

    I found the hostname the ads were loading from and grepped the WordPress code for the hostname and nothing turned up. Next I dumped the database  – in fact all mysql databases on the server and grepped for the ad hostname and still nothing.

    Eventually I found it. The hacker had done an eval(base64_decode(‘…long base64 encoded string’)) in one of WordPress PHP files. My bad for allowing that file to be writeable by the web server. Read on, because even if you set your file permissions correctly on the WordPress php files, you may still be vulnerable.

    But what I really wanted to know was how the hell he wrote to a file on my machine.

    I checked my nginx and apache access and error logs and eventually found a few PHP errors in the apache log that clued me in.

    Turns out the theme I’m using, Memoir, which I bought for $30 from ElegantThemes.com uses a library called timthumb.php. timthumb.php uses a cache directory which lives under wp-content and it writes to that directory when it fetches an image and resizes it.

    If you can figure out a way to get timthumb to fetch a php file and put it in that directory, you’re in.

    The default configuration of timthumb.php which many themes use allow files to be remotely loaded and resized from the following domains:

    $allowedSites = array (
    	'flickr.com',
    	'picasa.com',
    	'blogger.com',
    	'wordpress.com',
    	'img.youtube.com',
    	'upload.wikimedia.org',
    	'photobucket.com',
    );

    The problem is the way the developer checks which domain he’s fetching from. He uses the PHP strpos function and if the domain string appears anywhere in the hostname, he’ll allow that file to be fetched.

    So if you create a file on a web server like so: http://blogger.com.somebadhackersite.com/badscript.php and tell timthumb.php to fetch it, it merrily fetches the file and puts it in the cache directory ready for execution.

    [Note: I’m 99% sure this will work on most webserver configurations because the cache directory that timthumb uses is a subdirectory of directories that are allowed to execute files with a .php extension. So unless you explicitly tell your server to not execute .php files in the cache directory, it’ll execute them. ]

    Then you just access the file in the cache directory on the target site using your web browser and whatever code came from http://blogger.com.somebadhackersite.com/badscript.php will get executed by the web server.

    In my case, this is what the hacker saw when he accessed my site:

    It’s called Alucar shell and it’s a php file that contains one massive base64 encoded string that gets decoded and evalled. It’s encoded in an attempt to hide itself.

    When you first hit the script it presents you with a login page and once you’re signed in you see the screenshot above. It works quite well actually. Even if the rest of your filesystem is secure, whoever is using it can dump read-only files like /etc/passwd to get a list of user accounts, config files which may contain passwords, etc..etc..

    The current version of timthumb has this issue. Since it’s already in the wild and I just got hacked by it, I figure it’s ok to release the vulnerability to the general public.

    To check if you have been hacked do the following:

    1. Sign into your server using ssh
    2. cd to your wordpress installation directory
    3. run “grep -r base64_decode *”
    4. You should see a few occurences but if any of them have a long encoded string between the parentheses, then you’re probably hacked.
    The hacker used base64_decode in the file uploaded to the timthumb.php cache directory as well as where he injected code in my blog.
    Also check your /tmp/ directory and if you have any suspicious files there like xwf.txt or any other .txt files, look at them in a text editor.
    How to (possibly) fix this:
    1. Go into your theme directory and figure out where timthumb.php is.
    2. You might try “find /your/wordpress/dir/wp-content/themes/YourTheme/ -name “timthumb.php””
    3. Edit timthumb and remove the list of external websites that content is allowed to be loaded from.
    4. I have not audited the rest of the code, so this may or may not make it secure.
    5. The developer really needs to use a regular expression to check the external hostnames images can be loaded from.
    I would also recommend that if you’re a theme developer using timthumb.php, you check to see how it’s configured and try to load a php file from blogger.com.yoursite.com to see if you’re vulnerable.
  • In case you're having a rough Monday….

    This will improve your day. Metric – Gold Guns Girls. Have an awesome week!!

     

  • How sovereign debt becomes leverage – a lesson from history

    "America Looks at its Neighbors" (political cartoon, 1932).
    "America Looks at its Neighbors" (political cartoon, 1932).

    I grew up in South Africa and for a time my birth country was the only worthwhile stop on the long sea journey that spice traders would make from Europe to India and back. South Africa was colonized by Europeans for this reason. A guy called Jan Van Riebeeck was ordered to set up camp there by the Dutch East India Company.

    In November, 1869 the 10 year construction project on The Highway to India, aka the Suez Canal, was completed and South Africa became just another colony. Since then the Suez has had a colorful history, but there’s one specific teachable moment in the history of the Suez the United States could learn from…

    In July 1956 the president of Egypt, Gamal Nasser decided to nationalize the Suez Canal. This irked a few stakeholders and on October 29, 1956, Israel invaded Egypt. The next day Britain and France threw their hats into the ring and started bombing Cairo.

    At this point in history, Britain was suffering under a mountain of debt. Here’s a historical graph of Britains debt to GDP ratio to give you some perspective:

    British Public Debt from 1900 to 2010
    British Public Debt from 1900 to 2010

    The United States held much of the debt that Britain was in. Some of the bonds were owed to the US as part of Britains World War II debt to the US government, corporations and individuals and some of them were part of the Marshall Plan to help rebuild Europe post WWII.

    The US used this debt to put tremendous pressure on Britain to halt the invasion. Eisenhower ordered Humphrey, secretary of the treasury to prepare to sell part of the US governments sterling bond holdings. His British counterpart advised his prime minister, Anthony Eden, that if the US did sell their bonds, the British pound would devalue to such an extent that they would no longer be able to import what they needed to sustain the islands. Eden announced a cease fire on November 6th.

    The US is now at around 90% debt to GDP ratio with a total debt of just over 14 trillion. Around 4 trillion of that is held by other countries, China being our biggest “investor”.

    Lets put it this way: It’s hard to not take the call when your single largest investor needs a favor.

  • The avg age of an entrepreneur is 40 and only 11% to 16% are venture backed

    Vivek Whadwa, director of the Center for Entrepreneurship and Research Commercialization at Duke has written about some interesting research his team has done in the Washington Post.

    They found the average age for an entrepreneur is 40 and there are twice as many entrepreneurs over 50 than under 25.

    They also found that entrepreneurs with a college degree tend to grow bigger companies than those who dropped out.

    But most controversial, Vivek and his team found that the VC industry tends to overstate their role and that most successful companies are not venture backed:

     

    The National Venture Capital Association touts its members’ impact on the U.S. economy, saying they created 12 million jobs and generated $3 trillion in revenue in 2010 (equivalent to 21 percent of the nation’s GDP), and claiming credit for eight out of 10software-industry jobs.

    But these numbers do not isolate venture capital’s real role. They include all the revenue generated in 2010 by any company that a venture capitalist ever invested in, at any stage of its existence. Venture capitalists could buy stock in a company before its initial public offering and then claim credit for its success in perpetuity.

    Less than 5 percent of venture capital goes to early-stage companies — those taking the risk of developing innovative products. Our analysis of more than 500 companies in high-growth industries revealed that not even 11 percent of these companies took venture capital at any stage of their existence. The Kauffman Foundation ran a similar analysis of companies on the Inc. magazine 500 list and found that only 16 percent of them raised venture capital.

    The reality is that venture capital follows innovation. Such investors seek out companies that already have working products and proven business models. Venture capital doesn’t stimulate innovation; it wants in once it looks like a good bet.

     

  • Tokyo markets open up 1%, guess they're not too worried

    The Nikkei just opened and is up 1% after a brutal week. Guess they’re confident the US is going to get it’s act together. They must not have heard Pelosi is thinking of backing out. Or maybe FT.com’s stats are delayed.

  • Business lessons from Breaking Bad

    On the recommendation of my sister-in-law I’m watching a few episodes of Breaking Bad, Season 1.

    There’s a classic exchange between Walt and Jesse in Ep 6 as they realize they need to move from retail to wholesale:

    Walt: This is unacceptable. I am breaking the law here. This return is too little for the risk. I thought you’d be ready for another lb today.

    Jesse: You may know a lot about chemistry man but you don’t know jack about slangin’ dope.

    Walt: We have to move our production bulk wholesale now. How do we do that?

    Jesse: What do you mean? To, like, a distributor?

    Walt: Yes. Yes, that’s what we need. We need a distributor now. Do you know anyone like that?

    Jesse: Yeah. I mean, I used to until you killed him.

  • Hacker News negativity finally hurts the program it supports

    I’ve posted a couple of times about the negativity on Hacker News and why I no longer post there. Well the AirBnB fiasco has been seized by the HN “nancy grace” crowd with a vengance and they’re pounding their most successful startup ever into the dirt and promoting its competitors. While you may feel the AirBnB execs could have handled the destruction of a customer’s home a little better, this seems a little over the top.

    Here is a list of all recent AirBnB stories that hit #1 on Hacker News during the last 7 days starting with the oldest first. These stats are taken from an app I wrote a while ago to track HN stats.

    Airbnb bags $112 million in Series B — comments guynamedloren Posted 6 days, 16 hours, 30 minutes ago. and spent 5 hours at #1

     

    AirBnB: Crimes committed against a host — comments foxit Posted 4 days, 13 hours, 40 minutes ago. and spent 7 hours, 50 minutes at #1

     

    The Moment Of Truth For AirBnB As User’s Home Is Utterly Trashed — comments ssclafani Posted 4 days, 30 minutes ago. and spent 6 hours at #1

     

    On Safety: A Word From Airbnb — comments ssclafani Posted 3 days, 18 hours, 20 minutes ago. and spent 4 hours, 30 minutes at #1

     

    Airbnb Victim Speaks Again: Homeless, Scared And Angry — comments jamesgagan Posted 2 days, 12 hours, 50 minutes ago. and spent 10 minutes at #1

     

    Airbnb Nightmare: No End In Sight — comments moonlighter Posted 2 days, 12 hours, 30 minutes ago. and spent 5 hours at #1

     

    Airbnb story on front page of Financial Times print edition — comments arghnoname Posted 2 days, 7 hours, 20 minutes ago. and spent 2 hours, 39 minutes at #1

     

    Airbnb Competitor Checks IDs: ‘We Don’t Want to Trade Security for Volume’ — comments citadrianne Posted 2 days, 2 hours, 50 minutes ago. and spent 4 hours, 40 minutes at #1

     

    Plot thickens in Airbnb vacation rental horror story — comments felipemnoa Posted 1 day, 2 hours, 20 minutes ago. and spent 1 hour, 40 minutes at #1

     

    Another Airbnb Victim Tells His Story: “There Were Meth Pipes Everywhere” — comments jasonlbaptiste Posted 10 minutes ago. and spent 10 minutes at #1

     

  • Political Marketing and the "Debt Ceiling"

    The phrase “Debt Ceiling” brings to mind phrases like “War on Terror”, “Abortion on Demand”, “Flip-Flop” – or some of the pejoratives used to criticize those wanting to depart from the Vietnam war: nervous nellies, special pleaders and politicians opposing the war were ready to “cut and run”.

    The reality is that there is no hard ceiling that is going to be reached this Tuesday. Instead the Federal Government will be forced to make progressively harder and more irresponsible decisions. Creditors and expenses will be put in a long queue and those with the lowest priority and least impact will go delinquent first.

    This has already started happening. For example, Geithner has declared a debt suspension period and is currently not putting money in the Civil Service Retirement Fund. They’re deferring payment. [Source: Hennessey’s interview on Econtalk]

    Come August 2nd with no decision, we will continue to pay back the interest and principal on government debt but Geithner will have to make progressively harder decisions about which payments to defer. At some point the lawyers in the treasury department are going to tell him he’s overstepping the bounds of what he’s able to do. But that will probably be some time after August 2nd.

    The Federal Government, the same organization that is asking the American people for more money and for permission to go into more debt, is the one who came up with the date August 2nd and the description of the political stalemate’s impact. They have branded it the “Debt Ceiling”.

    I think the date August 2nd is a useful forcing mechanism to inspire political debate and decision making. Movement is always good when it comes to Washington. But one wonders if the phrase “debt ceiling” is Barack Obama’s “War on Terror”.