Hello, and welcome to the fabulous executable image exploit. What I'm going to show you is a very, very simple set of concepts. And the exploit is not so much the technique, it's what you do with the technique. So, here's what you're going to learn. You're going to learn the origins of this little exploit, and some of the quote-unquote legitimate reasons for doing some of this stuff. You're going to learn the difference between what I refer to as executable and static images. You're going to learn how to create images with PHP and the GD library. Importantly, you're going to learn how to fool your own server into executing what looks like an image. So, it'll interpret it as a script instead of sending it off to a browser as an image. And I'm going to teach you how to do some really cool things with Web 2.0 websites. What you will not learn, okay, this is not the GDI exploit that some of you might be familiar with. What that was, was they played around with the headers of JPEG, and basically they screwed up the, I think it was the file length. They gave it a negative number, and that forced buffer overflow or something. And Windows machines would then execute code that was in the HTTP header of the file. This is not that exploit. This is something different. This works on images. This is images that people download from your server, okay? This is not a client-side exploit. This is something, again, that runs off of a server that you control. Actually, that's not quite true. There's one little thing I want to show you here, just to kind of warm things up. As soon as the browser loads. There we go. So if you look up here in the location here, this is actually a JPEG that ran a piece of JavaScript. So you could do Ajax or any kind of cool stuff like that. Okay, some of the goals. You're going to learn how to program these executable images. You're going to learn how they can be applied to do some interesting things. And again, I want to get you guys started doing your own things. I'm going to give you some examples of things you can do, but I know you guys can do better than what I came up with. So my email address is on every one of these screens. So take it down. If you do something interesting, I want to know about it, okay? Also, this is not a code-heavy demonstration. There's a little bit of code. I think most of it is on the CD that went out, the conference CD. What I'm going to do is, I'm going to take this presentation and put it on my website, which is also up there, www.shrink.com. It'll be in the lower left-hand corner down where it says, Fun with Bots. That's where you'll find this presentation. Okay, who am I? I've been writing web bots for a long, long time. I've been writing web bots for about 11 or 12 years. Started off doing a lot of telemedicine kind of stuff with web bots. Now I do a lot of stuff with Russian companies. This is my eighth DEF CON. This is the third time I was a speaker. First time, to tell you how much things changed, first time I did DEF CON was DEF CON 5. And I felt to my employer that I needed to legitimize going out to DEF CON. So I covered it for Computer World magazine. So I had an alibi. Based in Minneapolis, which unfortunately has been in the news way too much this week. For all the wrong reasons. This week, we're also opening an office in India. Most of you probably know it as Madras, but they changed the name now. It's called Cine. In April, I had a book come out with my friends at No Starch Press called Web Bots, Spiders, and Screen Scrapers. Now available in Italian. And this month, I also have the cover story on PHP Architect. Okay, origins of the exploit. Originally, my goal was to come up with a really good MySpace tracker. And I had a couple of reasons for doing that. I mean, there are, what do they say, 200,000 sexual predators on MySpace. You know, it would be nice to kind of keep track of these people. I was thinking you could maybe make a tool for parents to kind of monitor what their kids are doing with MySpace or who their friends are. That was kind of the goal. That and a lot of curiosity. So what I wanted to do is I wanted to add images to people's pages like you can do with MySpace. In fact, a lot of web 2.0 sites where you can, you know, there's like message areas where you can upload messages and you can include a little HTML and you can refer to images that are on different servers. That's what I wanted to do. And I wanted to put up an image that looked like this. Basically, a simple little PHP program with a little thing on a query here. Well, I got frustrated because MySpace doesn't let you do that. And they've got good reasons for not letting you do that. And we'll talk about those later. In fact, most web 2.0 sites, the good ones, they won't let you post stuff like that. If you have the source for an image tag, if it's not a JPEG, if it's not a GIF, if it's not a PNG, they will not let you put it up there. They'll filter it out. And the reason they do that is because it's a program. It's not an image. It can be executable. And it might still send an image to someone's browser. It'll look like an image. But in reality, it also has the ability to write cookies, track environment variables, access databases, send instant messages, send faxes, blah-de-blah. Anything a program can do, you can do if you insert a PHP program as a source for an image. So an executable image, is a program. It's not just a static piece of data. It's an actual program. And there are some legitimate uses for this kind of stuff. A lot of times, people will store images as blobs or something in a database, and they'll use a piece of dynamic code to pull it out of the database and send it out. It's perfectly legitimate. A lot of times, people will also use executable images like this to put watermarks on things, so you can tell when an image was downloaded, who did it, at what time, what their IP address was, all that kind of stuff. You just kind of log that kind of stuff in the image. It's also used a lot in CAPTCHAs, where you want to have an image, but you don't want the file name to represent what's in the image, so it's all done behind the scenes with a dynamic image. And that's what we're talking about here. We're talking about dynamic images. So here's a quick little piece of code that shows how some of this works, how you would pull stuff out of a database. So here's our query. We've got something called show image, and we're giving it on the query string an ID of 34. So in our imaginary world here, we've got a table called dbtable, and we've got images that are in a column called image, and we've got an ID associated with each one of those. So you can do a little query, pull out the image, and create a header. This is your MIME type for the media, and then you just echo out the stuff you got back from the database. I usually like to base 64 and code all this stuff, because that way you're guaranteed that you'll never ever have any toxic bit combinations that might be represented as some special character or something. This one? I have some similar to this. Do you see a bug? There might be a bug, I don't know. This is the general concept. I can tell you, no, I don't. This is heavily abstracted. Did I answer your question? Okay. To do something like this, it doesn't require any special graphics libraries. You have to previously store the image as a blob in the database. Images can be referenced by it, by an index or by a name, what have you. This is really useful in cases where your web server doesn't have write permissions on your files. In case you want to upload user-uploaded images, but you can't save them to a file, you can store them in the database usually. Here's an example of this. This is not taking it from a database, but it's taking the information from the query string. The way this would work, as you come in, again, you make this thing look just like the source for an image tag. You send out a header. You create the image. This is using GD. It's a library that's packaged with PHP. Basically, you grab the name of the image, and this image should be sitting someplace on a file. It creates a handle. You create the image, send it out to the browser, destroy the image so you don't run into memory issues with your server, do an exit, and get out of there. The way this would look is you'd have something like this. We've got a file called show reference. If we hit it down here, we change the data, and we have one of the Riviera. Simple stuff. I'm showing you this because when you do one of these things, you have to still masquerade as an image. It's important to be able to, in addition to doing all the cool stuff, you still have to be able to take an image and get it out to the server. You have to still mimic a real image, in other words. Here's another example. It's a little more interesting, a little more dynamic. Again, I'm going to grab an image from an ID. I'm going to define a font and a color for the font. I'm going to have some executable content here. In this case, basically it's just a timestamp, and I'm going to change the angle at which it's rendered from 0 to 90 degrees. Then again, create the header, send the image out, destroy the handle, and exit out of there. The way this looks, this is a couple of images, and every time I flow the page, you see the image is different. Again, I'm just showing you this so that you can see how this kind of stuff is done, because you still have to be able to mimic an actual image when you're doing this kind of stuff. Real quickly, an executable image can display images that are stored in databases. You can programmatically select images that you want to display. You can dynamically produce image content, but you can also do everything that a regular script can do. In other words, you can read refer variables, and you can do that so that you can see the page that the viewer was looking at before they came to the page with your image on it. You can also see the query string, and in the case of what I was trying to do, that was really important, because I wanted to track users on MySpace, and that's where that information was kept. You can also read and write cookies, and you do this, obviously, to track individuals, and this actually also works across domains, and that's kind of an interesting thing, and I'll show you some reasons for that being interesting here in a bit. And you can keep track of histories of stuff, so you can store all your stuff that you've found in a database. And again, you can also communicate whatever you want to communicate when these images are downloaded. So here, for example, on top we've got the header that we've seen a number of times. On the bottom, we've got the place where it's actually creating the image. But in between, this is how you would set a cookie. This is how you would read a cookie. This is how you would get a refer variable, and here's how you would get a query string, okay? And primarily, those are the things that you really want to do. Those are the useful things. Okay, the problem with this, I mean, this all looks great, right? The problem is the sites you want to use this on won't allow you to do it. They'll let you do what's on top, but they won't let you do what's on the bottom. Anything that looks like something that's not a real image, MySpace in particular, MySpace in particular, in my case, would not allow you to upload. They would just parse it away, filter it out. So here's what you do. If you're running Apache, go into the .htaccess file, and you want to add a little line of code that will tell your server that every time you see a file that's requested, and if it's got a .jpg extension, you're going to interpret that as a PHP file. You don't have to do this with JPEGs. You could do it with any other kind of image type. You can do it with GIFs. You can do it with PNGs. For that matter, you don't have to do this with PHP. You could also do this with Perl. You could do it with whatever you're using. Can you do it with .mov? .what? .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. .mov. So once it's done, you can have an image that looks like this. So for example, I've got an example right here. This is the same example we had before. But this time, if I come in here and do a view image, notice it still works, but now we're looking at a JPEG. We're not looking at a... There's nothing here that would identify this as being active. It's just a plain old JPEG as far as anyone's concerned. But in reality, it's a script that's running dynamically. Yes, question back there. No, you had a question for somebody in the back of the room. No problem. No problem. I can go back there. That'd be fine. Okay. So, applications. These can be used on a lot of Web 2.0 websites, which are basically sites where the users build the content. So any place where there's a bulletin board, any place where there's a message board, any place where you're allowed to post content, you can use this kind of image. So that would include places like Craigslist, eBay, MySpace, Fark. I haven't found a use for this one yet, but you can actually do it on PayPal as well. If you are a PayPal merchant, you can brand the checkout process with a... like a banner image. That can be a dynamic image as well. Again, I haven't figured out a use for that yet, but that is something you can do. Yes? Updating the latest bid? Like on eBay? Yeah, and I've got some eBay examples I'll show later. Yeah. But good, you're thinking. So when you do something cool, I want to know about it. You can also use this technique in some non-traditional web environments. You can use it in newsgroups, because a lot of people use browser-based newsgroup readers. You can use it in email, all kinds of cool stuff. So here's what I started doing. I would go into MySpace, and in that area where you can leave people a little love, send them a little message, basically you would post your script, hey, how you doing? Haven't seen you for a while. And here's an image. This is not one of mine, by the way. I just pulled this off of MySpace. But you could put a dynamic image here. And then what happens is when that person who owns that MySpace account, when they come in and they're in their home page, not in their profile, but in their home page, they look to see, oh, friend requests. Cool. They go click on that, or new messages, or new comments. They click on those. When they get to the point where they're looking at your image, their user ID is exposed in the server HTTP refer. And in the case of MySpace, that's really cool because inside their query string, you've got their friend ID, which is their user ID, which basically will tell you exactly who they are in MySpace. And then if you write a cookie to them, any time they come back to your page, even if they didn't capture their query string again, the fact that you wrote a cookie to them, every time they come back, you're going to know who they are. So you're going to be able to track them that way. So here's some fun you can have with MySpace. You can write an application that shows the viewing habits of all your friends by sending each one of them a message that contains an executable image. Okay? You can capture this information. Maybe you send it in an email. And they say, here, look at this image. And they look at it. You get their cookie on their browser. And then when they come to view any of your other dynamic images on MySpace, you can say, oh, I know this person. I wrote their cookie. Here's something else I think would be kind of a fun thing to do. Once you've got people's IDs and you've written your cookies, you could show one set of images on MySpace, to all your MySpace friends, and another set of images to everybody else, which would be kind of cool. You can use these same cookies to track people's movements to other sites, to, again, like eBay, Craigslist, what have you. And since your cookies all belong to your domain, wherever your executable images are, and they could be multiple images, but they're all on the same domain, they're on your domain, your cookies will at least appear to function across domains. Let me show you what I'm talking about here. So if you're on eBay, putting your browser on eBay, eBay's going to write you a cookie. If you're on MySpace, MySpace is going to write you a cookie. But the server on eBay and the MySpace server, they cannot see each other's cookies. However, if you have an executable image on each one of those, and they're both coming from your server, one domain, right, you can say, oh, I wrote you a cookie when you saw my image on eBay, and then I see that you're also at my MySpace page. And I know that's the same person, because I wrote you a cookie. And I can read that, even though you picked them up from two different domains. So this is what's called third-party cookies. So example, if MySpace writes you a cookie, and then an advertiser writes you a cookie, that advertiser's cookie is called a third-party cookie. Now, you might be saying, you can't do that. Browsers don't let you do that. This isn't 1996. Well, actually, it is 1996. The default configuration for Firefox enables third-party cookies. In fact, it's really hard to turn it off. You can't do it from the tools area. There's no simple configuration. What you need to do is you need to type about colon config in your location bar, and then it's way down. It's probably like the 200th command. You need to do that. You change the network cookie behavior, the default from zero to one. Pretty obvious, right? That's how you turn off third-party cookies. So obvious to say, most people running Firefox allow third-party cookies. With Microsoft, it's even easier. The default configuration for IE says it's going to block third-party cookies that do not have a compact privacy policy. So all you have to do is, you have to supply one, okay? And the way you do that is in your header, right after you define your MIME type, you do something like this. In fact, you could steal somebody's privacy, compact privacy thing. Not a big deal. And if you don't really understand it, Microsoft will show you how to defeat their own. So there are some things Microsoft does very, very well. And this is one of them. I have to hand it to them. This is actually a very good reference if any of you are interested in this kind of stuff. Okay, some more fun stuff to do with executable images. You could show high-quality images to the members of your website, but people who haven't signed up are gonna see very poor-quality images. And this might even have a commercial use. One of the things I was thinking about doing with this is selling an image service to museums because they're very touchy about having their images, end up on T-shirts and whatnot. So what you could do is you could have certain sets of people could be able to see the full images and other people not. And if you wanted to, you could even watermark those images. So if they ever did show up someplace, you would know, oh, wait a minute, you're a member of our website. I know who you are. And you downloaded this image at such and such time with this IP address. And we even wrote a cookie to you when you did it. And now we see this image appearing other places and we really like you to take it. Take it off. This is something that you were kind of referring to with eBay. One of the things you could do is you can have dynamic eBay auctions. And this is something that eBay would never allow you to do. But you could actually change the images in your auction based on events. Maybe if nobody's bidding on your auction, maybe you might wanna make your images different. I don't know what you would do, but maybe you would change them somehow. Perhaps you'd change descriptions. Maybe you've got text in images and you're changing the text. Something else you could do, and this, again, I don't know that this is practical, but I'm trying to get you guys thinking about some of the possibilities for this. You could have an eBay auction set up and you could have something on Craigslist at the same time. So if somebody's on Craigslist and they see your stuff and then they go see your stuff on eBay, and they go see your stuff on eBay, you could show them something different. Something that nobody else would see if they hadn't previously seen your ad on Craigslist. Or vice versa, it would work either way. You could also use this tool to evaluate websites that you wanna advertise on. For example, if you're on someplace like FARC and you wanna find out how many times people download things, how many unique IP addresses, browsers, all that kind of stuff. IP addresses, trying to figure out what parts of the country people are coming from. You could do this. You could do that just by posting a few images and seeing what happens. Theoretically, you could also audit results if you're taking out ads on a place like FARC. You could also do some non-repudiation with emails. For example, you could put a executable image inside of an HTML formatted email and if somebody reads the email, you could get an instant message, right? Because it's a dynamic image. You can do anything that a program can do. So if someone says, no, Mike, I never got your email. You can say, yeah, you did. You got it at five o'clock and I wrote you a cookie to prove it. It's on your hard disk. So this is another thing you can do with this kind of stuff. Something else that would be fun to do, and again, I have not figured out an application for this, is to develop expiring images. Images with an expiration date. So you throw them up in your server and after a certain time, they no longer exist. Or they change. Or they're dependent on any other kind of event. Like, when was the first time that particular person saw that image? You know, they've got a limited amount of time to enjoy that image or what have you. But you can do images with expiration dates on them. Okay, I've given you some ideas for what you can do with this. Here's some things that you can use to kind of move things along on your own. Focus on applications where images can be loaded from your server. Okay? And again, the most powerful things you're going to do are those things where it can happen across domains. So you've got images on one domain or that are referred to from one domain, images that are referred to from another domain, and start to triangulate that information. Use cookies. Use referrer variables. And also use query strings. And as I showed you before, it's very easy to do. It's very easy to manipulate images. So if you wanted to do an expiring image or an image that fades over time or something like that, it's very easy to do that kind of stuff with PHP and the GD library. Okay, what can you do? Supposing you've got a website and you don't want people doing this kind of stuff. What can you do? Well, the first thing you should do is you should watch what you put in your query strings. Because if you're putting session information, if that session information is not also backed up by the query strings, by information that's in a cookie or something, you can hijack sessions this way. Because you've got the query string, right? So if you just took that query string, loaded it into a browser, it suddenly becomes your session. So be careful what you put in your query strings. The other safe thing to do is that if you're going to allow people to have, you know, upload images or refer to images, it's much, much better to have them upload them to your server. Because at that point, you control them and you ensure the fact that they are indeed static and that they don't have dynamic content. The downside is that it's going to take more server space and it's going to have more bandwidth that it's going to use, but it does remove the executable from the whole executable image thing. And it also gives you the opportunity to scale images and do thumbnails and stuff like that. All right, that is it. As I told you, it's a very simple concept. If you guys come up with some interesting uses for this, by all means, shoot me an email. I'd love to hear it. So, yeah, you have a question? There's not a microphone out there, is there? Okay, talk loud and I'll repeat the question. Yeah, there's a couple things you can do. One of the things you can do is turn off the third-party cookies. And either you do that by sitting down and saying, are you setting your security high in Internet Explorer? Or, better yet, don't use Internet Explorer, use Firefox, do the about config, and look for that place and then change that image behavior to a one. That I don't know. That I don't. But, you know, if you start doing that kind of stuff, you're also going to miss out a lot of things. So, I think it's better to restrict how cookies are used in your browser. And then the other thing, if you're running Norton, Norton will strip off, or can be set up to strip off, referrers. So, if someone's running Norton and if it's configured properly, the MySpace thing that I showed you would never work. Okay? Yes? Yes? The question was executing something out of MySQL Lite. I'm not sure you can do that. It would have to run on your server and I don't know that there would be an advantage of doing MySQL Lite over doing Postgres or Oracle or anything. Excuse me? 250K? Okay. But, I don't know. I mean, beyond size and price and what have you, I don't see it. MySQL Lite, I don't think, is a client-side database. I think it... Okay. Now, the cool thing, and I just kind of brushed on it a little bit, is the fact that you can run JavaScript in these things. You know? And basically, all I did there is I set up the image the way I did the other one so that it would execute the .jpj or .jp... Yeah, .jpj. JPEG. And it would see it as a script and instead of putting PHP in there, I just put a couple of JavaScript open and close tags and threw up an alert. But you don't have to do just something simple like an alert. Like I said, you could run some really cool AJAX in there. Yeah? Yeah. So, specifically about that, when you're doing JavaScript, if you upload that next to your social network, instead of, like, say, MySpace, would that JavaScript execute or execute in the JavaScript sandbox of MySpace or your server that the server's actually running? Well, MySpace won't know about it because MySpace says, oh, they're just... Here's an image. And then it's your client. Your browser is going to load that from your server and it's going to execute the JavaScript on the client. So, it can't be used for a browser server? Yeah. Not so much. No, no, no. No, it's not that kind of a thing. Yes? Yes? Well, here, let me show you what... No. No. Let me get back to that. Oh, I passed it. Where is it? Okay. There it is. This is really pretty simple. So, you load the image. So, basically, if you did this, you'd have file... What am I looking for? View. There we go. There we go. That took long enough. That's basically all that's in the image. So, yeah, it would be difficult to embed this in HTML. But if you could get people to load the file, you know, like here's an image for you to look at, that kind of thing. Click on the image. One more time. I'm sorry. Get me after the talk and I'll answer your question. Yes. Yes. Yeah, I think it would. I think if you turned off JavaScript in your browser, yeah, it absolutely would. But, again, most people don't do that because, you know, how many websites could you really use now if you turned off JavaScript? You know, especially with all the Ajax and the dynamic HTML and stuff that's going on. So, you could do that, but very, very few people will. Anybody else? Yes. No. What would that do? You could. Yeah. I mean, absolutely. You could probably do some interesting things that way. Absolutely. Yeah. That's an interesting point. So, do one. Send me an email. I'm going to see it work. The one thing that you can't do, is you cannot start an image, prematurely end it, insert a bunch of stuff, and then complete the old image, you know? Like, so you're kind of like inserting stuff in the middle of an image, you know what I mean? So, like, if you've got an image you want to show, and it goes off and grabs it, you prematurely end it, insert a bunch of stuff, and then start another image and have it all interpreted, that will not work. And the reason that won't work is because that's not the way browsers work. Browsers download the page, they figure out all the media that they need to load, they figure out what they've got in cache, and then they load the individual files. So that would not work. And that was something I was thinking about. Gee, how could you get that to work? Could you insert things in the middle of images? But no, I don't believe you can. I don't think that would work. Yeah. . . . . . . . . . . . . . . . . . . . . . . Yep, yep. That would be another way of doing the same thing. Absolutely. Yes? I don't think so. You know, some of the things you can do is in the header, well, from the standpoint of the person creating the image, there are things that you can do in the header, like with dates and stuff, that it'll always say, okay, I've got to fetch this. From the standpoint of a client having these things saved in cache, I don't see any danger in that because by the time they end up in your cache, they're no longer dynamic. They're dead. They're static at that point. So they're only dynamic when they're downloaded. After that, just an image. Anybody else? Yes? Well, cookies will not work across browsers. So if you save a cookie on Firefox and someone comes and downloads the same page with Internet Explorer, those cookies are not interoperable. They're in different places. So it'll work with a wide variety of browsers, but it won't happen cross-browser, if that's what your question is. But it's worked in every browser I've tried with the default settings. Sure. Oh, yeah, yeah. The only time I've had a problem with that is I had a problem once a long time ago. I don't know if they fixed this or if it was not intended to be fixed. But at one point, I had to run Apache on IIS. And they have a problem with something called dual parsed headers. Where if you send, like with Apache, you can pretty much sit there and send out headers all day long before you send out the first actual HTML. For a while, IIS, I, I, excuse me. Yeah, I was running, excuse me, I was running PHP on IIS, not Apache. That doesn't make sense. And IIS would not allow you to do that because it's this dual parsed header thing. And they reserved. They reserved that for their own web server. So you could only do it on, at that time it was NT4. You could only run dual parsed headers with PHP if you were running it on, excuse me, I'm getting confused here. It was one of those intentional bugs that Microsoft put in so you'd have to use ASP as opposed to PHP. At least that's how I interpreted it. But that was a long time ago. I can hardly remember that. I'm, I'm surprised I remember dual parsed headers. Anything else? Well, thank you very much. I appreciate it.