Hello, and welcome to BitTorrent Hacks! My name is Michael Brooks, and we are here to, to, we are pirates, we are hackers, and we like BitTorrent. Our motivation for this project is to improve the protocol. A part of this is to educate people on the weaknesses. Here's my speaker, co-speaker David. My name is David Aslanian, I'm a programmer, and I test avionics software. I may have worked on some of the products that some of you have flown here to. This is my trophy room. These are companies I have hacked. This is my Millworm page. My number is 677. The BitTorrent protocol is now seven years old. The protocol has become widely successful in a very short period of time. But with this success has come growing pains. The protocol originally envisioned by Bram Cohen is fairly secure. Vulnerabilities have been introduced by people trying to do things with the protocol. It was never meant to do, such as private networks and encryption. Most vulnerabilities identified by the CVE, or Common Vulnerability Exposure System, more than 37 vulnerabilities are identified, 37,000 vulnerabilities are identified by CVEs. Most CVEs fall into specific categories and are given a Common Weakness Enumeration, or CWE number. For instance, all SQL injection vulnerabilities are CWE89. In the current BitTorrent specification, there are three CWE violations. In order to understand the vulnerabilities, you must first understand the protocol. All right, so I'm going to go over some of the basics of the BitTorrent protocol here. All the data used in BitTorrent communication is B-encoded. The two simple data types are shown here. The first one that you see is an integer. The second one that you see is a string. As you can notice, with a string, the size of the string precedes the colon. And with the integer, the integers can be of any size. They start with an I and end with an E. The next data type is a list. This example shows a list that contains one number and two strings. Pretty simple. The fourth and final data type is a dictionary. This is very similar to a Python dictionary. This dictionary, shown here, contains a single integer and two strings. Dictionaries access their members using strings, whereas lists access their members using numerical indexes. This is a torrent file. These four data types that I just showed you can be used together to create more complex data types, such as irregular trees. As shown here. For instance, the first step in starting a torrent is obtaining the torrent file, obviously. The torrent file uses B-encoding, as we just described, to build a data tree. A couple things to notice about this data tree. All of the leaf nodes in this tree are either integers or strings. Another thing to notice is the announced list subtree. That's a list of all the trackers that the torrent can connect to. ... ... notice the pieces part of the info subtree. It contains SHA-1 hashes of all the chunks of the file that will be transferred. During client tracker communication, torrents are identified by their info hash, which is a SHA-1 hash of the info subtree of a torrent file, shown highlighted in yellow here. One thing to note is that the list of trackers, as well as some other information about the torrent, can change without the info hash ever changing. Immediately after opening a torrent file, your BitTorrent sends a scrape request, which looks like this, to the tracker, to one of the trackers that is listed in the torrent file. The scrape request is a simple HTTP GET. Then the tracker responds with statistics about the torrent, including the number of people who have downloaded the file, the number of people who are seeding and leaching the file, and that's pretty much it. The next thing your BitTorrent client will do is it will send an announce request to the tracker. The announce request sends more information about your client. For instance, in this announce request, the event is started, as you can see, most of the way down there. For example, if you just completed a download, the event would be completed. And zero bytes have been uploaded and downloaded in this announce request. Another important thing to notice is the port number, which the tracker will use when identifying this client in a, in a announce response. This is an announce response. What the announce response will do is it will send a scrape request to the tracker. What the response does is it gives the client a list of peers that it can connect to. The peers, as you can probably see, are not being coded. Instead, it is a binary representation of the IP address and port pairs. At this point, now that the BitTorrent client has a list of peers, it's going to begin a series of handshakes to establish connections with other peers in the swarm. The first packet of a handshake is going to be a UDP ping request, as shown here. This is the response to the UDP ping request. Once the client receives this UDP ping response, the client knows that the remote peer is available, not firewalled or behind a NAT or otherwise unreachable. There are many other packets, UDP packets, that will be sent before any sort of file transfer begins. In addition, there are extensions to the BitTorrent protocol that allow things like chat to occur after the handshake is complete. Take a swig of Jameson. Ah! You are. You are. Client trust. Client trust. BitTorrent was never meant to be an exclusive protocol. As a result, two vulnerabilities were created. The first one is CWE-602, Client-Side Enforcement of Server-Side Security, or better known as Client-Side Trust. With every announced request, the client is expected to report on how much they've uploaded and downloaded and how much is remaining to download. Private BitTorrent networks use these announced requests to enforce the share ratio. For instance, some private BitTorrent networks will impose a 50% ratio in which you must upload at least half as much as you download. The tools to spoof this information are well known. They send spoofed HTTP GET requests informing the tracker that the client has uploaded data when in fact no data has been uploaded. RatioMaster is an example of a spoofed client. However, some BitTorrent communities have been able to detect this method. A more difficult to detect method is is that if the BitTorrent client lies to the tracker saying that it hasn't transferred any data. In addition, the client never reports that the transfer is completed. From the tracker's perspective, the BitTorrent client is unable to contact other peers in the swarm, which could happen under legitimate conditions. As a result, the bandwidth ratio stays constant. But, the BitTorrent client is able to accomplish this. It's called Stealth Bomber. I created it by modifying the original, Python BitTorrent 5.2.2 client written by Bram Cohen. The Stealth Bomber is open source and is on the DEF CON CD. The next vulnerability is Immortal Sessions. All private BitTorrent communities are vulnerable to this. It is CWE384, which ranks in at number 7 on the OWASP Top 10. All BitTorrent clients authenticate with private trackers using a session ID sent as a GET parameter. These values cannot be changed. Because there isn't a system in the protocol to notify the BitTorrent client of a new session ID. The session ID is a part of the announced URL in the .torrent file. One could exploit this vulnerability by sniffing the network for announced requests or .torrent files. Another try-and-true method is XSS in the web application. Due to this session fixation vulnerability, SQL injection in the tracker leads to immediate access without the need to break the password hash. The most popular private BitTorrent community codebase is TBDev. And the MD5 password hash is also the Immortal Session ID. Password hashes should always be kept secret. The hashing of passwords is meant to be a last line of defense after the database... Thank you, thank you. More Jameson! More Jameson! More Jameson! Where was I? DMCA bots. How do you get busted? Just out of curiosity, how many people in the crowd have gotten a DMCA violation? Quite a few of you. When I asked at Black Hat, no one raised their hand. I'm like, oh, you guys aren't pirates. Another client-side trust issue is that a number of seeds for the torrent is calculated by looking at the number of requests which have recently set an event as completed. It is trivial to inflate this number by forging announced requests. Just because a torrent has a lot of seeds doesn't mean it's good. In fact, it could be a trap laid by a DMCA bot. Announced requests are important to anti-pollution. They are piracy software. These automated bots will download torrent files of copyrighted material in question and periodically contact the announced server, obtaining a list of IP addresses. The DMCA bots will connect to you, pretending to be a normal BitTorrent client, and attempt to download a chunk of the copyrighted evidence to use in court. PeerGuardian is a popular peer-to-peer plugin that prevents you from contacting two known DMCA bots. PeerGuardian is similar in that it allows you to convert your server to a real-time blacklist used to fight spam. I don't like PeerGuardian at all. IP addresses are very cheap, and proxy servers are free. Unfortunately, the safest method to avoid anti-piracy bots is to use private BitTorrent networks. Demonoid is invite-only. However, Demonoid users will get DMCA violations. This is because the community is too open. Other communities, such as waffles.fm and what.cd, are very exclusive. I don't think any one of these communities will ever get a DMCA violation. The third vulnerability to affect BitTorrent falls under CWE-300, Channel Accessible by Non-Endpoint, or better known as Man in the Middle. This is a vulnerability in the Message Stream Encryption protocol used for BitTorrent encryption. This is not the first security problem that affects MSE. A company by the name of iPock is able to detect and throttle MSE traffic using deep packet inspection. Furthermore, a research paper entitled Attacks on Message Stream Encryption goes over many problems with MSE. The biggest problem with this paper is that many of the attacks discussed are highly theoretical and may not be possible in the real world. To make matters worse, no source code has been provided to otherwise prove or verify any of the attacks presented. I think this is a common problem with academics. They like to talk about security problems, and they don't write exploit code. On the con.cd is a proof-of-concept code that I wrote, to show that Man in the Middle attacks in MSE is possible. So here it is. This is MSE in short. MSE relies on three cryptographic primitives. The very beginning, before the dot dot dot, is the Diffie-Hellman key exchange. This is to produce a shared secret between the two users. This secret is then concatenated to the end of key A, the string key A. Finally, the info hash of the dot torrent file is concatenated to the end. Then, the SHA-1 hash of this entire string is used as the key. What is lacking from this protocol is authentication. The info hash is sent as a git request, usually over plain text, and can be obtained. Or often, you're downloading torrents from the Pyre Bay, in which case all info hashes are known. This is not a reasonable method of authentication. This protocol is very similar to unauthenticated SSL. Both can use DH key exchanges, both can use RC4, and most importantly, they're both vulnerable to Man in the Middle attacks. So, speaking of unauthenticated SSL, all of these BitTorrent clients whose icons are listed have a completely broken SSL implementation. When communicating over SSL, they don't even bother to check the validity of the certificate. What you're seeing right here on the bottom is a pop-up from Views, and what it originally said is basically something like, would you like to trust this invalid certificate? And what they're really saying is, do you want to get owned? And I think, unfortunately, the answer from most users would be yes, because most users aren't going to understand the subtleties of what this pop-up box even means. Views also fails to check the certificate, but rather claims all certificates are invalid, which is just as bad. These are not problems in the SSL library. They're problems in how the SSL library is being used. This is like the X-Files, trust no one, not even your certificate authority. And, you know, very powerful security tools like SSL can be used completely incorrectly, even if the libraries they're using are completely insecure. So while working with the Python BitTorrent client, I found some awesome code comments. The first one is, this function is a boat made of shit floating on a river of shit. The next one is, Azureus sucks equals choke. I'm not shitting. Great stuff. Great stuff. I don't know if Bram Cohen wrote that, but it'd be funny. Okay. Attack surface. Okay. So. So what is the attack surface of an application? The attack surface is any code the attacker is able to influence through data. The data structures I've shown you, .torrent files, scrape requests, announce requests, as well as handshake between peers, all data that touches BitTorrent code. Web applications have a very large attack surface, whereas GUI and desktop applications are usually, are very small. There are also inner workings of the application that isn't a surface at all, such as crons, scheduling, and logic to support configurations. It's best to think of an application as a fortress, but hackers make software into jumping castles. Oh. Missing a slide. No. There we go. Fun. Okay. Any data that the attacker controls is called tainted. If a variable containing tainted data is passed to a sensitive function, otherwise known as a sync, then a vulnerability is created. In this example, taint is coming from the command line, using scanf. We're reading in 100 characters, and then string copy, is being used as a, is a sync, and that is triggering a buffer overflow. Another example is in phpMySQL. In this case, taint is a get variable, and MySQL query is a sync. Input validation is a way of taking tainted data and making it safe to use with a sync. In this case, we're using int val, passing it to get before entering the query, making sure that it's a number. All right. So, we did a lot of fuzzing with BitTorrent clients, and we were looking into possibly any tools that are already out there to help us fuzz this application, to fuzz a variety of applications, and one of the tools that we ran into was Peach. Peach is a popular fuzzing framework, and let me diverge for a little bit here. What you're looking at is another .torrent file, displayed as a tree. What our fuzzing framework does, our fuzzing framework does not, is not Peach, but it uses Peach. What our fuzzing framework does, is it traverses this tree with a recursive function, and whenever it finds a leaf node, it inserts juicy fuzz data in place of the original data. Like, for example, once we get to the link node under the info subtree, we see that it's an integer, and then we query Peach for nasty integers that we can insert into the torrent file at that point, and that becomes one fuzz test case. Just given one torrent file like this, we can generate over 150,000 fuzzed test cases by using this method. I should note that the Peach fuzzing framework does not have an API to be able to say something like, I want an integer that would be great for fuzzing, or I want a string that would be great for fuzzing. So we basically had to use a debugger to dig into Peach and find out where we can get that sort of data. So at this point, I think it's important to make the distinction between fuzzing data versus fuzzing to code routines. And this is applicable not just to BitTorrent. The example at the top here you can see data being fuzzed. The encoding is correct for the first two examples under fuzzing data. However, the data itself is what's being fuzzed. In the first case, we're fuzzing for format string vulnerabilities. This is in contrast to actually fuzzing the routines that would decode this data, this been encoded, this be encoded data. And so for the first example on the bottom of the slide, you can see that we corrupted the size of the first string tiny to see what would happen when the decode routine comes across an invalid size of a string. Below that, it's the same thing, just vice versa. One important thing to note is when you find a memory corruption vulnerability in a lower level, in the encoding level, the vulnerability would appear in multiple places in the application. For instance, B decoding routines exist when you're opening .torrent files, when you're communicating with the tracker, as well as handshakes. So all three of those interfaces would be vulnerable to the buffer overflow. It would be up to the choice of the hacker where to exploit it. So our approach to fuzzing is a little bit different than Peach's approach to fuzzing, as you may already have inferred. What you're seeing here is what they call a Peach pit file. Peach is good because it has a lot of pre-made tools that are useful for fuzz testing a variety of different types of software. Peach is bad because you still have to know a lot about the protocol in general, or the file type that you're fuzzing. And it can take a lot of time to track down the documentation, or if there is no documentation, you might even have to reverse engineer the protocol before you can even start this process. Creating a Peach pit file, this XML file shown here, is really time-consuming and can be somewhat difficult. And what it does is what this XML file defines is it defines the type of data you're fuzzing, where the data should go, and how the fuzz test should actually be run. The biggest reason we specifically can't use Peach is because it's not good at modeling irregular trees, such as all of the B-encoded data types that we've shown you thus far. It has an ability to represent binary trees, but that's hardly ever useful in the real world. So, now we're going to talk a little bit about code coverage. All of these pictures, all of these products, rather, have something in common, and that is that they're all safety-critical applications. Code coverage has likely been used in the testing process of each of these products, even the robot from RoboCop, obviously. And the reason that is is because code coverage is really good at finding certain types of bugs. It's really good at quantifying how good your tests are. And, like, for example, in safety-critical industries like the airspace industry, you have to show that your code base was thoroughly tested. And what code coverage does is it provides visibility into what your tests are actually doing. And it also provides a way to indirectly quantify the quality of your testing. This is really important in fuzzing, because fuzzing can be like shooting in the dark. You don't really know what your fuzz tests are doing as a whole, and code coverage can be something that can provide much-needed visibility into what fuzz inputs are actually covering parts of the application that you're interested in, basically your attack surface. So what is code coverage? Code coverage is a form of white-box testing. It provides visibility into the code that... into how the code that you're running is being executed. And from an attacker's standpoint, it allows you to test more of your attack surface. It allows you to identify your attack surface and attempt to increase how much you're testing at that attack surface. And there's many different types of code coverage metrics, and there's many different ways that you can get code coverage data. For example, we have statement coverage, which is pretty simple. How many statements were executed? We have decision coverage, which is how many control flow structures, like if statements, took both their true and false pass. We have condition coverage, which is how many conditions within each decision evaluated to both true and false. So in this context, the condition refers to a Boolean sub-expression, say, within an if statement. And then the new concept here is sync coverage, which is how many syncs have I executed. And we're able to infer this metric from statement coverage. So what we do is we look at the output from a source code analysis program, like, for example, the open source source code analysis program RATS, and determine what percentage of the syncs that we're interested in have been executed during a set, during a run of a specific test or a set of tests. And just to provide a graphical example, this is a statement coverage report produced by LCOV. LCOV is a HTML front end to the GCOV application, which comes with GCC. So there's a couple caveats that you have to be aware of when trying to interpret code coverage metrics. One of them is that just because you've executed code does not mean that you've tested code. So in this example, you can, with the user input of less than ten characters, you can execute this code without having the bug being, the buffer overflow discovered. In addition, in this case, you're only gonna be able to find the null free that exists in this code snippet if the condition is false, in which case you'd have less than 100% code coverage. So 100% code coverage really doesn't mean anything. It's, it's how you interpret these metrics that makes a difference. And, yeah. Okay. So this is basically the same example, except it's with decision coverage. You can completely execute this code with these two test cases listed without ever discovering the buffer overflow in the string copy. So there's a lot of code coverage tools out there. But there's a lot of problems with the code coverage tools that are out there. A lot of them don't, are short on the list of features. They don't integrate source code analysis, which is really useful, whether you're a software tester or an attacker. They don't allow you to add, subtract, or compare coverage reports. They don't allow you to automate the process. And this is really a big problem. So how can we use code coverage with fuzzing? During DEF CON 15, Charlie Miller came up with a pretty cool concept that says you keep adding smart fuzz templates until the effect, the cumulative coverage does not increase. And we built a tool to automate this so that we can have a set of smart fuzz templates to base all of our other fuzz test cases on. And we also integrated source code analysis into this tool so that we could have an idea of what dangerous function calls were or were not being executed during our fuzzing. So... I'm going to give it to Mike from here. Charlie Miller is a doctor. He has a doctoral. Man, I would go to any... But he actually doesn't use doctorate in his name. And I asked him why. And it said because it was too pretentious. I have to agree with him. But hey, if there was a talk by Dr. Doom, even if it was a cooking talk, I would be there cooking with Dr. Doom. Oh, sorry. Let's skip this. Anyway. And I'm going to show you some smash stacks. Here. Yeah. Oh, no, we lost the parrot. All right. So this is the fuzzing application. I've set it to a very special place in the test cases. And I hit enter. That's a stack trace. And more to come. What it's doing is actually it's multi-threaded. We have about 32 threads running. Fucking segfault, bitches. This hit at like 2 a.m. I did this on purpose. None of them are crashing. But a lot of them are. I was screaming and jumping up and down with excitement. And the apartment above me, they're hitting the floor. Quiet down! Just like... Anyway. Anyway. Got to call all Python. Anyway. Thank you for seeing our talk. This is the end of BitTorrent Hacks. Thank you.