1 00:00:00,000 --> 00:00:03,180 Ryan Edmonds is here to chat with you and he's going to tell you about what. 2 00:00:03,180 --> 00:00:05,300 So let's welcome Ryan to the stage. 3 00:00:05,580 --> 00:00:07,060 Thank you so much. 4 00:00:07,060 --> 00:00:08,080 Thanks, Bruce. 5 00:00:09,760 --> 00:00:10,720 All right. 6 00:00:10,720 --> 00:00:11,540 Hi, everyone. 7 00:00:11,540 --> 00:00:17,640 Welcome to my talk titled Disguised Delimit, Exploiting Synology NAS with Delimiters and Novel Tricks. 8 00:00:17,640 --> 00:00:18,800 Let's get started. 9 00:00:19,600 --> 00:00:21,000 Picture this. 10 00:00:21,120 --> 00:00:23,900 You're at home one day and the doorbell rings. 11 00:00:24,520 --> 00:00:27,020 You open the door and are handed a package. 12 00:00:27,180 --> 00:00:28,960 It's addressed to you. 13 00:00:28,960 --> 00:00:35,320 After cracking it open and rifling through the packing peanuts, you're greeted with a small black box. 14 00:00:35,320 --> 00:00:37,020 What is it, you might ask? 15 00:00:37,020 --> 00:00:43,280 A Synology B Station, a consumer version of the Synology NAS line, network attached storage. 16 00:00:43,360 --> 00:00:48,280 And it's going to be a challenging adversary over the coming weeks of research. 17 00:00:48,580 --> 00:00:55,520 That's the position that I found myself in last year when I wanted to participate in the Pwn2Own exploitation competition. 18 00:00:55,560 --> 00:01:01,600 This talk will discuss what I learned while performing zero day research on Synology network attached storage. 19 00:01:01,740 --> 00:01:07,320 And the end result of that research was a $40,000 prize at the Pwn2Own Ireland competition. 20 00:01:08,020 --> 00:01:09,740 Let's talk overview. 21 00:01:09,860 --> 00:01:11,340 First off, the introduction. 22 00:01:11,340 --> 00:01:12,700 And we're in it right now. 23 00:01:12,700 --> 00:01:18,640 Second, we're going to have a brief topic covering Synology NAS internals and attack surface. 24 00:01:18,640 --> 00:01:19,900 Very high level. 25 00:01:20,140 --> 00:01:25,620 Number three, we'll talk through discovering an unauthenticated vulnerability via case study. 26 00:01:25,620 --> 00:01:27,960 This is the research I performed last year. 27 00:01:28,000 --> 00:01:36,700 Four, we'll have a novel exploitation technique for Linux that I stumbled upon while developing the exploit targeting Synology last year. 28 00:01:36,700 --> 00:01:41,240 And fifth, we'll have an exploit presentation so you can see everything come together in video. 29 00:01:41,440 --> 00:01:42,680 Let's get started. 30 00:01:43,320 --> 00:01:44,360 Who am I? 31 00:01:44,360 --> 00:01:45,860 My name is Ryan Emmons. 32 00:01:45,860 --> 00:01:49,160 I am a staff security researcher at Rapid7. 33 00:01:49,160 --> 00:01:58,280 I have approximately four years of zero day research experience and my day to day work is zero day discovery and in day analysis at Rapid7. 34 00:01:58,280 --> 00:02:04,900 I've reported bugs to notable organizations like Oracle, Microsoft and SonicWall in the past. 35 00:02:05,660 --> 00:02:08,780 For this case study research, my project goals were clear. 36 00:02:08,780 --> 00:02:11,960 I wanted to write an unauthenticated exploit. 37 00:02:12,100 --> 00:02:21,200 It should target Synology DSM or disk station manager, the proprietary Linux based operating system that most or all Synology NAS devices run. 38 00:02:21,740 --> 00:02:27,860 I wanted it to work in the default config, no extra changes necessary to make the device vulnerable. 39 00:02:28,220 --> 00:02:31,720 I didn't want to employ any adversary in the middle threat models. 40 00:02:31,720 --> 00:02:35,280 I just wanted to point and shoot remotely. 41 00:02:36,060 --> 00:02:42,420 I wanted the exploit to be highly reliable as opposed to leveraging bugs that were less reliable. 42 00:02:42,740 --> 00:02:44,940 And this was a tough one. 43 00:02:44,940 --> 00:02:50,420 The timeline that I had to research was three and a half weeks due to the conference registration. 44 00:02:50,420 --> 00:02:52,780 So I knew that I had to move quickly. 45 00:02:53,760 --> 00:02:56,380 Let's talk about Synology DSM quickly. 46 00:02:56,500 --> 00:03:02,120 So Synology DSM is the proprietary Linux based operating system used by Synology NAS. 47 00:03:02,120 --> 00:03:08,040 It's used for file management, multimedia, collaboration, productivity, virtualization and surveillance. 48 00:03:08,080 --> 00:03:16,640 It acts as the brain for any number of these workloads and you can see a screen shot there on the right of the web service, the desktop for Synology NAS. 49 00:03:17,820 --> 00:03:32,200 When talking about the device footprint, a naive Shodan query for Synology DSM which encompasses the B station line and the disk station lines returns approximately one million public facing devices. 50 00:03:32,260 --> 00:03:36,040 So these are devices likely to be vulnerable exposed to the Internet. 51 00:03:36,260 --> 00:03:39,340 I knew that there was significant potential impact. 52 00:03:39,340 --> 00:03:41,120 Tons of initial access. 53 00:03:41,120 --> 00:03:49,820 If an exploit works on these devices, then an attacker could move laterally into the network of all of these organizations that had the device public. 54 00:03:51,300 --> 00:03:52,160 All right. 55 00:03:52,160 --> 00:03:54,360 That brings us to a high level. 56 00:03:54,400 --> 00:03:59,300 We're going to talk through Synology DSM internals and attack surface on a high level. 57 00:03:59,300 --> 00:04:01,240 So let's get started with that. 58 00:04:01,800 --> 00:04:09,760 When we're talking about research targets, all Synology NAS runs some variation of disk station manager OS or DSM OS. 59 00:04:10,000 --> 00:04:19,120 The most affordable new hardware options for security researchers are most likely the DS124 and the B station, both running a couple of hundred dollars more or less. 60 00:04:19,680 --> 00:04:26,900 There's also a community project that's very helpful called virtual dash DSM to run DSM in a docker container. 61 00:04:26,900 --> 00:04:32,800 So I recommend that if an individual wants to spin up a quick DSM instance, test exploits, pull binaries or whatnot. 62 00:04:33,620 --> 00:04:37,760 Different Synology NAS devices have different default configurations. 63 00:04:37,760 --> 00:04:49,200 For example, some disk stations automatically have all web services HTTPS whereas others have HTTP and the B station has a different configuration than most disk stations. 64 00:04:49,420 --> 00:04:53,320 Different Synology NAS products also run different architectures. 65 00:04:53,320 --> 00:05:02,020 So the B station is ARM whereas the disk station is x8664 and older disk stations are x86. 66 00:05:04,040 --> 00:05:12,340 Synology offers a package manager, sort of an app store, to install add on packages for the operating system. 67 00:05:12,340 --> 00:05:22,260 And despite these packages qualifying for the pony to own competition, I wanted to go for maximum possible impact so I wanted to stick to default packages only. 68 00:05:23,700 --> 00:05:28,740 Synology B station does not expose SSH or other advanced services by default. 69 00:05:28,740 --> 00:05:31,140 It's a consumer focused device. 70 00:05:31,380 --> 00:05:43,860 In order to enable SSH, you have to go in and toggle Synology technology support remote access which is designed for Synology employees but it can be used to gain access to the root account on the operating system. 71 00:05:44,400 --> 00:05:49,840 Local access can be enabled to configure local credentials and enable SNB, a useful target. 72 00:05:49,840 --> 00:05:55,000 And the disk station line of products has an admin panel with an SSH toggle. 73 00:05:55,000 --> 00:05:57,900 So it's a little more straightforward for security researchers. 74 00:05:59,400 --> 00:06:03,700 Covering a brief history of exploitation at the time that I went into my project. 75 00:06:03,700 --> 00:06:13,620 Pain in the NAS, exploiting cloud connectivity to pwn your NAS by clarity team 82 from 2023 and your NAS is not your NAS by dev core from 2022. 76 00:06:13,620 --> 00:06:36,040 Both great pieces of research that worked in different ways and both were primarily applicable to an attacker in an adjacent network position, so already on the LAN most likely since clarity team 82 targeted an information leak in a discovery protocol on the LAN and the dev core exploit was binary exploitation targeting the Net-A-Talk service which is less likely to be exposed publicly. 77 00:06:37,460 --> 00:06:52,080 Researcher Chen Chen presented an excellent overview of the JSON RPC like Synology web API at power of community in 2019 and this is a great primer for the web application although I didn't end up leaning too much into it during my own research. 78 00:06:52,560 --> 00:07:00,000 When talking about remote attack surface the DSM web service is by far the most widely exposed attack surface. 79 00:07:00,000 --> 00:07:13,600 It's enabled by default and virtually after administrator web authentication virtually all NAS functionality is exposed just via the web service so it's powerful if one is able to establish an authentication bypass or something of that nature. 80 00:07:14,180 --> 00:07:19,540 So the question is what's reachable without authentication for the Synology DSM web service? 81 00:07:20,680 --> 00:07:28,080 And that brings us to our case study, discovering an unauthenticated vulnerability in Synology NAS and we'll dive in now. 82 00:07:28,920 --> 00:07:38,030 The DSM web service listens on ports 5000, 5001, 6600 and 6601 by default depending on the disk station that you got. 83 00:07:38,480 --> 00:07:41,660 A lot of researchers have tested this attack surface. 84 00:07:41,680 --> 00:07:48,600 In fact it was scoped for a few years prior to me picking up the project so I knew it would be reasonably hardened. 85 00:07:49,140 --> 00:07:52,400 But the hope was that maybe we could do it in a new way. 86 00:07:54,580 --> 00:07:59,400 Let's talk about the Synology DSM web service authentication workflow. 87 00:07:59,720 --> 00:08:01,800 Here's the login portal that you see right here. 88 00:08:01,800 --> 00:08:05,160 So step one for the B station is to input a user name. 89 00:08:05,380 --> 00:08:08,280 We'll do that with the string user name in all caps. 90 00:08:08,880 --> 00:08:12,460 Next we have to input a password and we'll just enter password. 91 00:08:12,460 --> 00:08:14,620 It's not a valid set of credentials. 92 00:08:15,220 --> 00:08:20,740 We'll click the blue button and the loading graphic will spin for about five seconds. 93 00:08:20,980 --> 00:08:27,720 Now during that five second period some calculations are being performed under the hood to determine whether the user should be logged in or not. 94 00:08:28,720 --> 00:08:34,120 And because we shouldn't be logged in, we have invalid credentials, a generic failure message appears. 95 00:08:34,120 --> 00:08:35,820 The account or password is invalid. 96 00:08:35,820 --> 00:08:37,060 Please try again. 97 00:08:37,340 --> 00:08:43,100 Now I was curious, during that five second period, what exactly is going on under the hood of the B station? 98 00:08:43,100 --> 00:08:45,840 What's it, what kind of calculations is it doing? 99 00:08:46,400 --> 00:08:49,700 So I wanted to dive in further and monitor at the OS level. 100 00:08:50,400 --> 00:08:56,660 In order to do that, I used the eBPF tool BPF trace with a simple script. 101 00:08:56,700 --> 00:09:06,060 So any time the operating system processed a sys call, it would print the command line string as well as the entire list of environment variables set for that process. 102 00:09:06,500 --> 00:09:08,540 It was very useful introspection. 103 00:09:09,120 --> 00:09:11,460 So we'll kick off a BPF trace program. 104 00:09:12,480 --> 00:09:17,040 And when we log in again, we see three executions take place. 105 00:09:17,200 --> 00:09:21,400 And number one, number two and number three each do a unique thing. 106 00:09:21,400 --> 00:09:22,960 So let's take a look now. 107 00:09:23,880 --> 00:09:31,760 Number one, the first execution, we see command is null, which is unexpected at first until we dig a little bit deeper. 108 00:09:31,900 --> 00:09:36,560 And we see that the program is a CGI binary execution. 109 00:09:36,560 --> 00:09:38,120 So it makes a little more sense. 110 00:09:38,120 --> 00:09:50,300 The environment variables are set, such as script underscore file name with entry.CGI and a number of additional common CGI environment variables such as remote method equals post, remote address, so on and so forth. 111 00:09:51,700 --> 00:09:53,620 Now, that's that. 112 00:09:53,620 --> 00:09:55,100 And it's not too exciting. 113 00:09:55,100 --> 00:09:57,940 But what happens next is a little bit more interesting. 114 00:09:57,940 --> 00:10:00,520 So let's take a look at execution number two. 115 00:10:02,040 --> 00:10:04,920 This time we see the command string set. 116 00:10:05,280 --> 00:10:08,820 It's seno CGI dash plugin dash web log in. 117 00:10:08,820 --> 00:10:11,440 And it has the argument dash dash pre. 118 00:10:11,440 --> 00:10:12,660 Note that. 119 00:10:13,320 --> 00:10:17,920 We also see some additional environment variables set that are different from the previous binary. 120 00:10:18,060 --> 00:10:19,460 User equals blank. 121 00:10:19,460 --> 00:10:21,140 Type equals password. 122 00:10:21,140 --> 00:10:22,660 Is known device equals no. 123 00:10:22,660 --> 00:10:32,280 And then at the bottom, we see seno underscore plugin underscore args, a custom environment variable with a temp directory and a randomized file path. 124 00:10:32,280 --> 00:10:33,840 So that immediately starts our fancy. 125 00:10:33,840 --> 00:10:35,320 Seems very interesting. 126 00:10:36,720 --> 00:10:39,360 And we'll explore that a bit more here in a moment. 127 00:10:39,360 --> 00:10:44,500 The third execution when a user attempts to log in as seen through the BPF trace monitor. 128 00:10:44,600 --> 00:10:48,060 We see the same binary is executed. 129 00:10:48,060 --> 00:10:51,240 This time with the post argument. 130 00:10:51,520 --> 00:10:53,000 So before it was pre. 131 00:10:53,000 --> 00:10:54,540 And now it's post. 132 00:10:54,800 --> 00:10:58,260 We also see that the environment has been populated to a degree. 133 00:10:58,260 --> 00:11:00,320 User equals user name in all caps. 134 00:11:00,320 --> 00:11:01,440 That's our tainted data. 135 00:11:01,440 --> 00:11:03,740 That's our string that we provided as our user name. 136 00:11:03,740 --> 00:11:05,660 We also see some additional information. 137 00:11:05,660 --> 00:11:07,620 The same information we saw previously. 138 00:11:07,960 --> 00:11:10,060 And we see status equals fail. 139 00:11:10,060 --> 00:11:11,880 Result equals negative two. 140 00:11:11,900 --> 00:11:16,600 So by this point in the process, we know that the authentication attempt has failed. 141 00:11:17,560 --> 00:11:26,720 Once more, we see this interesting environment variable seno underscore plugin underscore args with a different file path in the same temp directory. 142 00:11:27,380 --> 00:11:31,580 So we really at this point want to know what's inside of that directory, what's inside of those files. 143 00:11:31,580 --> 00:11:33,500 And when we go to look, they're not there. 144 00:11:33,500 --> 00:11:36,120 These are very ephemeral files that have already been deleted. 145 00:11:36,480 --> 00:11:42,600 So in order to see what those files contain, we're going to have to follow in the footsteps of these two gentlemen and do some investigative work. 146 00:11:44,660 --> 00:11:48,640 To do so, we're going to use inotify, the inotify API for Linux. 147 00:11:49,040 --> 00:11:50,900 More specifically, inotify wait. 148 00:11:50,900 --> 00:11:56,820 We'll monitor that temp directory and we'll look for creation, modification and deletion events in that directory. 149 00:11:57,140 --> 00:12:01,520 Once we start that monitor, then we'll kick off another authentication attempt. 150 00:12:02,880 --> 00:12:05,520 Inotify wait picks up a number of different events. 151 00:12:05,520 --> 00:12:10,160 The creation and modification and deletion of a couple of files. 152 00:12:10,480 --> 00:12:16,060 We want to know what's in these files and unfortunately, inotify is not quick enough to trigger catting the file. 153 00:12:16,060 --> 00:12:18,940 So we'll do it in a hacky way. 154 00:12:20,520 --> 00:12:22,160 We'll race with bash. 155 00:12:22,380 --> 00:12:25,960 You don't have to know what this bash snippet is doing nor pay that much attention. 156 00:12:25,980 --> 00:12:32,680 Essentially, it's constantly trying to grab those files and when they're written for a snap second, it prints them to the console. 157 00:12:32,800 --> 00:12:34,520 File one and file two. 158 00:12:35,300 --> 00:12:40,240 We're going to start this bash snippet and then we're going to attempt to log in again and see what it prints. 159 00:12:42,120 --> 00:12:44,060 So, first file. 160 00:12:44,140 --> 00:12:46,960 User equals type equals password. 161 00:12:46,960 --> 00:12:49,240 Is known device equals no. 162 00:12:49,240 --> 00:12:50,920 Pretty standard stuff. 163 00:12:51,440 --> 00:12:54,400 The second file, user equals user name. 164 00:12:54,400 --> 00:12:55,820 That's our data again. 165 00:12:55,820 --> 00:12:57,000 Type equals password. 166 00:12:57,000 --> 00:12:58,280 Session equals web UI. 167 00:12:58,280 --> 00:12:59,360 So on and so forth. 168 00:12:59,360 --> 00:13:01,600 And we see status equals fail. 169 00:13:01,600 --> 00:13:03,320 Result equals negative two. 170 00:13:03,660 --> 00:13:09,500 So these seem to be similar to the environment variables we saw previously for the second and process executions. 171 00:13:09,980 --> 00:13:20,480 We also note once again that this is tainted data being written to a file before we've even authenticated which is rather exciting as an attacker even if it's not much of a capability at this point. 172 00:13:20,480 --> 00:13:25,120 So user equals user name is our string and the user agent is also provided by the attacker. 173 00:13:27,440 --> 00:13:31,380 Based on what we've discussed so far, the workflow is something like this. 174 00:13:31,380 --> 00:13:34,740 The CGI binary which is native code executes. 175 00:13:34,780 --> 00:13:40,180 Then the SenoCGI plugin dash web log in binary executes twice. 176 00:13:40,180 --> 00:13:44,340 First with the pre argument and second with the post argument. 177 00:13:44,340 --> 00:13:47,140 Making a total of three process executions. 178 00:13:47,500 --> 00:13:59,320 And our hypothesis is that file number one is used to pass data between the CGI binary and the first pre execution and file two is used for the post execution. 179 00:13:59,320 --> 00:14:01,780 And our data is written into those files. 180 00:14:03,000 --> 00:14:07,140 We'll confirm the theory by going to var log and checking the log file. 181 00:14:07,140 --> 00:14:19,940 We'll grep for user name in var log Seno plugin dot log and indeed we see that for plugin web log in, user equals user name is listed as one of the ARGs for the process. 182 00:14:20,040 --> 00:14:28,540 And these environment variables are passed in to a root process execution as environment variables. 183 00:14:28,580 --> 00:14:31,310 And they're considered arguments by the underlying operating system. 184 00:14:32,500 --> 00:14:34,820 Now at this point, this is exciting. 185 00:14:34,820 --> 00:14:40,200 We have the ability to potentially mess around in the environment variable world. 186 00:14:40,460 --> 00:14:54,280 And I love injecting and so I pulled open my favorite web proxy and intercepted a log in request expecting to have some fun and unfortunately I was hit with some custom client side encryption which was unexpected. 187 00:14:54,580 --> 00:14:59,140 So you can see that when I intercepted the request, there's this IK message parameter. 188 00:14:59,140 --> 00:15:00,280 And don't squint too closely. 189 00:15:00,280 --> 00:15:01,480 It's not too important. 190 00:15:02,040 --> 00:15:10,140 There's cipher text, a cipher text argument and you can see that it mentions RSA, AES, things of that nature with a lot of sort of scrambled text. 191 00:15:10,240 --> 00:15:12,280 So there's some encryption at play here. 192 00:15:13,060 --> 00:15:17,520 And the question is, what's the deal with the custom encryption scheme on the log in page? 193 00:15:17,520 --> 00:15:19,060 Because we want to have fun. 194 00:15:19,700 --> 00:15:27,000 A researcher named Fabian Tamp published a blog post in 2021 about this exact custom Synology encryption routine which was very useful. 195 00:15:27,000 --> 00:15:28,560 I recommend checking it out. 196 00:15:28,620 --> 00:15:44,240 Essentially the scheme is layered RSA plus AES to satisfy customer encryption, customer data encryption and transit requirements when using the HTTP web service that started by default in many Synology disk station configurations. 197 00:15:44,400 --> 00:15:54,300 So essentially Synology wanted to address this use case where the HTTP service starts by default and they want encryption so that these credentials aren't transmitted in plain text. 198 00:15:55,260 --> 00:16:02,380 A series of network requests are performed to web APIs to gather the required cryptographic materials. 199 00:16:02,600 --> 00:16:09,800 Then a client side JavaScript implementation is executed with the log in form data to encrypt it. 200 00:16:09,800 --> 00:16:14,840 And lastly, the encrypted data is submitted and then eventually decrypted in the back end. 201 00:16:15,880 --> 00:16:22,580 So in order to inject, we're either going to have to deal with this in some capacity or fall back on alternative tactics. 202 00:16:22,940 --> 00:16:28,080 And let's fall back on alternative tactics as we don't have much time for this project in my position. 203 00:16:28,080 --> 00:16:32,360 The DSM web application is Vue.js client side framework. 204 00:16:32,580 --> 00:16:38,100 We can avoid this crypto soup side quest entirely by using a couple of tricks here. 205 00:16:38,100 --> 00:16:48,600 We'll install the Vue dev tools extension, the official developer tools and we'll install the Vue force dev browser extension which is an unofficial tool that's incredibly useful. 206 00:16:48,620 --> 00:16:54,160 It tricks the official dev tools into thinking a Vue application isn't in production mode. 207 00:16:54,160 --> 00:17:03,800 So it can be used to tamper a little bit with the application and then use Vue dev tools in its intended capacity to be able to modify client side fields and debug in a proper capacity. 208 00:17:05,360 --> 00:17:08,380 Once we have all that configured, we can tamper usernames. 209 00:17:08,380 --> 00:17:13,540 We'll find the DSM log in form object and we see there that username is a JSON string. 210 00:17:13,540 --> 00:17:19,020 So we'll set it to username in all caps as we have been and submit our log in request. 211 00:17:20,640 --> 00:17:26,820 Our bash snippet captures user equals username in the second file which is the same thing we previously observed. 212 00:17:27,200 --> 00:17:30,680 So this is just getting a sense of what was happening before. 213 00:17:31,060 --> 00:17:32,900 And now we'll inject a little bit. 214 00:17:33,160 --> 00:17:37,060 We'll do a classic username line feed next line. 215 00:17:37,060 --> 00:17:41,660 A classic line feed injection and hit submit and see what happens in our bash snippet. 216 00:17:42,460 --> 00:17:45,880 This time something really notable happens. 217 00:17:45,880 --> 00:17:55,300 We can see user equals plug in value start, username, line feed, next line, plug in value end. 218 00:17:55,300 --> 00:18:00,300 Custom book end delimiters have been injected to encase the attacker provided data. 219 00:18:00,300 --> 00:18:09,100 So rather than stripping out any sort of line feeds, they're instead saying, okay, we can expect this and we'll safely encompass them with these delimiters, one on each side. 220 00:18:11,080 --> 00:18:20,840 We confirm this by checking the logs where we can see that args equals user equals username, line feed which was preserved in the log, next line. 221 00:18:20,840 --> 00:18:23,520 But then, most importantly, there's a comma. 222 00:18:23,560 --> 00:18:27,700 So you can see comma, type equals password, comma, session equals web UI. 223 00:18:27,700 --> 00:18:30,240 And that's the delimiter between arguments. 224 00:18:30,240 --> 00:18:37,600 We haven't escaped the context we were hoping to even though our line feed injection was preserved in the logs and on the back end. 225 00:18:39,340 --> 00:18:47,880 So to recap, by default, each environment variable argument is on its own line as is the case if we submit user equals username. 226 00:18:48,300 --> 00:18:53,700 When line feed characters are present, custom book end delimiters are automatically added. 227 00:18:53,700 --> 00:18:57,740 User equals plug in value start, then our data, plug in value end. 228 00:18:57,740 --> 00:19:02,480 And notably, we see type equals password, session equals web UI. 229 00:19:02,520 --> 00:19:15,500 Other environment variables for this native code root process execution, it would be really nice to be able to escape our contained context and be able to inject new environment variables for this root process execution. 230 00:19:16,160 --> 00:19:20,080 So let's inject our own delimiters as many of you may be thinking. 231 00:19:20,940 --> 00:19:24,880 In order to craft an injection, first we need to know what we have to deal with. 232 00:19:24,880 --> 00:19:28,140 User equals, that exists by default no matter what. 233 00:19:28,140 --> 00:19:35,940 And if we have line feed characters in our data, user equals plug in value start, our data, plug in value end will be present. 234 00:19:35,940 --> 00:19:38,020 This is what we're working with to begin with. 235 00:19:38,920 --> 00:19:40,740 Let's craft an injection. 236 00:19:40,780 --> 00:19:43,460 Plug in value start, username. 237 00:19:43,620 --> 00:19:46,700 Plug in value end, we've injected a delimiter. 238 00:19:46,880 --> 00:19:49,220 New variable equals plug in value start. 239 00:19:52,700 --> 00:19:59,180 We're going to set it to test and then close out the context with the preexisting plug in value end delimiter. 240 00:20:00,520 --> 00:20:05,400 When we log in like this, we see that the second file logs something interesting. 241 00:20:05,400 --> 00:20:09,080 So it appears that the device has not escaped our content. 242 00:20:09,080 --> 00:20:14,600 Username, plug in value end, new variable equals plug in value start, test, plug in value end. 243 00:20:14,600 --> 00:20:23,340 And if we check the log files, indeed, under args, we see user equals username, comma, new variable equals test. 244 00:20:23,340 --> 00:20:27,620 We've injected a new environment variable for this root process execution. 245 00:20:27,620 --> 00:20:29,820 Pretty exciting without authentication. 246 00:20:31,560 --> 00:20:39,100 So without authenticating, we can set arbitrary new environment variables for a root owned native code process execution. 247 00:20:39,100 --> 00:20:40,620 It's a strong primitive. 248 00:20:40,620 --> 00:20:46,520 However, we can't clobber or pollute log in success variables, which was something I tried right away. 249 00:20:46,520 --> 00:20:55,120 And it's because the overwrite essentially happens when we can inject those, happens after the log in attempt is completed during the post execution and cleanup. 250 00:20:56,200 --> 00:21:02,340 Moreover, reversing for Synology specific variables we might be able to target yields nothing. 251 00:21:02,400 --> 00:21:12,400 There are no custom environment variables being checked for that might be injectable and there's nothing on that front that is exploitable based on my experience. 252 00:21:13,080 --> 00:21:16,300 So the question really is, how can this be weaponized? 253 00:21:16,980 --> 00:21:26,140 That brings us to the main section of this talk, weaponizing Linux environment variable control, particularly in a remote capacity as we've seen here. 254 00:21:26,140 --> 00:21:27,560 Let's talk about it now. 255 00:21:29,140 --> 00:21:31,940 And we'll cover existing techniques. 256 00:21:32,580 --> 00:21:37,500 So by default, exploitation of environment variables often focuses on local threat models. 257 00:21:37,500 --> 00:21:41,980 If you Google around, people usually tend to assume that you're talking in a local context. 258 00:21:41,980 --> 00:21:47,640 However, there is quite a bit of work that's been done for remote environment variable exploitation on Linux. 259 00:21:48,040 --> 00:22:02,600 Now, my disguise the limit, white paper that's being published today in tandem with this talk, references remote exploitation work by Orange Sci, Felix Wilhelm, Y4ER, Watchtower and LTAM, a security research firm. 260 00:22:03,040 --> 00:22:06,200 So lots of good research that's been published on that topic. 261 00:22:06,940 --> 00:22:12,440 In specific, let's talk about LTAM publications because they nicely encapsulate all known techniques. 262 00:22:12,740 --> 00:22:18,480 The security firm LTAM has published some great research on remote environment variable exploitation on Linux. 263 00:22:18,660 --> 00:22:21,820 Hacking with environment variables was published in 2020. 264 00:22:21,940 --> 00:22:29,260 And in that, they documented a number of different techniques when the environment variables are being injected into a well-known process. 265 00:22:29,260 --> 00:22:32,480 For example, Python, Perl, Node.js or Ruby. 266 00:22:32,480 --> 00:22:44,080 If you're injecting environment variables for a Node.js process, you might be able to inject Node underscore options, which is evaluated by the Node runtime and can be used to establish remote code execution. 267 00:22:45,700 --> 00:22:50,580 They also published remote LD preload exploitation in 2017. 268 00:22:50,580 --> 00:22:53,120 And this is another great piece of research. 269 00:22:53,120 --> 00:23:01,780 Typically, LD preload is an environment variable that can be set with ‑‑ that's pointing to a local native code library on disk. 270 00:23:01,980 --> 00:23:08,920 And it can be used to load that library before any other libraries in order to establish remote code execution. 271 00:23:08,920 --> 00:23:15,160 So if you have a clean disk write, you can upload a file, then load it and establish arbitrary remote code execution. 272 00:23:16,080 --> 00:23:22,140 In their publication, they documented a novel scenario where they did not have a clean disk write. 273 00:23:22,820 --> 00:23:31,220 Instead, they used LD preload to preload a mapped file descriptor in memory via proc FS, self FD. 274 00:23:31,220 --> 00:23:38,760 So essentially, they performed a web request and in the body of the request, they had an ephemeral file, their malicious library. 275 00:23:39,620 --> 00:23:49,820 Then, although the file was not uploaded to disk, they reflectively loaded it from memory using LD preload with proc FS and established remote code execution. 276 00:23:49,820 --> 00:23:51,840 A really cool piece of research. 277 00:23:51,840 --> 00:24:01,340 But unfortunately, during my work, I was not able to establish the ability to upload arbitrary files in a file descriptors mapped. 278 00:24:02,640 --> 00:24:05,000 So that brings us to some local techniques historically. 279 00:24:05,000 --> 00:24:06,380 Let's talk through those. 280 00:24:06,820 --> 00:24:12,800 Researchers have targeted G lib C profiling features on Linux for local privilege escalation in the past. 281 00:24:12,800 --> 00:24:20,680 In specific, I'll mention CVE 20103856, although the CVE details are not quite as important as the techniques used to exploit. 282 00:24:21,620 --> 00:24:33,400 Tavis Ormandy abused PC profile underscore output, part of lib PC profile, and Todor Denev exploited mem usage underscore output in order to exploit this vulnerability. 283 00:24:33,520 --> 00:24:49,520 So by setting these environment variables on systems with these libraries available, they can be used to write debugging files, profiling files, to arbitrary file paths, and this technique leverages a controlled file path, right? 284 00:24:49,520 --> 00:24:52,700 However, an uncontrolled file content, right? 285 00:24:52,760 --> 00:25:02,520 So an attacker that sets these environment variables can create arbitrarily named files anywhere on the system without any control of the contents of the files on the system. 286 00:25:04,100 --> 00:25:07,900 So let's talk through our options based on what we've covered so far. 287 00:25:08,400 --> 00:25:11,140 We could target technology specific variables. 288 00:25:11,140 --> 00:25:20,980 For instance, if Synology checked for certain arguments that weren't present by default or things of that nature, an interpreted language process injection perhaps, but those are present. 289 00:25:20,980 --> 00:25:23,680 Those were not viable based on my research. 290 00:25:24,060 --> 00:25:29,540 We could get a file on disk or ephemerally mapped to then use LD preload to preload that library. 291 00:25:29,820 --> 00:25:38,040 And unfortunately, try as I might, caching mechanisms, anything of that nature, I was unable to get an unauthenticated file upload on Synology disk station manager. 292 00:25:39,300 --> 00:25:48,360 We could also leverage a profiling output variable to write a junk file with a controlled path for command execution. 293 00:25:48,660 --> 00:25:55,770 This is in line with CVE-2024-3400 affecting a Palo Alto piece of software. 294 00:25:56,460 --> 00:26:03,640 And what happened in that case was an arbitrary file write was possible to create file names that contained command injections. 295 00:26:03,780 --> 00:26:17,500 Then once per hour, an automated telemetry collection service crawled that directory and unsafely concatenated the poisoned file names into a command string, thus establishing remote code execution. 296 00:26:18,100 --> 00:26:26,960 Try as I might, I was unable to find a primitive of this nature, essentially a second vulnerability where file names were unsafely concatenated with command strings. 297 00:26:27,780 --> 00:26:31,920 So that brings us to the point of needing a new technique. 298 00:26:35,310 --> 00:26:37,070 What is LD debug? 299 00:26:37,470 --> 00:26:42,250 LD debug is a central figure in this exploitation strategy that I'm presenting today. 300 00:26:42,250 --> 00:26:47,350 It's been around since GLIB C2.1, circa 1999. 301 00:26:47,410 --> 00:26:50,430 So for quite a while and it's available on most modern systems. 302 00:26:50,550 --> 00:26:55,830 It outputs verbose debugging information about the operation of the Linux dynamic linker. 303 00:26:56,510 --> 00:26:59,250 We'll see what some of the options it supports are now. 304 00:27:00,850 --> 00:27:06,110 You can write all bindings, files, or libraries in the value. 305 00:27:06,110 --> 00:27:09,310 And we'll stick to libraries for now in order to see that in action. 306 00:27:11,270 --> 00:27:18,650 We'll run the ID command with LD debug set to libs to print the library search path information. 307 00:27:19,910 --> 00:27:25,890 Down at the bottom, we see the expected UID information that we might expect from an ID command. 308 00:27:25,910 --> 00:27:28,630 However, in between, there's some other interesting info. 309 00:27:28,750 --> 00:27:38,150 In standard error, we see that there are five spaces, followed by a process ID number, followed by a colon, and then some information about the library search path. 310 00:27:39,190 --> 00:27:45,270 So it's attempting to find the library libselinux.so.1, libc.so.6. 311 00:27:45,270 --> 00:27:57,650 And one by one, it's searching known locations for libraries until it establishes where that library is at and it loads it and then prints a blank line with the exception of five spaces, a process ID number, and a colon. 312 00:28:01,040 --> 00:28:08,440 Now, let's talk about LD debug output, which is a partner variable of the last environment variable we discussed. 313 00:28:08,580 --> 00:28:15,900 LD debug output was implemented in the same circa 1999 version, and it is essentially a partner of LD debug. 314 00:28:16,400 --> 00:28:19,560 Normally, LD debug output is written to standard error. 315 00:28:19,580 --> 00:28:29,860 But if LD debug output is defined, then instead, the contents of the standard error messages are written into a file specified by the value of this environment variable. 316 00:28:30,720 --> 00:28:37,900 Notably, it's written with the suffix period followed by the process ID number appended to the path name. 317 00:28:37,900 --> 00:28:39,320 So let's see that now. 318 00:28:40,060 --> 00:28:42,020 LD debug equals libs. 319 00:28:42,020 --> 00:28:47,020 LD debug output equals var temp debug file, and we'll run ID once again. 320 00:28:47,860 --> 00:28:50,800 This time, we don't see anything printed to standard error. 321 00:28:50,800 --> 00:28:52,840 We see the expected ID output. 322 00:28:53,360 --> 00:28:56,780 If we check in var temp, we can find our debug file. 323 00:28:56,780 --> 00:29:02,100 In addition to the file name we specified, a period and process ID number has been appended. 324 00:29:03,100 --> 00:29:10,420 We'll grab the first three lines of this file and confirm that it contains the output that we expect, the LD debug information. 325 00:29:10,420 --> 00:29:16,120 So you can see there a different process ID number, but the same five spaces, process ID number, and a colon. 326 00:29:17,280 --> 00:29:21,460 So at this point, we've accomplished something, although it's a little hard to say what. 327 00:29:21,520 --> 00:29:26,240 We sit and think for a moment and try to determine what exactly we've accomplished at this point. 328 00:29:26,660 --> 00:29:34,360 We've been able to write an arbitrary file, but we don't even control the full file extension, nor do we control any of the file contents. 329 00:29:34,620 --> 00:29:45,300 And so we think to ourselves that it might be valuable to be able to control more of the file, and we're going to try to do that now in order to get at least a dirty file right, controlling some of the contents. 330 00:29:47,040 --> 00:29:49,960 We'll fall back on LD debug to do this. 331 00:29:49,960 --> 00:29:56,900 LD debug, as I mentioned, is an environment variable to load a library before any other libraries on Linux. 332 00:29:57,460 --> 00:30:00,680 So we'll set an LD preload of the string test. 333 00:30:02,400 --> 00:30:07,840 Immediately we see an error because the linker cannot find a library called test, and that is expected. 334 00:30:08,940 --> 00:30:15,980 If we check inside of our file, we see that line one now has some ‑‑ some user input there. 335 00:30:15,980 --> 00:30:17,680 You can see library equals test. 336 00:30:17,680 --> 00:30:19,140 That's tainted data. 337 00:30:19,140 --> 00:30:22,120 We see a second line where we don't control anything. 338 00:30:24,600 --> 00:30:27,640 So let's talk through our existing capabilities. 339 00:30:27,840 --> 00:30:31,400 We have the ability to write files to a path that we mostly control. 340 00:30:31,540 --> 00:30:34,440 We do not control the created files extension. 341 00:30:35,060 --> 00:30:40,620 We only control a string in the middle of a content line, just that test snippet in our example. 342 00:30:41,860 --> 00:30:44,920 Exploitation would require a very lax parser. 343 00:30:45,160 --> 00:30:55,540 Even if we could inject a PHP opening tag with some malicious code and then a PHP closing tag, the file extension is still not controlled and I wasn't able to find a way to escape that in any capacity. 344 00:30:58,080 --> 00:31:01,240 Once again, we'll look in the mirror and think very hard. 345 00:31:01,920 --> 00:31:04,020 What have we done up to this point that worked? 346 00:31:04,040 --> 00:31:05,540 Inject delimiters. 347 00:31:05,760 --> 00:31:10,640 So we're going to fall back on a common friend and try to inject some more delimiters to see what happens. 348 00:31:11,780 --> 00:31:16,100 We'll set LD debug to libs, LD debug output to var temp out. 349 00:31:16,540 --> 00:31:24,500 And this time we're going to LD preload, test preload, line feed, line 2, line feed, line 3 with the ID command. 350 00:31:25,900 --> 00:31:36,680 So this time the error, which is robust, does indicate that object test preload, line feed, line 2, line feed, line 3 cannot be preloaded, which is a promising start. 351 00:31:36,680 --> 00:31:50,140 And indeed if we check in our file, we see that the Linux dynamic linker does not properly handle line feed characters in a library search path ‑‑ in a library path when printing to LD debug. 352 00:31:50,140 --> 00:31:54,480 So you can see that we have those five spaces still with the process ID number and a colon. 353 00:31:54,760 --> 00:31:56,780 Then we have some uncontrolled content. 354 00:31:56,900 --> 00:31:59,080 Test preload, which is our content . 355 00:31:59,080 --> 00:32:05,480 And then on line 2, for a beautiful moment, we control all of the entire line, which is an exciting primitive. 356 00:32:06,200 --> 00:32:10,140 We also see line 3, which then escapes additional uncontrolled content. 357 00:32:10,140 --> 00:32:11,860 There's lots of junk written after that. 358 00:32:11,860 --> 00:32:15,640 But for line 2, we control all of that content and that's important to us. 359 00:32:16,500 --> 00:32:21,400 We have the ability to write files to a path that we mostly control, although we don't control the extension. 360 00:32:22,420 --> 00:32:26,740 We do not create ‑‑ control any of that extension and also it's randomized. 361 00:32:26,740 --> 00:32:31,760 So that number is going to change, making it difficult to predict in any exploitation strategy. 362 00:32:32,660 --> 00:32:38,400 However, we do now control entire lines of the file's contents as we saw there with line 2. 363 00:32:38,400 --> 00:32:46,800 And as it turns out, that's enough for remote code execution on most modern Linux systems when injecting into a root native code process. 364 00:32:48,820 --> 00:32:51,700 Many of you may be familiar with the cron demon. 365 00:32:51,700 --> 00:32:56,600 It's an automated tool in order to execute commands on a routine schedule. 366 00:32:56,600 --> 00:32:59,440 It's installed by default on most Linux systems. 367 00:32:59,460 --> 00:33:02,860 It parses files in etsy cron.d. 368 00:33:03,200 --> 00:33:05,960 It is file extension agnostic. 369 00:33:05,960 --> 00:33:10,400 So it doesn't matter what the file name is, just so long as it's in etsy cron.d. 370 00:33:11,100 --> 00:33:19,960 It also has a permissive parser based on testing and often ignores malformed lines depending on what characters are in those lines. 371 00:33:21,420 --> 00:33:23,780 Here's an example cron tab file. 372 00:33:24,060 --> 00:33:26,560 You can see the first line is the hash symbol. 373 00:33:26,560 --> 00:33:27,600 This is a comment. 374 00:33:27,600 --> 00:33:28,980 That's a comment line. 375 00:33:28,980 --> 00:33:36,300 And on line 2, we see an execution schedule of every one minute defined by those asterisks running as root to touch a file. 376 00:33:36,820 --> 00:33:40,980 Now, note that the second line is delimited by spaces. 377 00:33:41,780 --> 00:33:44,300 In between each character, there are some spaces. 378 00:33:46,500 --> 00:33:48,120 Here's what we're working with. 379 00:33:48,120 --> 00:33:51,960 On line 1, we have a bunch of garbage and we don't control most of the beginning of it. 380 00:33:52,140 --> 00:33:56,420 On line 2, we have a number of asterisks and root and touch executed. 381 00:33:59,940 --> 00:34:03,940 So we're able to execute this in a meaningful manner. 382 00:34:04,640 --> 00:34:13,320 And in order to test, we're going to set LD preload to NOP line feed in order to escape that first line of junk. 383 00:34:13,440 --> 00:34:17,860 Then an execution schedule and the user root and we're going to execute ID. 384 00:34:17,860 --> 00:34:21,960 Then a line feed and then NOP to escape that third line of junk contents. 385 00:34:23,680 --> 00:34:26,020 This time something unexpected happens. 386 00:34:26,140 --> 00:34:31,020 We see that there are a number of asterisks spread out as different libraries. 387 00:34:31,020 --> 00:34:35,780 So we expected this all to be one library, but instead it's thinking that there are a number of different libraries. 388 00:34:36,240 --> 00:34:42,800 And when we check our file, we can see that it's attempting to find library NOP, line feed, asterisk. 389 00:34:42,980 --> 00:34:44,480 And the rest of our payload is cut off. 390 00:34:44,480 --> 00:34:46,660 So the question is, really, why is this happening? 391 00:34:48,100 --> 00:34:58,200 When we look at documentation for LD preload, we see that the items of the list can be separated by spaces or colons and that there is no support for escaping either character. 392 00:34:58,480 --> 00:35:07,680 So it's possible to LD preload multiple libraries and if you have a space present in your data, it says, all right, that's a delimiter for two separate libraries. 393 00:35:07,860 --> 00:35:13,820 But the problem is that we need to inject spaces if we want to be able to write a crontab file and execute malicious code. 394 00:35:14,780 --> 00:35:18,400 So this is the face that I made at that point in the research cycle. 395 00:35:18,400 --> 00:35:21,980 So close but not quite there and not really sure what to do next. 396 00:35:22,360 --> 00:35:28,520 So after thinking about it for a bit, I determined that the best course of action would be to inject more delimiters. 397 00:35:28,520 --> 00:35:32,540 Delimiters are the name of the game with this project and we'll fall back on that classic friend. 398 00:35:34,100 --> 00:35:40,800 Now, as it turns out, although it's not formally documented, cron daemon allows you to use tabs instead of spaces. 399 00:35:40,800 --> 00:35:53,900 So we'll inject NOP, line feed, then an execution schedule and a user and a command ID delimited by tabs, then a line feed and then we'll add a NOP just to escape that additional content on the third line. 400 00:35:55,600 --> 00:36:00,160 This time we can see that the linker error maintains something exciting. 401 00:36:00,160 --> 00:36:09,000 So rather than splitting our payload out, we now have the second line, asterisks, delimited by tab, and then those additional fields for the crontab payload. 402 00:36:10,040 --> 00:36:19,340 And if we check our file, we can see that indeed in the first line, we don't control all of the content, but on the second line we do when delimited by tabs. 403 00:36:19,440 --> 00:36:28,120 So we see asterisk, asterisk, asterisk, every one minute the root user will execute ID delimited by tabs and then we escape the content on the third line. 404 00:36:29,900 --> 00:36:34,260 So, let's craft an injection now based on what we know. 405 00:36:34,480 --> 00:36:38,660 By default we're going to have user equals plugin value start presence. 406 00:36:38,660 --> 00:36:45,120 And we're also going to have plugin value end, assuming that we inject at least one line feed character, which we will. 407 00:36:46,400 --> 00:36:48,520 So here's a payload to try. 408 00:36:48,780 --> 00:36:50,360 Username, plugin value end. 409 00:36:50,360 --> 00:36:53,300 This is irrelevant, it's just necessary for the binary. 410 00:36:53,540 --> 00:37:02,340 Then we'll say LD preload, an environment variable definition, equals plugin value start, NOP, line feed, that's to escape that first line of uncontrolled content. 411 00:37:02,340 --> 00:37:05,720 Then a reverse shell payload for a crontab. 412 00:37:05,720 --> 00:37:12,660 We can see that it's tab delimited and we're using a base 64 encoded string there to avoid bad character problems. 413 00:37:12,660 --> 00:37:16,000 We'll decode that and execute it using the default shell. 414 00:37:16,280 --> 00:37:21,620 Then a line feed character and the string NOP to escape the third line of uncontrolled content. 415 00:37:22,020 --> 00:37:25,380 Plugin value end to end the definition of LD preload. 416 00:37:25,380 --> 00:37:27,280 LD debug equals libs. 417 00:37:27,340 --> 00:37:31,040 LD debug output equals etsy cron D hacks. 418 00:37:31,620 --> 00:37:37,540 And lastly we'll define the escape variable which is just garbage but it's required in order to cleanly escape. 419 00:37:37,540 --> 00:37:44,380 So we see plugin value start, NOP and then that initially injected plugin value end to close things out in a clean manner. 420 00:37:45,620 --> 00:37:58,160 Now funny enough, this is a really interesting vulnerability because you can quite literally copy paste this payload and paste it into the user name field on the web service and hit log in and get a reverse root shell which is pretty awesome. 421 00:37:58,160 --> 00:38:03,700 There's something to be said about not requiring any form of automation and just being able to copy paste a payload in my opinion. 422 00:38:06,240 --> 00:38:06,800 All right. 423 00:38:06,800 --> 00:38:09,880 That brings us to the exploit presentation where you'll be able to see it in action. 424 00:38:09,880 --> 00:38:10,960 Let me pull up the video. 425 00:38:17,550 --> 00:38:18,350 All right. 426 00:38:20,730 --> 00:38:21,190 Okay. 427 00:38:21,190 --> 00:38:27,950 So we'll start a listener with netcat on port 4444 and we'll launch our exploit against the web service. 428 00:38:28,690 --> 00:38:34,110 So we can see that the malicious log in is performed and this is the log in that I just mentioned with that user name. 429 00:38:34,110 --> 00:38:38,090 It will inject environment variables for that root owned process execution. 430 00:38:39,130 --> 00:38:45,190 There's a little bit of a wait here because cron tab and cron demon has to run it every one minute but it won't run immediately. 431 00:38:45,570 --> 00:38:53,330 We see we catch a reverse shell as the user root and we're on a disk station of which there are about a million public facing. 432 00:38:53,330 --> 00:38:54,370 So pretty exciting. 433 00:38:57,070 --> 00:38:58,470 Thank you so much. 434 00:38:58,530 --> 00:39:04,790 And then we also touched the rapid 7 RCE file just for style. 435 00:39:06,410 --> 00:39:06,990 All right. 436 00:39:06,990 --> 00:39:08,350 Let me pull this back up. 437 00:39:10,190 --> 00:39:10,990 Great. 438 00:39:11,130 --> 00:39:12,750 That brings us to the end of the talk. 439 00:39:12,750 --> 00:39:19,950 And I just wanted to extend a special thank you to everyone listed on this slide including some organizations and individuals that were cited as references. 440 00:39:19,950 --> 00:39:22,930 Without these people, it would not have been possible to do this talk. 441 00:39:22,930 --> 00:39:27,650 Huge shout out also to the people that helped out after this slide was finalized. 442 00:39:27,650 --> 00:39:29,030 The team at rapid 7. 443 00:39:29,030 --> 00:39:30,650 Huge thank you to all of them. 444 00:39:30,970 --> 00:39:33,030 And lastly, thank you to all of you for attending. 445 00:39:33,030 --> 00:39:35,670 I really appreciate it and you can find my info there. 446 00:39:35,670 --> 00:39:36,930 Have a great rest of your day.