1 00:00:00,000 --> 00:00:07,040 Well, so it's going to be 2.9 right now. 2 00:00:07,140 --> 00:00:16,120 And originally we were going to have another person introduce Sam, but he's a little bit camera shy, so I guess I've got to do it. 3 00:00:16,480 --> 00:00:26,220 So I guess the best way I can introduce our next speaker is Council 9 Black Badge winner, Sam Earp. 4 00:00:26,240 --> 00:00:26,980 Thank you. 5 00:00:26,980 --> 00:00:29,260 Thank you. 6 00:00:30,040 --> 00:00:31,540 Hi, my name is Sam. 7 00:00:31,820 --> 00:00:34,480 Today I'll be presenting on hunting certificates and servers. 8 00:00:35,000 --> 00:00:36,620 And so let's get started. 9 00:00:37,340 --> 00:00:43,020 Our team, Council 9, has won two black badges through the badge challenge. 10 00:00:44,520 --> 00:00:48,120 As Meg mentioned, I'm also a software engineer at Akamai. 11 00:00:50,400 --> 00:00:54,940 So one thing I have to be very clear here, the opinion is expressed here on my own, no one else's. 12 00:00:54,940 --> 00:00:58,740 And be careful when you connect to any host online. 13 00:00:58,740 --> 00:01:04,020 And always seek explicit permission before attempting to exploit, etc. 14 00:01:06,020 --> 00:01:08,160 So there's three parts to this. 15 00:01:08,160 --> 00:01:09,980 So part one is getting the internet. 16 00:01:10,200 --> 00:01:13,380 So this is actually a DEF CON story. 17 00:01:13,860 --> 00:01:22,540 As part of the badge challenge, we noticed the person who would run the badge challenge every year, 1057, would use the same TLDs over and over again. 18 00:01:22,540 --> 00:01:25,240 In particular, he was very fond of the .codes TLD. 19 00:01:25,520 --> 00:01:29,520 Unfortunately, there are not a lot of .codes domain names. 20 00:01:29,720 --> 00:01:43,000 So he picked up on this and started monitoring for any new .codes registration online, as well as scanning the internet for domains hosted that contained a .code domain. 21 00:01:43,120 --> 00:01:45,480 We'd also then search for relevant Whois records as well. 22 00:01:46,520 --> 00:01:48,940 And this was a pretty closely held secret by our team. 23 00:01:48,940 --> 00:01:51,660 There were about 10 people or so that knew this at the time. 24 00:01:51,660 --> 00:01:54,660 And I'm really only sharing it now simply because it's... 25 00:01:55,180 --> 00:01:58,380 the contest is largely over in its current form anyways. 26 00:02:00,200 --> 00:02:02,360 And so our scanners actually worked. 27 00:02:02,360 --> 00:02:03,800 They picked up one domain. 28 00:02:03,920 --> 00:02:09,100 We got a Slack notification that grey.codes was registered, and it contained lost handle. 29 00:02:09,160 --> 00:02:11,120 Therefore, it must be him, obviously. 30 00:02:12,260 --> 00:02:14,260 So it contained a series of puzzles. 31 00:02:14,260 --> 00:02:15,480 So we visited the website. 32 00:02:15,480 --> 00:02:22,140 We spent about 72 hours not really sleeping, solving the puzzles, thinking it was a registration for a contest or something similar. 33 00:02:23,020 --> 00:02:27,180 And we sent through the solution on the final step. 34 00:02:27,360 --> 00:02:30,200 This was November 2nd, 2016. 35 00:02:30,760 --> 00:02:34,580 And lost replied back, I don't know what you're talking about, essentially. 36 00:02:34,580 --> 00:02:36,080 And it appears you've been trolled. 37 00:02:37,600 --> 00:02:43,840 So I sent grey.codes a message on Twitter asking who he was and what he's doing. 38 00:02:44,080 --> 00:02:47,460 And it turns out we weren't the only ones who got trolled. 39 00:02:47,460 --> 00:02:53,660 All of the other teams who were competing against us at DEF CON that year also got trolled. 40 00:02:53,780 --> 00:03:01,600 And, yeah, it actually turned out it was one of the ten people that we had told about this trolled the rest of us using our own scanner. 41 00:03:04,200 --> 00:03:12,020 So that was kind of my first introduction to scanning the Internet and doing large-scale reconnaissance in the IPv4 space. 42 00:03:12,100 --> 00:03:17,300 And that really got me thinking of what else I could find and search for on public Internet. 43 00:03:17,900 --> 00:03:22,200 And one thing I'm very familiar with is TLS certificates. 44 00:03:22,420 --> 00:03:26,100 And as you might know, TLS certificates contain host names. 45 00:03:26,620 --> 00:03:32,980 And so before I talk about scanning the Internet for TLS certificates, I have to talk about TLS. 46 00:03:32,980 --> 00:03:34,400 And this is going to be really basic. 47 00:03:34,400 --> 00:03:37,280 I'm sorry if you're a TLS expert. 48 00:03:38,160 --> 00:03:47,580 TLS handshake, you send over in the client hello, you send over what you're connecting to in the server name indicator, or who you're asking for. 49 00:03:47,720 --> 00:03:50,920 Then the server will return back a certificate to you. 50 00:03:51,500 --> 00:03:56,880 And then there's a handshake that occurs that drives the secret that's used to communicate. 51 00:03:58,220 --> 00:04:02,240 And, yeah, so that's kind of like a high level of how TLS works. 52 00:04:02,240 --> 00:04:06,720 So if you go to HTTPS, any website today, this is what your browser will be doing. 53 00:04:08,220 --> 00:04:11,780 So I want the host names that are found in that second step there. 54 00:04:12,600 --> 00:04:22,480 And so I wanted an efficient way to get these host names at scale, ideally from as small a server as possible. 55 00:04:22,480 --> 00:04:24,060 I'm not operating with any budget here. 56 00:04:24,060 --> 00:04:27,940 I'm running VPS with a single CPU core. 57 00:04:28,680 --> 00:04:31,120 So I cut off the rest of the connection. 58 00:04:32,060 --> 00:04:45,120 And so when I get a certificate back from the server, I would just immediately kill the connection and not continue along with the heavy crypto that's involved in doing the rest of the handshake. 59 00:04:45,960 --> 00:04:50,480 This isn't really anything crazy. 60 00:04:50,480 --> 00:04:51,880 It's entirely by the spec. 61 00:04:51,880 --> 00:04:55,840 You're allowed to exit out of the handshake at any point if something goes wrong. 62 00:04:59,020 --> 00:05:07,100 So, yeah, so by doing this, I'm able to save CPU costs on my server and connect to many more servers for as cheap as possible. 63 00:05:08,380 --> 00:05:10,340 So what's in an X.509 certificate? 64 00:05:10,340 --> 00:05:13,420 Just to give you an example, this is one for google.com. 65 00:05:13,420 --> 00:05:15,780 And as you can see, it contains many, many host names. 66 00:05:15,780 --> 00:05:17,340 And this is fairly common. 67 00:05:17,340 --> 00:05:21,120 You'll see either wildcard or just a list of domain names. 68 00:05:24,240 --> 00:05:29,100 So as I kind of mentioned previously, I scanned the Internet for these. 69 00:05:29,100 --> 00:05:31,000 So I set up a scanner. 70 00:05:31,000 --> 00:05:33,520 So for every IPv4 host, I did two things. 71 00:05:33,520 --> 00:05:38,260 I ran mass scan over it, which just checks whether or not port 443 is open. 72 00:05:38,520 --> 00:05:42,160 I do that because nothing I write will be faster than mass scan. 73 00:05:42,160 --> 00:05:46,100 So this is a good first filter to prove that 443 is open. 74 00:05:46,320 --> 00:05:56,080 I then wrote a Golang program that sends a TLS client hello and then gets the server certificate back and then just immediately disconnects. 75 00:05:56,120 --> 00:05:58,780 And so the pseudocode roughly looks like this. 76 00:05:58,780 --> 00:06:02,360 There's a few parsing steps in between, but this is essentially all that I run. 77 00:06:02,360 --> 00:06:09,080 And I had to modify the Golang TLS stack to get this early return because it's not really a standard thing to do. 78 00:06:09,080 --> 00:06:11,980 It was actually surprisingly easy. 79 00:06:11,980 --> 00:06:14,060 I really love working with the Golang TLS stack. 80 00:06:14,060 --> 00:06:19,040 It's much, much easier than working with OpenSSL, if you're familiar with OpenSSL. 81 00:06:20,580 --> 00:06:25,940 So this is what I set up and ran, and it took about 72 hours or so. 82 00:06:25,940 --> 00:06:32,300 So I got about 12 gigabytes of data back, which was host name, IP address combinations. 83 00:06:33,120 --> 00:06:37,060 And I then asked myself some really basic questions, like, am I finding every host? 84 00:06:37,060 --> 00:06:41,520 And this scanner identified 51 million hosts online in the IPv4 space. 85 00:06:41,600 --> 00:06:45,000 A 2015 paper that I found identified 42 million. 86 00:06:45,000 --> 00:06:48,500 One really interesting thing, Shodan only finds 42 million today. 87 00:06:48,500 --> 00:06:51,760 I don't understand the discrepancy between MyScanner and Shodan. 88 00:06:51,760 --> 00:06:56,380 I suspect that Shodan's been blocked from parts of the Internet, but I don't have any data to back that up. 89 00:06:58,180 --> 00:07:01,220 And I have to ask myself, am I finding every certificate online? 90 00:07:01,220 --> 00:07:02,620 And the answer really is no. 91 00:07:02,840 --> 00:07:07,280 It's common server configuration in that client hello. 92 00:07:07,280 --> 00:07:08,320 I could specify an SNI. 93 00:07:08,320 --> 00:07:13,500 It's a client server configuration that the TLS client hello is used to differentiate clients. 94 00:07:13,780 --> 00:07:18,120 And based on what you pass in there, that will turn a different certificate back to you. 95 00:07:18,120 --> 00:07:20,420 And so this scanner will miss all of those. 96 00:07:20,840 --> 00:07:24,520 Another thing I have to point out is that the Golang X.509 parser is very strict. 97 00:07:24,740 --> 00:07:35,440 So if your server was doing something weird with certificates or really just had any sort of non-standard X.509 cert, it likely will not pass through Golang's X.509 parser. 98 00:07:35,560 --> 00:07:41,300 And there's actually a blog post on how to parse malformed certificates there, if you're interested. 99 00:07:42,780 --> 00:07:44,720 So I also have to ask, who else is doing this? 100 00:07:44,720 --> 00:07:49,400 Because none of this is really new or revolutionary, in my opinion. 101 00:07:49,400 --> 00:07:53,360 So I set up a TLS server, and I just captured traffic. 102 00:07:53,360 --> 00:07:57,380 And I just got the PCAP back. 103 00:07:57,380 --> 00:08:01,820 And I actually found three servers that did the exact same thing that I'm doing. 104 00:08:01,840 --> 00:08:07,360 Two of them were from universities, which were scanning the internet for their own research purposes. 105 00:08:07,360 --> 00:08:11,100 And one of them was from a hosting provider in Germany as well. 106 00:08:11,100 --> 00:08:17,500 So clearly there's somebody in Germany who's doing the exact same thing and simply not publishing it in any paper that I found. 107 00:08:19,600 --> 00:08:27,580 I believe that they're doing the same thing as I am because of the way that they closed the connections, as well as there was no SNI sent in the connection they made to me. 108 00:08:27,580 --> 00:08:29,480 So clearly they weren't trying to connect me to a host. 109 00:08:29,480 --> 00:08:35,940 They were just simply trying to connect to my IP address to just get back the certificate that I would return. 110 00:08:39,410 --> 00:08:51,290 So I now have 12 gigabytes of host name, IP address combinations, which is a lot of data and it's not the most friendly form to work with, 12 gigabytes of text. 111 00:08:51,710 --> 00:08:57,510 So this is part two, which is how to search large parts of large DNS datasets. 112 00:08:57,790 --> 00:09:01,790 And so this actually came about because I wanted to use the Rapid7 datasets. 113 00:09:02,150 --> 00:09:15,230 So in the Rapid7 DNS, they have DNS listeners online for FDNS and RDNS requests and forward and reverse DNS requests. 114 00:09:15,910 --> 00:09:18,630 And you can just go to their website and download this. 115 00:09:18,630 --> 00:09:19,570 It's a great resource. 116 00:09:19,570 --> 00:09:20,230 I love it. 117 00:09:20,230 --> 00:09:27,130 It's just really hard to work with because it's 10 gigabytes of compressed text, which expands out to about 100 gigabytes of uncompressed text. 118 00:09:27,530 --> 00:09:30,470 And so this always took a long time to search. 119 00:09:30,470 --> 00:09:34,810 Every time I wanted to search through this, it took about 20 minutes, which is a little bit insane. 120 00:09:35,010 --> 00:09:48,990 So I found myself trying to write better or trying to script, I should say, better or more fast ways to decompress and grep or use more disk space to grep faster. 121 00:09:49,310 --> 00:09:51,050 And this obviously took a long time. 122 00:09:51,050 --> 00:09:58,690 And I actually wrote a blog post about this, which is linked to there, back in February. 123 00:09:58,890 --> 00:10:04,490 And so in order to sort the data, I took advantage of the DNS structure. 124 00:10:04,490 --> 00:10:19,750 So DNS is structured such that when you make a DNS request for, in this case, blog.erbysam.com, you actually first go to the root, which is dot, roughly com, and then you go to com, and then you go to com.erbysam, and then you go to com.erbysam.blog. 125 00:10:20,010 --> 00:10:26,030 And so you can take advantage of this in order to sort the data. 126 00:10:26,170 --> 00:10:32,170 So I reversed every string, roughly, and sorted it. 127 00:10:32,170 --> 00:10:51,250 At this point, because the data is sorted, in order to find a host name that I'm looking for in this dataset, I just simply have to binary search it, which is an order of magnitude faster than grepping through the files, if you're familiar with how binary searches work. 128 00:10:52,590 --> 00:10:56,450 I talked about this in much more detail in my blog, if you're interested in the technical details. 129 00:10:56,450 --> 00:10:58,650 I also have code online on how to do this. 130 00:10:58,830 --> 00:11:01,950 And this is actually something that's hosted online as well. 131 00:11:02,710 --> 00:11:05,430 So I put this online using a Golang web server. 132 00:11:05,570 --> 00:11:14,170 It's available today on dns.bufferover.run, bufferover.run.dns, and you get back data from the Rapid7 datasets. 133 00:11:15,710 --> 00:11:17,170 I use this myself. 134 00:11:17,170 --> 00:11:20,710 It's a great way to just quickly grep through them if you're searching for something. 135 00:11:20,910 --> 00:11:23,170 And I also posted the runtime on there. 136 00:11:23,290 --> 00:11:27,030 The runtime is usually a fraction of a second to binary search through this data. 137 00:11:28,730 --> 00:11:43,450 And I also linked to my GitHub account here, which has the code to convert the data into a searchable format that's usable by the server. 138 00:11:44,990 --> 00:11:48,470 So one day I woke up and I checked the traffic for this website. 139 00:11:48,470 --> 00:11:49,890 And I saw this. 140 00:11:49,890 --> 00:11:52,810 And I thought, man, I'm a really good blogger. 141 00:11:52,810 --> 00:12:05,450 But what had actually happened was I got pulled into something called Amass, which is a utility for hostname reconnaissance that's published by OWASP. 142 00:12:05,590 --> 00:12:07,450 And this was completely unknown to me. 143 00:12:07,450 --> 00:12:09,050 Luckily, the server held up. 144 00:12:09,270 --> 00:12:11,310 I'm a little bit proud of that, personally. 145 00:12:11,510 --> 00:12:16,230 But that was the source of all this data. 146 00:12:16,390 --> 00:12:25,330 And the fact that it's so heavily cached means that everyone's searching for the same hostnames, or they're repeatedly searching for the same hostnames over and over again, which I found really interesting. 147 00:12:27,390 --> 00:12:29,670 So I actually don't collect data on this. 148 00:12:29,670 --> 00:12:31,190 I don't actually know what everyone's searching for. 149 00:12:31,190 --> 00:12:33,050 And I, frankly, don't really want to. 150 00:12:33,850 --> 00:12:38,590 So now that I have a good data set and I have a good way to search it, I want to put it all together. 151 00:12:39,490 --> 00:12:48,490 And so similar to the DNS records with Rapid7, I hosted this online at tls.bufferoverrun over .run.dns. 152 00:12:48,670 --> 00:12:50,310 And this is actually online today. 153 00:12:50,310 --> 00:12:51,930 And you're welcome to try it out. 154 00:12:53,210 --> 00:13:05,190 It's literally just a combination of the data set that the MySQL scanner picked up, as well as the server behind the Rapid7 data set. 155 00:13:05,590 --> 00:13:09,730 And this is actually fully automated at this point, so this will refresh once a week. 156 00:13:11,930 --> 00:13:15,770 So I then have to compare myself to what else is out there. 157 00:13:15,770 --> 00:13:19,650 I'm mostly just curious to see if this is even necessary. 158 00:13:19,650 --> 00:13:24,050 And so Shodan.io should contain similar results. 159 00:13:24,150 --> 00:13:25,110 It's not free. 160 00:13:25,730 --> 00:13:31,350 I found that my TLS scanner tends to pick up some more results than Shodan, which I found interesting. 161 00:13:31,390 --> 00:13:37,150 And that kind of goes back to my previous point that I think Shodan is being blocked by large parts of the internet, but I don't have any data to really prove that. 162 00:13:37,690 --> 00:13:44,130 Certificate transparency monitors are awesome, but they only contain publicly trusted certs and they don't link back to where the cert came from. 163 00:13:44,330 --> 00:13:50,430 And Rapid7 actually has a TLS data set, but it's only the new certificates they encounter in their scans every week. 164 00:13:50,430 --> 00:13:54,050 They don't have historical data unless you have an account with them. 165 00:13:54,550 --> 00:13:55,590 And there are many others. 166 00:13:55,590 --> 00:14:00,650 The OAuth AMS tool has a great list of existing resources online. 167 00:14:03,070 --> 00:14:08,550 So just to give a demo here, obviously hack yourself first, plug in your company's name, see what you find. 168 00:14:08,610 --> 00:14:10,150 I find that really interesting. 169 00:14:10,510 --> 00:14:16,530 There's also, when I build these tools, one of the first things I do is I run them against .mil. 170 00:14:16,530 --> 00:14:21,350 And I report whatever I find back to the DoD Vulnerability Disclosure Program. 171 00:14:21,430 --> 00:14:33,430 So if you're interested in finding vulnerabilities, go look at the 473,000 results here, find a host name, try and exploit it, report what you find. 172 00:14:33,710 --> 00:14:35,190 So I did that. 173 00:14:35,190 --> 00:14:44,510 And I found a WebLogic remote code execution that was still online from the 2017 exploit. 174 00:14:44,590 --> 00:14:48,310 And I was able to exploit it and report it in. 175 00:14:48,570 --> 00:14:51,110 And I just do this simply to test my tools. 176 00:14:51,110 --> 00:15:07,530 But at the same time, because the military has such a variation of technology online, because it's all subcontracted out, it's not all PHP, it's not all Java, your tools will likely pick up something interesting. 177 00:15:08,290 --> 00:15:17,030 And so actually, one interesting thing about this is that they actually blocked outbound traffic, which would likely make automated skinners fail here. 178 00:15:17,030 --> 00:15:25,250 So I was able to demonstrate this by injecting a sleep of 12 seconds, and then the result took 12 seconds to return back to me, demonstrating RCE. 179 00:15:28,590 --> 00:15:31,650 So yeah, hack yourself first, hack your military first. 180 00:15:32,530 --> 00:15:33,790 Thank you. 181 00:15:40,070 --> 00:15:41,110 Questions? 182 00:15:44,740 --> 00:15:47,220 I use Linode. 183 00:15:47,340 --> 00:16:05,620 With Linode, if you are a security researcher, or sorry, if you are doing something for security research, you can get a security researcher designation on your account, which means that as long as you follow the rules and you have a page that links to what your tool is doing, 184 00:16:05,620 --> 00:16:12,900 connecting to other hosts, they will have an automated reply to anybody filing abuse complaints against you. 185 00:16:20,150 --> 00:16:28,470 No, it matches based on a reverse of the DNS name. 186 00:16:28,750 --> 00:16:33,830 So you could do .mil, army.mil, and it would return you everything underneath that zone. 187 00:16:39,070 --> 00:16:40,670 Any other questions? 188 00:16:43,900 --> 00:16:44,800 Thank you. 189 00:16:44,800 --> 00:16:46,080 I hope this was interesting.