1 00:00:00,125 --> 00:00:05,750 >>  SO, I'm going talk to you about highjacking build pipeline. I'm Kyle and this is 2 00:00:05,750 --> 00:00:11,750 Greg. We both came from Rackspace. All right. When I talk about build pipelines I'm 3 00:00:13,917 --> 00:00:18,958 talking about your source control from GitHub, GitLab GitBucket, wherever you are 4 00:00:18,958 --> 00:00:25,958 storing their code, depending what site you are on. Continuous integration, are you running on 5 00:00:25,958 --> 00:00:33,333 (?), Kravitz, do you just run it on your local box? What actually is  -- where are your tests 6 00:00:33,333 --> 00:00:39,333 running as well as upstream sources? Like NPM, by pi ruby, GEMS, all of those. The reason 7 00:00:41,500 --> 00:00:46,917 I care about this is I work with a lot of open source software where we set up builds for that 8 00:00:46,917 --> 00:00:54,333 software and you know anybody can contribute, anybody can push patches in, so what sort of 9 00:00:54,333 --> 00:01:01,000 vector is that? What can you do with that? What can you leave? Kind of the precursor here is 10 00:01:01,000 --> 00:01:05,917 all need secrets, you might need secrets for testing if you are deploying to production, 11 00:01:05,917 --> 00:01:12,042 database credentials, OF ced, might be cloud credentials, GitHub tokens, Twitter tokens, 12 00:01:12,042 --> 00:01:18,042 the actual SSH keys to get onto boxes. So need to manage these secrets in some way. Not 13 00:01:21,583 --> 00:01:28,375 everybody does this appropriately. Credentials get leaked. Like if you do a simple 14 00:01:28,375 --> 00:01:35,292 search on GitHub for AKIAJ you'll find Amazon credentials instantly. I'm throwing 1071 15 00:01:35,292 --> 00:01:41,625 results just finding for that stream. Right? Run infrastructure or do whatever, 16 00:01:41,625 --> 00:01:47,625 keep going. I think this is a problem with how people treat Git. (Pause) Like you start up a 17 00:01:49,958 --> 00:01:56,042 new project run GitApp, everything goes in there. If you have any secrets encoded into 18 00:01:56,042 --> 00:02:02,167 the repository itself it's there for the world to see next time you get pushed. This even 19 00:02:02,167 --> 00:02:08,875 happens to people who know what they are doing. Rich Milgo gave a talk at BlackHat on working 20 00:02:08,875 --> 00:02:15,083 with the cloud, and to do security appropriately, set up the DEV environment 21 00:02:15,083 --> 00:02:21,417 appropriately as well as lock down your instances and how to handle keys. When he ended up 22 00:02:21,417 --> 00:02:27,417 looking at the account that got highjacked for him, what did he find running there? Right, so 23 00:02:30,042 --> 00:02:33,792 they didn't actually care about his running infrastructure at all, they just wanted someplace 24 00:02:33,792 --> 00:02:41,583 to go mine. They pulled up really big, really, really big instances and just started 25 00:02:41,583 --> 00:02:47,708 spending his money. If you want to read more about that, go to bit leave/mobile, he does a good 26 00:02:47,708 --> 00:02:54,625 article on what happened, what he did forensically to figure out what they had done, tried to 27 00:02:54,625 --> 00:03:00,625 go back through cloud trail to figure out what they had access to and what else they tried to 28 00:03:02,958 --> 00:03:08,958 get access to. It's not just in the GitHub credentials either, this is an Android file, APK. If 29 00:03:12,750 --> 00:03:19,708 you look here in the middle, there's there cedes so every client that downloaded the app 30 00:03:19,708 --> 00:03:25,708 has access to their Amazon. (Laughter) So what fun can we do with cloud credentials? I can 31 00:03:31,125 --> 00:03:37,208 spin servers, take down their servers, redistribute DNS and load balance exercise, maybe you 32 00:03:37,208 --> 00:03:42,292 have another node you want to put into the mix. I can remount volumes, if you are on Amazon 33 00:03:42,292 --> 00:03:48,750 you have your cloud server and it has a EVS volume on it. Disconnect that, connect it to a 34 00:03:48,750 --> 00:03:53,875 separate like you've got their server and their EBS volumes connected to their own. Have 35 00:03:53,875 --> 00:03:59,875 your SSH and make you have access on their box. For open stack Nova you can append your 36 00:04:03,500 --> 00:04:09,875 own key to the main key they use when they spin up new servers so that every time the authorized 37 00:04:09,875 --> 00:04:16,542 key file gets dropped you get access to the new server. So how do we find these keys to 38 00:04:16,542 --> 00:04:24,083 begin with? Like I was saying before for Amazon, just search, find a list of keys, there it 39 00:04:24,083 --> 00:04:30,833 is. For open stack and the open stack writers it's a little harder because they actually 40 00:04:30,833 --> 00:04:36,875 just use a hex decimal D so you have to look at what it would look like in the code. OS 41 00:04:36,875 --> 00:04:44,708 passion word would be if they had an environment variable or in some config file. The 42 00:04:44,708 --> 00:04:48,750 results are really big here but it could have come from a library. Same thing with 43 00:04:48,750 --> 00:04:54,750 Bianchi, looking at 3,000 results. And then API key, 305,000 results. Again, that's 44 00:04:59,667 --> 00:05:03,708 like could be the libraries, too, doesn't necessarily mean it's the actual API key. We have 45 00:05:03,708 --> 00:05:10,625 to do some amount of triage. For GitHub they only let you search for 1,000  -- you only get 1,000 46 00:05:10,625 --> 00:05:18,292 results back so you to have break it up. So like one day we took the Rackspace EPI key and 47 00:05:18,292 --> 00:05:23,792 we were looking for one good Python, we can do it with or without language. GitHub is 48 00:05:23,792 --> 00:05:30,417 their own search setup so you can break out as many things as you want and also look down 49 00:05:30,417 --> 00:05:36,417 particular paths and languages. We get 4 20 for Python and 8,303 for the rest. When I was 50 00:05:39,458 --> 00:05:45,458 breaking these out I had to go through pretty much all languages and break them into 51 00:05:49,250 --> 00:05:55,500 their own discrete begins. Paragraph bins. Right. Keep sweating. Couldn't we just let 52 00:05:55,500 --> 00:06:00,833 people know when they fuck up? Like those are all there, their e-mails are attached from 53 00:06:00,833 --> 00:06:07,917 GitHub. So we created a project get get sec nanny. It searches repositories for the same 54 00:06:07,917 --> 00:06:14,500 security oop. He mail the original committer and the owner of the project, let them know 55 00:06:14,500 --> 00:06:20,500 how to revoke keys and also how to panic! (Laughter) The responses were very flip. They 56 00:06:24,208 --> 00:06:29,333 were like wow, thank you, how did you find these? You're such a savior and, this is only a 57 00:06:29,333 --> 00:06:37,000 testing project. Is it your testing credit card? (Laughter) So far I found about 265 keys, 58 00:06:37,000 --> 00:06:43,000 I'm clearly going after one provider I want to protect. There is certainly more out 59 00:06:47,167 --> 00:06:53,167 there to find. One of the biggest offenders for this were rails developers, config 60 00:06:55,333 --> 00:07:03,167 initializer secret RV, it's got usually with cred, there are rails itself and this one is 61 00:07:03,167 --> 00:07:09,167 infamous, there's a port out there that will never get accepted that just adds it to 62 00:07:11,500 --> 00:07:16,875 the get ignore so it doesn't happen to people in the future. The comment from rails was, 63 00:07:19,667 --> 00:07:23,042 they're not gonna do it in there because people shouldn't be doing it that way. (Laughter) 64 00:07:23,042 --> 00:07:28,750 You know  -- (Laughter)   -- great! So they're supposed to use environment variables, 65 00:07:28,750 --> 00:07:34,750 that's the way you are supposed to develop and deploy an app. Anyway... (Laughter) So you have 66 00:07:40,375 --> 00:07:44,375 to be  -- we have to do it the right way if we're doing a security checker. This isn't my 67 00:07:44,375 --> 00:07:50,208 security checker I just noticed this last week so Danny Greenfield here he got the 68 00:07:50,208 --> 00:07:56,208 message back. You can't read that enhanced. There we go! All right. So they said they have 69 00:07:58,208 --> 00:08:03,667 confirmed this possibility by manual inspection and then there's the secret. I don't 70 00:08:03,667 --> 00:08:09,667 think they inspected that. So that's it for secrets. But what if you actually need secrets for 71 00:08:12,000 --> 00:08:16,333 testing? It's not like you can live in a vacuum where your code never touches anything. You are 72 00:08:16,333 --> 00:08:22,292 not gonna build any real web infrastructure with that. One opportune nivt  -- I'll give -- 73 00:08:22,292 --> 00:08:30,000 (Inaudible) -- in a second, travesty i I great for open source, just do a get push, hits 74 00:08:30,000 --> 00:08:37,250 the web hook and then sends out tasks to run the build and let you know if you have success. 75 00:08:37,250 --> 00:08:43,750 Lot less control than Jenkins but super simple. This is the definition for a Travis yammel, 76 00:08:43,750 --> 00:08:50,917 you sign up language, here we are doing Python, 2.7, we have, before the install you have 77 00:08:50,917 --> 00:08:58,625 steps as well as the installation and then what script will run. They thv neat 78 00:08:58,625 --> 00:09:04,625 thing called encrypted secrets, though. Like you put secret on the Web out in the open, so I 79 00:09:08,958 --> 00:09:13,000 was curious if I could use it. All this does is sets up an environment variable that can be 80 00:09:13,000 --> 00:09:20,250 used later. So could we leak those deC ed secrets? Like where do they come out? How do I get 81 00:09:20,250 --> 00:09:26,250 the secrets out? Say we open a request and we'll try to echo off their environment variable 82 00:09:29,000 --> 00:09:35,000 has the secret. Oh, nothing! What! So Travis, the keys used for encryption and decryption 83 00:09:40,458 --> 00:09:44,875 are tied to the repository. If you fork a project and add it to Travis it will have a different 84 00:09:44,875 --> 00:09:51,208 pair of keys than the original. What that is saying is if you make a repo it gets one key, 85 00:09:51,208 --> 00:09:58,875 someone forks your repo they get a separate key. They could run their own secret stuff they are 86 00:09:58,875 --> 00:10:06,083 running off their own fork to do testing, but then only the stuff that is on master gets tested 87 00:10:06,083 --> 00:10:12,083 with the original keys. Nice. So let's say we emergency that port request, secret message 88 00:10:14,583 --> 00:10:20,583 then comes in. So drink. Right, so you are only  -- only variable is cloud review and the 89 00:10:22,792 --> 00:10:29,708 fact you to have trust Travis. At some point Travis is decrypting secrets so you have 90 00:10:29,708 --> 00:10:35,708 no idea. But those guys drink good coffee and live in Berlin so I assume they're great. Now 91 00:10:41,042 --> 00:10:47,042 we'll talk about Jenkins and I'll pass over to Greg. (Applause) >> Thanks, Kyle. So 92 00:10:54,042 --> 00:10:58,708 for those of y'all who are not familiar with Jenkins or you're not  -- Jenkins is a continuous 93 00:10:58,708 --> 00:11:06,333 integration and deployment suite used for not only functional testing but also deployment to 94 00:11:06,333 --> 00:11:12,500 production. Even if you are not a Jenkins user this is something you have never heard of before, 95 00:11:12,500 --> 00:11:18,083 why do we care about Jenkins? Well Jenkins is the road to production. If there is a 96 00:11:18,083 --> 00:11:24,292 general since server at the end of that pipeline is either a repository that multiple servers 97 00:11:24,292 --> 00:11:30,292 are using, or a production server with data worth ceiling sealing. The normal interaction 98 00:11:33,500 --> 00:11:39,500 is hipster developer pushes code to GitHub  -- (Laughter)   -- Jenkins is notified via web hook 99 00:11:43,417 --> 00:11:50,417 that there is some code ready for testing. What we want to do is masquerade as hipster 100 00:11:50,417 --> 00:11:55,792 developer so we can push code to production servers or repositories and compromise 101 00:11:55,792 --> 00:12:01,792 them. So let's say that hipster developer makes an oops. He either commits his cloud 102 00:12:04,458 --> 00:12:10,458 credentials which have no off token or he commits his GitHub token directly to his code 103 00:12:12,917 --> 00:12:20,417 repository. We're kind of off to the races, right? So if there is a repository at the end of 104 00:12:20,417 --> 00:12:27,042 that pipeline, all we do is propagate vulnerable code we can scan for and then pop every 105 00:12:27,042 --> 00:12:33,042 single server  -- yeah, and the mic  -- (Laughter)   -- so you can pop every single server that 106 00:12:39,583 --> 00:12:45,583 is using that repository. Can y'all hear this? >>  Yeah! >>  No shit! Okay. (Laughter) So 107 00:12:52,583 --> 00:12:57,875 what's the other option, right? The other option is instead of a repository there's a server with 108 00:12:57,875 --> 00:13:03,500 information we want to steal and sell. So one thing you can do is propagate things for 109 00:13:03,500 --> 00:13:07,958 persistence, think reverse shells, the other options is to try to extract the secrets that 110 00:13:07,958 --> 00:13:13,958 Jenkins has out of the build, so there's two different ways to manage environment variables in 111 00:13:18,708 --> 00:13:23,667 Jenkins. Either you throw them directly in configuration or you're using the credentials 112 00:13:23,667 --> 00:13:31,542 plug-in but at run time they all boil down to actual environment variables. So both plug-ins 113 00:13:31,542 --> 00:13:38,542 could possibly be leaked if you could sneak something like this into the code. So the goal of 114 00:13:38,542 --> 00:13:44,542 this script is to print the environment variables and then phone them home to a server. No 115 00:13:47,333 --> 00:13:53,333 problem. So that's kind of cool but that will only give you one server. You know what's really 116 00:13:56,583 --> 00:14:04,417 cool is compromising multiple servers. So how do we do that? We target the Jenkins install 117 00:14:04,417 --> 00:14:10,417 directly. So for my testing, I was using the Jenkins database library or Jenkins user 118 00:14:16,375 --> 00:14:21,417 management for authentication, Jenkins is like widely configurable so you might not 119 00:14:21,417 --> 00:14:27,375 run into this unless you use that exact authorization to replicate this. I was curious 120 00:14:27,375 --> 00:14:33,500 about how Jenkins decided to manage its users and it boils down to this file called 121 00:14:33,500 --> 00:14:40,833 config.xml and here is what it looks like. You'll see that there is a user name, a hash'd 122 00:14:40,833 --> 00:14:46,833 API token and a hash password. So what caught my attention is they are using JV script. So I 123 00:14:50,292 --> 00:14:57,583 did more digging. A lot moreing. All the way down to this file called Hudson private security 124 00:14:57,583 --> 00:15:03,750 realm. And wall you'll see is how Jenkins is actually encoding and stories passwords. There's 125 00:15:03,750 --> 00:15:10,792 nothing really wrong with this except that it's really to duplicate. So using this 126 00:15:10,792 --> 00:15:16,792 three-line Java code, we can create hashes that are passable to Jenkins. But so now we have 127 00:15:18,833 --> 00:15:23,667 to figure out a way of how to actually pass those hashes and get them installed on Jenkins. 128 00:15:23,667 --> 00:15:31,417 So what if we have something like this in our build, the goal of this code is to locate the 129 00:15:31,417 --> 00:15:37,417 config files for all users and attempt to replace all hashes to a password of our choice. Let's 130 00:15:39,750 --> 00:15:45,750 find out. (Pause) So over here is my Jenkins server. To show I'm not cheating, I need a 131 00:15:52,750 --> 00:15:58,750 password, anyone. Admin? That would be a good guess. (Laughter) So "admin" clearly 132 00:16:02,542 --> 00:16:08,542 doesn't work, right? What I do is go over to my Java code ... (Pause) We get back a secret 133 00:16:38,000 --> 00:16:44,000 path. So then we go over to the compromise repository. And we throw this in our code. So if we 134 00:17:25,500 --> 00:17:31,375 go back to the Jenkins instance, we will see our project is now building or it's queued to build 135 00:17:33,750 --> 00:17:39,750 and it's building. Building... >>  -- (Inaudible) -- (Laughter) >>  So part of the 136 00:17:50,708 --> 00:17:55,042 reason that it's going through this right now is in the script there's a command to restart 137 00:17:55,042 --> 00:18:01,042 because you have to refresh the file. And boom! We can then  -- (Cheers and applause) >>  So 138 00:18:11,375 --> 00:18:15,917 from there it's really simple, right? All you have to do is go change the voice script and 139 00:18:15,917 --> 00:18:23,792 propagate them through. The other thing I wrote just so  -- thankfully it worked, too, I'm 140 00:18:23,792 --> 00:18:31,333 really scared of live demos, the other thing our script did is phoned home all the users that 141 00:18:31,333 --> 00:18:36,583 Jenkins has so I wouldn't even have to know user name, Admin. It's right there for me to see. 142 00:18:36,583 --> 00:18:42,583 Yes. Maybe. Not in this room. They kick you out. But so there is a catch to this technique and 143 00:18:50,625 --> 00:18:56,625 that's this config file is only stored on the master node of Jenkins. So if builds are 144 00:18:59,417 --> 00:19:04,292 allowed to run concurrently that's really good news for us. All you have to do is add a 145 00:19:04,292 --> 00:19:10,833 sleep function to your malicious code to put the nodes to sleep, and back on (?). If you are not 146 00:19:10,833 --> 00:19:15,792 allowed to run concurrent builds you'll have to get creative. One option is to just keep 147 00:19:15,792 --> 00:19:23,750 committing and that's because we know these people we have compromised are developers and 148 00:19:23,750 --> 00:19:30,292 they will be executing other builds. The other thing you can try is with the compromised 149 00:19:30,292 --> 00:19:35,583 credentials, commit other repositories and push other repositories to try to trigger 150 00:19:35,583 --> 00:19:42,542 other bills on Jenkins but what if there aren't any oops, right? What if we really want to 151 00:19:42,542 --> 00:19:48,542 compromise this deployment system because at the end there are 100,000 servers that are 152 00:19:52,083 --> 00:19:57,042 consuming the repository or maybe you have like a vendetta against someone and you just 153 00:19:57,042 --> 00:20:04,083 really want to get into their production boxes. Well, one thing that can help you out is 154 00:20:04,083 --> 00:20:08,458 that the low-hanging fruit in this scenario is that they're doing automatic floor request 155 00:20:08,458 --> 00:20:14,083 building which basically means they are taking all the core requests they get and they are 156 00:20:14,083 --> 00:20:20,083 building them before they decide to push them to master or merge them into the repository. From a 157 00:20:22,917 --> 00:20:27,875 security perspective that sounds really stupid, right. Why would you ever run code that you have 158 00:20:27,875 --> 00:20:33,000 no idea what it does. Well the nice thing about Jenkins is if you were to do this, it would 159 00:20:33,000 --> 00:20:40,083 tell us first it was functional and, two, how the performance compared to previous versions of 160 00:20:40,083 --> 00:20:47,083 the code. There is definitely an argument to be made for doing this. I'm glad I don't develop 161 00:20:47,083 --> 00:20:52,833 because I would probably do this. The other option, though, is that you hit the gate. And 162 00:20:52,833 --> 00:20:59,542 you get this notification from Jenkins asking for one of the Admins to verify the file. So 163 00:20:59,542 --> 00:21:04,167 if you are not going to give up, and no one likes to give up, you essentially have two options. 164 00:21:04,167 --> 00:21:10,167 (Pause) (Silence) The web hook we originally talked about, right, Jenkins is notified there 165 00:21:31,708 --> 00:21:37,708 is code that has to be a web hook from GitHub and it's always or it's by default configured to 166 00:21:55,292 --> 00:22:02,167 be at GitHub-web hook on the Jenkins server. How the process is kicked off is a simple post 167 00:22:02,167 --> 00:22:08,625 request. If you can forage that post request, you can beat the gate and cause a trigger and 168 00:22:08,625 --> 00:22:14,167 compromise the Jenkins server and all things attached to it. The worst-case scenario, right, 169 00:22:14,167 --> 00:22:19,333 it let's say there is no continuous integration, there is no deployment system, it's just 170 00:22:19,333 --> 00:22:25,625 a repository you want to compromise. Sneak in your code, hope no one sees it and it might 171 00:22:25,625 --> 00:22:33,417 propagate. So real quick, I'm going to do the quickest overview ever on how to secure a 172 00:22:33,417 --> 00:22:41,167 Jenkins server. So disable anonymous access, date your deploys, use random port, 173 00:22:41,167 --> 00:22:48,417 disable executors on master, change web hook from default URL, and it's that easy. 174 00:22:48,417 --> 00:22:54,417 (Applause) >>  So we can take questions. If you have questions, come up to the mic. 175 00:23:03,542 --> 00:23:09,542 >>  This is kind of tall. I noticed that Jenkins failed. I have a suggestion. There is a 176 00:23:13,583 --> 00:23:19,083 URL you can hit on the Jenkins master that will make it reload all the files without restarting 177 00:23:19,083 --> 00:23:24,125 so no one would be able to tell you just did that. >>  You have to be authorized for that. 178 00:23:24,125 --> 00:23:30,125 That's my question. You back off! (Laughter) You're absolutely right. There is a 179 00:23:32,250 --> 00:23:36,792 reload URL in the Jenkins server but there's two problems with that. A, you have to be 180 00:23:36,792 --> 00:23:42,417 authorized and, B, you have to know where Jenkins server is. This is like a blind injection. 181 00:23:42,417 --> 00:23:46,875 I don't even have to know where the server is because it will phone home that information to 182 00:23:46,875 --> 00:23:52,917 me. >>  Okay, thanks. >> Anyone else? Anyone? Anyone? No. I know you. I'm not taking your 183 00:23:52,917 --> 00:23:58,917 question. (Laughter) Thank you all so much for coming and feel free to come up and talk to us. 184 00:24:09,875 --> 00:24:11,875 (Applause)