1 00:00:02,800 --> 00:00:10,280 Thank you. So, hello, DEF CON. I'm David. I'm here to talk about terminals. I actually 2 00:00:10,280 --> 00:00:14,160 came through four terminals on the way here from the other side of the world. My flight 3 00:00:14,160 --> 00:00:20,260 got rerouted, so that was four airport terminals. But that's not what I'm here to talk about. 4 00:00:21,200 --> 00:00:26,740 I'm from G research. I work for our open source team. We're not a security research company. 5 00:00:26,740 --> 00:00:32,880 We're a quantitative research company, and we predict things. So we use a lot of machine 6 00:00:32,880 --> 00:00:39,380 learning, AI, but we also use a lot of infrastructure underneath that. So our open source team aims 7 00:00:39,380 --> 00:00:43,700 to improve the infrastructure we use. We contribute some projects on top of that infrastructure. 8 00:00:44,460 --> 00:00:49,880 We use things like Kubernetes. But we also use terminals, and that's what I'm here to 9 00:00:49,880 --> 00:00:55,600 talk about today. So what is a terminal? Well, first of all, we need to go back in time. 10 00:00:55,600 --> 00:01:03,840 So we're going to go back to the 1960s, and what are terminals built on? ASCII, American 11 00:01:03,840 --> 00:01:10,280 Standard Code for Information Interchange. We've all heard of it. Do we know it that 12 00:01:10,280 --> 00:01:15,940 well? Probably, maybe. There's some strange things in it that are legacy. We take it for 13 00:01:15,940 --> 00:01:23,680 granted. This very slide has ASCII on it. To give it its full name, American Standards 14 00:01:23,680 --> 00:01:34,700 Association, X3.4-1963. That looks like this. So this is the ASCII standard code. Now, if you 15 00:01:34,700 --> 00:01:43,320 look closely at this, it's got seven bits. It's got this large unassigned range. It's got an 16 00:01:43,320 --> 00:01:50,520 escape. That's what a lot of this talk is about. There's also something called WRU, which is 17 00:01:50,520 --> 00:01:53,660 who are you? And that's a way of identifying who are you. And that's a way of identifying the 18 00:01:53,680 --> 00:01:58,540 terminal. So it's asking the terminal on the other end what it is. And a lot of this talk will talk 19 00:01:58,540 --> 00:02:04,380 about that. So from the early days of the ASCII standard, replies were something that were a key 20 00:02:04,380 --> 00:02:12,460 part of it. So in this case, you sent a single character, WRU, and in response, the other end 21 00:02:12,460 --> 00:02:18,880 would probably respond with RU. It turns out that this is actually a bit vague because later on, 22 00:02:18,900 --> 00:02:23,660 ASCII changed. And in 1967, they changed the terminology. And then in 1960, they changed the 23 00:02:23,680 --> 00:02:31,700 terminology. They added, well, lowercase, which was quite key. They also moved escape. So now, if 24 00:02:31,700 --> 00:02:37,740 you look closely at this table, all the control sequences are on the left two columns, except 25 00:02:37,740 --> 00:02:43,220 delete, which is actually an interesting legacy and one of those facts that I really like. So 26 00:02:43,220 --> 00:02:48,820 delete is where it is because on a punch card, if you punch out all the holes, you delete a 27 00:02:48,820 --> 00:02:53,660 character. So sort of legacy things that we still have in our system. So I'm going to go back to 28 00:02:53,680 --> 00:02:58,560 ASCII, a design like that for a reason. It's one of those legacy things that doesn't hurt, but is 29 00:02:58,560 --> 00:03:04,840 kind of amusing. So there's a few other details about ASCII. It only defines seven bits, as I said. 30 00:03:06,060 --> 00:03:13,700 Although, interestingly, two years after this, in 1969, Vint Cerf wrote RFC20, and that basically 31 00:03:13,700 --> 00:03:20,020 said that the Internet should use ASCII. But it also said that it should use 8-bit characters, 32 00:03:20,280 --> 00:03:23,660 which is, you know, what we expect these days. But that RFC20 is a little bit different. It's a 33 00:03:23,680 --> 00:03:27,140 little bit different in that RFC20 actually defines that the 8-bit should just always be zero. So 34 00:03:27,140 --> 00:03:32,180 they hadn't yet actually defined what the 8-bit was, but they were leaving room for expansion. So 35 00:03:32,180 --> 00:03:37,540 there's a lot of sort of history here. And if you're interested in character encodings, there's 36 00:03:37,540 --> 00:03:43,700 some references for this talk. There's books on it. IBM were very involved in it and some other 37 00:03:43,700 --> 00:03:52,580 things. So 60 years ago, there were some proposals on escape. So before ASCII was even officially 38 00:03:52,580 --> 00:03:53,660 published as a standard, there were some proposals on escape. So before ASCII was even officially 39 00:03:53,680 --> 00:03:59,140 published as a standard, Bob Bemmer, who was an employee of IBM, wrote an article in Communications 40 00:03:59,140 --> 00:04:05,340 of the ACM called A Proposal for Character Code Compatibility, which is a bit of a wordy thing. But 41 00:04:05,340 --> 00:04:11,060 basically, it was saying, we've got this single character set, but what are we going to do if we 42 00:04:11,060 --> 00:04:16,140 need to put international characters in and so on? And that was where escape was defined as a 43 00:04:16,140 --> 00:04:23,660 mechanism that allowed basically to say something different is coming after this point. And that's 44 00:04:23,660 --> 00:04:29,500 what the first reference is about, and, you know, everyone here who's done freaking or anything knows 45 00:04:29,500 --> 00:04:36,300 how great in-band signalling is. So basically, this is in-band signalling. And so the second reference is 46 00:04:36,300 --> 00:04:44,060 from 1963, and that was written by someone at AT&T, you know? So they really loved in-band signalling, 47 00:04:44,180 --> 00:04:49,940 and so this was a proposed discipline for the use of escape. And this was kind of the first time what 48 00:04:49,940 --> 00:04:53,640 escape might mean was defined. And it's very interesting to see that. So, therefore, this is a 49 00:04:53,640 --> 00:04:56,940 sort of go back at the history of this, and one of the key things it actually defines 50 00:04:56,940 --> 00:05:04,160 is not what ESCAPE does, but how it is parsed, essentially, and so it says that particular 51 00:05:04,160 --> 00:05:11,880 letters or symbols after ESCAPE will either continue the ESCAPE sequence or end it, and 52 00:05:11,880 --> 00:05:13,240 that later becomes relevant. 53 00:05:15,120 --> 00:05:17,320 So what was ASCII actually used in? 54 00:05:17,320 --> 00:05:24,360 So in 1963, the Teletype Model 33 was released, and this was one of the first devices to use 55 00:05:24,360 --> 00:05:24,760 ASCII. 56 00:05:25,540 --> 00:05:30,960 It's kind of lost a history of exactly what was using it, but, you know, this was a thing 57 00:05:30,960 --> 00:05:35,160 you could type into, and it was used in the development of Unix. 58 00:05:35,360 --> 00:05:42,260 So sitting down here, this is 1972, Ken Thompson is sitting down and Dennis Ritchie is standing 59 00:05:42,260 --> 00:05:42,560 up. 60 00:05:43,260 --> 00:05:47,300 Ken is sitting at a Teletype writer, or a TTW. 61 00:05:47,300 --> 00:05:54,060 So this Unix machine has two TTYs, you know, we still call them TTYs, but they're not that 62 00:05:54,060 --> 00:05:54,780 device anymore. 63 00:05:55,020 --> 00:05:57,940 They're something quite different, but this is where terminals started. 64 00:05:59,960 --> 00:06:06,100 So in 1976, Leah Sigler released the ADM3A. 65 00:06:06,600 --> 00:06:08,640 Now, this wasn't the first glass terminal. 66 00:06:08,700 --> 00:06:13,320 That was released by Datapoint in the early 70s, but this was one of the first affordable 67 00:06:13,320 --> 00:06:15,280 terminals, at least for the time. 68 00:06:15,320 --> 00:06:17,280 In kit form, it cost under $1,000.00. 69 00:06:19,740 --> 00:06:25,900 And the University of California, Berkeley standardized on these for various uses, and 70 00:06:27,120 --> 00:06:28,520 Bill Joy had one of these. 71 00:06:28,840 --> 00:06:33,120 And if we look closely at the keyboard, on the very left-hand side are the configuration 72 00:06:33,120 --> 00:06:33,620 options. 73 00:06:33,960 --> 00:06:39,880 So you open a panel and change dip switches to adjust the speed and other things like 74 00:06:39,880 --> 00:06:40,080 that. 75 00:06:40,360 --> 00:06:45,280 But if you look very closely at the H, J, K, and L keys, you see they've got arrows. 76 00:06:45,400 --> 00:06:47,260 And if you're a Vim user, you can see the arrows. 77 00:06:47,260 --> 00:06:49,140 And if you're a Vim user, you might be familiar with what those do. 78 00:06:49,920 --> 00:06:56,060 So this terminal was used by Bill Joy in the development of the X editor, which had a mode 79 00:06:56,060 --> 00:06:56,520 called VI. 80 00:06:56,840 --> 00:06:58,700 And as we all know, the rest is history. 81 00:06:58,880 --> 00:07:04,760 So this was the first device that was used to develop the visual editor that we now know 82 00:07:04,760 --> 00:07:05,300 are Unix. 83 00:07:06,180 --> 00:07:08,180 And there's another Unix reference here. 84 00:07:08,300 --> 00:07:12,460 If you look at the top right-hand side, the home button has a tilde on it. 85 00:07:12,620 --> 00:07:15,320 And that's obviously what the home directory is these days. 86 00:07:15,440 --> 00:07:17,240 So that's the first device that was used to develop the visual editor that we now know 87 00:07:17,260 --> 00:07:18,880 And that's the exact reason that that is the case. 88 00:07:18,960 --> 00:07:21,060 People think so, but it's kind of lost in time. 89 00:07:22,140 --> 00:07:24,580 But yeah, there's definitely a lot of Unix history here. 90 00:07:25,180 --> 00:07:27,160 But why is this also interesting? 91 00:07:27,640 --> 00:07:32,280 Well, this was also one of the first terminals that let you position the cursor anywhere 92 00:07:32,280 --> 00:07:32,940 on the screen. 93 00:07:33,520 --> 00:07:36,260 This is from the manual, which someone has kindly preserved. 94 00:07:37,180 --> 00:07:41,960 And it basically says that the escape key can be used in conjunction with character 95 00:07:41,960 --> 00:07:46,000 keys to position the cursor anywhere on the screen. 96 00:07:46,000 --> 00:07:50,080 So rather than just moving the cursor up and down, you can say, I want it five rows 97 00:07:50,080 --> 00:07:55,140 down, two across, whatever it is, which lets you essentially implement any kind of editor 98 00:07:55,140 --> 00:07:59,120 or a visual application, at least as far as you can go visually, in a terminal. 99 00:08:00,260 --> 00:08:03,880 And this was one of the first devices to support this kind of thing. 100 00:08:04,680 --> 00:08:07,580 Some of the earlier terminals only supported moving up and down. 101 00:08:07,700 --> 00:08:12,860 So it was more inefficient, especially over, at the time, people were using 300-board modems 102 00:08:12,860 --> 00:08:13,660 to talk to things. 103 00:08:13,780 --> 00:08:15,820 So if you imagine you have to send multiple. 104 00:08:16,420 --> 00:08:19,440 Characters to move the cursor around, you can literally see the cursor move. 105 00:08:21,420 --> 00:08:25,660 So the interesting thing in this document is actually somewhere in the middle. 106 00:08:25,780 --> 00:08:28,520 It says this operation may also be initiated by the host computer. 107 00:08:28,820 --> 00:08:34,640 So the user can type this as an escape sequence, or the computer can basically say, position 108 00:08:34,640 --> 00:08:35,860 this where I want it on the screen. 109 00:08:36,100 --> 00:08:38,400 And basically, that makes VI possible. 110 00:08:41,160 --> 00:08:45,800 So in 1978, the VT100 was released. 111 00:08:45,820 --> 00:08:52,300 And this was really continuing the introduction of more and more features into terminals. 112 00:08:53,640 --> 00:08:58,020 But the interesting thing about the VT100 is it was one of the first terminals to implement 113 00:08:58,020 --> 00:09:03,340 this standard, which is known as X3.64-1979. 114 00:09:04,020 --> 00:09:08,420 But many people know this simply as ANSI. 115 00:09:09,320 --> 00:09:11,240 So we'll get to what that means. 116 00:09:11,600 --> 00:09:13,760 But to give it its full name. 117 00:09:13,820 --> 00:09:15,320 Additional controls for use with. 118 00:09:15,380 --> 00:09:15,800 A manual. 119 00:09:15,820 --> 00:09:18,020 The American National Standard Code for Information Interchange. 120 00:09:18,280 --> 00:09:21,860 So basically, this was, as I said earlier, there was a working group on escape. 121 00:09:22,180 --> 00:09:24,600 That working group turned into this standard. 122 00:09:24,960 --> 00:09:26,700 There were some other details along the way. 123 00:09:26,780 --> 00:09:28,180 But this was the final product. 124 00:09:28,800 --> 00:09:30,940 The standard was actually released in 1979. 125 00:09:31,400 --> 00:09:35,880 So the VT100 was built in development of that standard. 126 00:09:36,080 --> 00:09:39,080 And actually released it before the standard was released. 127 00:09:39,300 --> 00:09:41,580 So very, very close together development. 128 00:09:41,820 --> 00:09:45,240 I haven't looked that closely at the history there, but it's quite interesting. 129 00:09:45,240 --> 00:09:48,240 But what does the VT100 give us? 130 00:09:48,580 --> 00:09:51,320 So it gives us escape sequences like this. 131 00:09:51,580 --> 00:09:54,340 So I'm using C-style strings here. 132 00:09:54,460 --> 00:09:56,640 So slash E is the escape character. 133 00:09:56,900 --> 00:09:59,880 And then the other things you see are just the symbols you expect them to be. 134 00:10:00,300 --> 00:10:04,420 So like I said, a particular sequence is defined. 135 00:10:04,700 --> 00:10:10,500 So here, the left bracket opens a particular kind of escape sequence. 136 00:10:10,880 --> 00:10:13,600 Then you can have numbers, semicolons. 137 00:10:13,600 --> 00:10:15,140 And then usually a letter. 138 00:10:15,240 --> 00:10:16,180 It ends the sequence. 139 00:10:17,080 --> 00:10:22,200 So here, the top one sets the top and bottom margins, which is a deck-specific one based 140 00:10:22,200 --> 00:10:22,780 on the name. 141 00:10:23,640 --> 00:10:26,620 The bottom two are positioning the cursor. 142 00:10:26,840 --> 00:10:28,240 And those are ANSI standard ones. 143 00:10:28,560 --> 00:10:30,420 They don't have a deck prefix to the name. 144 00:10:32,280 --> 00:10:34,040 So we can actually just demo this. 145 00:10:34,220 --> 00:10:39,240 So like on a VT100 emulator, we can just, in a command line, we can run printf. 146 00:10:39,580 --> 00:10:41,860 And this will do something. 147 00:10:46,500 --> 00:10:47,980 Sorry, this bit is not. 148 00:10:51,040 --> 00:10:53,240 So I'm actually using OpenBSD to demo this. 149 00:10:53,520 --> 00:10:55,720 So here I am just logging in as root, as you do. 150 00:10:56,820 --> 00:10:59,480 But a console is a VT100 emulator. 151 00:10:59,920 --> 00:11:01,640 And here I can just type printf. 152 00:11:03,060 --> 00:11:05,760 And I've now limited myself to the top ten lines of the screen. 153 00:11:05,860 --> 00:11:11,620 So if I type clear, you can now see I'm still hitting enter, but I don't go further down 154 00:11:11,620 --> 00:11:11,920 the screen. 155 00:11:12,460 --> 00:11:15,220 But I can use an escape sequence to move further down the screen. 156 00:11:15,220 --> 00:11:20,460 So I've now moved myself outside that restricted region using the deck escape sequence. 157 00:11:20,720 --> 00:11:22,260 Now I can move 2,000 lines down. 158 00:11:23,000 --> 00:11:26,220 So OpenBSD just rebooted when I moved 2,000 lines down then. 159 00:11:26,800 --> 00:11:28,280 So what just happened? 160 00:11:31,320 --> 00:11:38,220 So have you tried forcing an unexpected reboot? 161 00:11:42,180 --> 00:11:43,300 So, okay. 162 00:11:44,160 --> 00:11:44,720 So. 163 00:11:46,700 --> 00:11:48,780 That was a present-day OpenBSD bug. 164 00:11:49,080 --> 00:11:51,980 It resulted in this reliability fix in early 2023. 165 00:11:52,440 --> 00:11:59,980 And I found that basically the VT100 emulation, which was originally written for NetBSD in 166 00:11:59,980 --> 00:12:02,840 1998, I don't think anyone had ever really audited it. 167 00:12:03,100 --> 00:12:08,440 So basically, yeah, you asked it to move around the screen, and there was some kind 168 00:12:08,440 --> 00:12:13,080 of, it was basically just a bounds checking error, as it says there. 169 00:12:13,440 --> 00:12:14,700 I later found another bug. 170 00:12:14,700 --> 00:12:17,900 So there was a further reliability fix while I was writing this talk. 171 00:12:18,020 --> 00:12:19,000 I was like, that's strange. 172 00:12:19,260 --> 00:12:26,160 So it turns out also, if you look at those escape sequences, I said you can have numbers 173 00:12:26,160 --> 00:12:27,300 and semicolons and things. 174 00:12:27,600 --> 00:12:31,760 It turns out if you just send four gigabytes of semicolons, you overflowed something. 175 00:12:33,820 --> 00:12:38,140 Both of these will either cause a kernel panic or a reboot, depending on exactly how they're 176 00:12:38,140 --> 00:12:38,480 arranged. 177 00:12:39,720 --> 00:12:41,740 I don't believe they're exploitable further. 178 00:12:42,040 --> 00:12:43,680 I'm sure someone might prove me wrong. 179 00:12:44,080 --> 00:12:44,680 But, yeah. 180 00:12:44,680 --> 00:12:51,660 But I would expect that address randomization and other recent additions to kernels limit 181 00:12:51,660 --> 00:13:01,060 a lot of this, and also on a 64-bit system, you're probably far enough away and you can't 182 00:13:01,060 --> 00:13:03,000 actually set more than 32-bit parameters. 183 00:13:03,220 --> 00:13:08,380 So maybe a 32-bit system without any address randomization might be exploitable. 184 00:13:08,740 --> 00:13:12,540 So if you went back to when this code was written in 1998 and found this bug, it would 185 00:13:12,540 --> 00:13:14,520 probably be far more interesting than it is right now. 186 00:13:14,680 --> 00:13:17,940 But no one found it then, fortunately or unfortunately. 187 00:13:18,340 --> 00:13:25,060 I mean, it's interesting that this bug has existed in OpenBSD and NetBSD for 25 years. 188 00:13:25,540 --> 00:13:26,380 But there you go. 189 00:13:26,840 --> 00:13:31,700 So let's go to the 1980s now, PCs. 190 00:13:32,200 --> 00:13:43,320 So DOS 2.0 introduced a driver called ANSI.SYS, and actually this is a screenshot from PCJS.org, 191 00:13:43,420 --> 00:13:44,000 which is awesome. 192 00:13:44,680 --> 00:13:49,920 Web-based PC emulator, and it actually is quite a faithful emulation of PCs. 193 00:13:50,080 --> 00:13:52,520 It's not just like trying to make sure that XX6 works. 194 00:13:52,720 --> 00:13:55,360 It really does have the IBM BIOS and some other things. 195 00:13:55,660 --> 00:13:58,240 So you can go there, type in those commands. 196 00:13:58,680 --> 00:14:03,060 And if you see at the bottom where I've typed CLS and typed it to more, that's actually 197 00:14:03,060 --> 00:14:04,240 something that is built into DOS. 198 00:14:04,320 --> 00:14:11,620 If you type CLS and it's not directly connected to the screen, then it will output the escape 199 00:14:11,620 --> 00:14:12,100 sequence. 200 00:14:12,440 --> 00:14:14,360 In this case, I've not actually rebooted. 201 00:14:14,360 --> 00:14:20,540 After I've said to load ANSI.SYS, so this is the default DOS behavior. 202 00:14:20,680 --> 00:14:22,440 You just see the escape character on the screen. 203 00:14:22,620 --> 00:14:32,480 If you do this on the PCJS emulator, you reboot, then it will actually run that code. 204 00:14:32,860 --> 00:14:36,080 So that's how that works. 205 00:14:39,740 --> 00:14:42,300 Unfortunately, I've minor technical issues. 206 00:14:42,340 --> 00:14:43,820 This particular demo is not going to work. 207 00:14:44,840 --> 00:14:49,940 So I was going to demo what happens with ANSI.SYS on a more recent DOS. 208 00:14:50,120 --> 00:14:58,500 So basically, bulletin boards and things use ANSI.SYS. 209 00:14:58,880 --> 00:15:04,640 And it was kind of awesome, you know, combined with this particular IBM code page. 210 00:15:04,760 --> 00:15:06,200 This is IBM code page 437. 211 00:15:06,660 --> 00:15:11,320 So I mentioned that what ASCII is. 212 00:15:11,480 --> 00:15:13,560 But if we look at the second half of this. 213 00:15:14,880 --> 00:15:19,480 There's some custom characters, and then these are the IBM PC specific characters. 214 00:15:19,740 --> 00:15:26,780 And these, combined with the escape sequences that the ANSI.SYS driver gives you, allows 215 00:15:26,780 --> 00:15:28,020 you to do art like this. 216 00:15:28,260 --> 00:15:34,320 This was taken from a Dreams of Insanity bulletin board, the intro to it. 217 00:15:35,340 --> 00:15:39,480 And you can find this online on textfiles.com or other places. 218 00:15:39,960 --> 00:15:43,540 So how you can actually view this on a modern computer is. 219 00:15:43,540 --> 00:15:47,620 You can actually use iConv to convert from IBM code page 437. 220 00:15:48,800 --> 00:15:53,760 And then you can pipe it to PV, which is a command line tool that can do various pipe-like 221 00:15:53,760 --> 00:15:54,640 things on Unix. 222 00:15:54,820 --> 00:15:59,520 And in this case, I'm asking it to slow down enough that it behaves like a 28K modem. 223 00:15:59,680 --> 00:16:02,820 And if you run that, unfortunately, the demo is not working for reasons. 224 00:16:03,100 --> 00:16:07,840 But basically, you'll see the output just below it. 225 00:16:08,860 --> 00:16:10,760 And what that actually looks like. 226 00:16:10,840 --> 00:16:13,300 When you look at it. 227 00:16:13,300 --> 00:16:15,200 In a raw form, it's something like this. 228 00:16:15,480 --> 00:16:20,740 So you can see the text is actually sort of here and here. 229 00:16:20,920 --> 00:16:22,540 But there's all these control characters here. 230 00:16:22,860 --> 00:16:26,400 So it's kind of hard if you actually just open this in an editor to see what it's doing. 231 00:16:27,040 --> 00:16:29,600 And that, of course, brings us to a potential attack. 232 00:16:30,360 --> 00:16:32,120 So there's something called ANSI bombs. 233 00:16:32,880 --> 00:16:36,920 Now the ANSI.SYS driver had a special ability to redefine keys. 234 00:16:37,420 --> 00:16:41,980 So if you could write something to someone's screen, you could redefine keys. 235 00:16:42,480 --> 00:16:42,960 Now. 236 00:16:43,440 --> 00:16:47,340 What I'm showing there is actually still detected by some antiviruses. 237 00:16:47,400 --> 00:16:48,640 That is a real virus. 238 00:16:49,620 --> 00:16:53,580 And it basically says for particular keys, redefine them. 239 00:16:53,860 --> 00:16:57,980 So if you type this to the screen like it tells you to, you do type help.txt, which 240 00:16:57,980 --> 00:17:02,080 is for those who have forgotten how DOS works, that's the same as cat to the screen, basically. 241 00:17:02,420 --> 00:17:04,180 So raw cat is a bad idea. 242 00:17:04,280 --> 00:17:07,940 Raw type is a bad idea on DOS a long time ago. 243 00:17:08,940 --> 00:17:10,740 So anyway, you do that. 244 00:17:10,840 --> 00:17:11,460 And then you hit enter. 245 00:17:11,900 --> 00:17:13,200 And what that actually expands. 246 00:17:13,300 --> 00:17:14,540 To is a DAO tree. 247 00:17:14,720 --> 00:17:24,460 So if we just go back, each one of these, the first part of the sequence is which character 248 00:17:24,460 --> 00:17:25,860 to redefine. 249 00:17:26,100 --> 00:17:28,780 Sorry about that. 250 00:17:29,480 --> 00:17:31,360 So that's which character to redefine. 251 00:17:32,720 --> 00:17:37,720 And if we go right back to the ANSI table, which I won't do right now, but you'll see 252 00:17:37,720 --> 00:17:42,260 that my favorite one here is so 27 is escape. 253 00:17:42,520 --> 00:17:43,280 We'll talk about that. 254 00:17:43,300 --> 00:17:43,780 A bit later. 255 00:17:44,060 --> 00:17:45,160 Eight is a good one. 256 00:17:45,240 --> 00:17:45,900 That's backspace. 257 00:17:46,140 --> 00:17:51,000 So you think you've made a mistake and then you press backspace and this silly, basically 258 00:17:51,000 --> 00:17:51,740 this is a prank. 259 00:17:52,260 --> 00:17:55,600 It's, you know, bombs are always a prank. 260 00:17:55,880 --> 00:18:03,480 But anyway, it redefines certain keys if you cat this to your screen. 261 00:18:03,600 --> 00:18:07,160 And maybe you don't immediately realize this, there were many different variants of this. 262 00:18:07,340 --> 00:18:10,540 Like some were sort of weird and you maybe didn't even touch that key. 263 00:18:10,740 --> 00:18:12,020 So you didn't know it happened. 264 00:18:12,020 --> 00:18:14,500 And then later on, you randomly press it. 265 00:18:14,600 --> 00:18:21,300 So this wasn't really considered a real security thing back in the day, but it's kind of amusing 266 00:18:21,300 --> 00:18:23,620 because it's a predecessor to some of the things I'm going to get to. 267 00:18:24,380 --> 00:18:27,500 So yeah, probably bad deleting your C drive. 268 00:18:28,240 --> 00:18:33,340 And this works, as you can see here, this was on DOS 622, I demoed this, but it worked 269 00:18:33,340 --> 00:18:37,920 all the way back to DOS 2 if you loaded the ANSI sys driver, which not everyone did. 270 00:18:38,260 --> 00:18:41,700 And there were also secure versions of the ANSI sys driver. 271 00:18:42,020 --> 00:18:46,180 But obviously this was the 90s and the 80s, so CVEs weren't really a thing and stuff 272 00:18:46,180 --> 00:18:46,560 like that. 273 00:18:46,780 --> 00:18:52,400 So, you know, some of these attacks that I'm talking about are older than you may realize. 274 00:18:57,460 --> 00:18:59,420 So let's go to the 90s. 275 00:18:59,680 --> 00:19:06,260 So this was from FRAC in 1994, and it's a utility called flash.c. 276 00:19:07,780 --> 00:19:11,900 And so it says here, it's intended to quickly mess up a user's terminal. 277 00:19:12,020 --> 00:19:15,480 By issuing a talk request and sending special characters. 278 00:19:15,720 --> 00:19:16,920 And it says it's really nasty. 279 00:19:17,260 --> 00:19:21,020 So let's have a look at what that actually is. 280 00:19:21,380 --> 00:19:23,820 So it sends these evil strings. 281 00:19:24,660 --> 00:19:29,040 So I won't go into exactly what they all do, but one of the ones that I mentioned 282 00:19:29,040 --> 00:19:34,560 earlier was the limiting to the top ten lines of the screen. 283 00:19:34,660 --> 00:19:36,600 Well this is limiting you to the top three lines of the screen. 284 00:19:36,840 --> 00:19:40,680 So already that's quite annoying because you've got a really large terminal and you can only 285 00:19:40,680 --> 00:19:41,360 see three lines. 286 00:19:42,020 --> 00:19:44,680 It also inverts the background color. 287 00:19:45,720 --> 00:19:51,080 One of them is a deck specific sequence that sends a test pattern to the screen, which 288 00:19:51,080 --> 00:19:52,740 basically means it fills the screen with ease. 289 00:19:53,340 --> 00:19:54,760 So yeah. 290 00:19:55,720 --> 00:20:00,280 But just a quick aside, because if you looked at this, you see that says slash 033. 291 00:20:01,160 --> 00:20:02,460 So that's octal. 292 00:20:02,540 --> 00:20:09,540 So 033 is actually 27 in character numbers in decimal. 293 00:20:09,860 --> 00:20:11,860 So there are many ways. 294 00:20:11,860 --> 00:20:12,580 There are ways to represent escape. 295 00:20:12,900 --> 00:20:15,700 I've already in this presentation shown three at the top. 296 00:20:18,120 --> 00:20:23,120 But you can also use JSON and Stock's talk mentioned that you probably should escape 297 00:20:23,120 --> 00:20:25,500 certain outputs of the screen using that. 298 00:20:25,720 --> 00:20:28,140 So yeah, you can do that in JSON. 299 00:20:28,800 --> 00:20:33,000 If you're writing stuff in Go, then it doesn't do C-style strings. 300 00:20:33,120 --> 00:20:39,520 So slash X1B and in some cases just slash 27 and we'll get to some other variants on 301 00:20:39,520 --> 00:20:40,100 this in a moment. 302 00:20:40,880 --> 00:20:41,560 But yeah. 303 00:20:41,560 --> 00:20:46,120 So if you see this in various different forms, just be aware that there are many ways to 304 00:20:46,120 --> 00:20:47,040 represent escape. 305 00:20:48,300 --> 00:20:49,560 So hello, DEF CON. 306 00:20:50,320 --> 00:20:53,180 This is a VT520. 307 00:20:53,620 --> 00:20:54,880 My dog also says hi. 308 00:20:55,700 --> 00:20:58,680 So November 1995, this was released. 309 00:20:59,120 --> 00:21:01,900 Now Windows 95 was obviously released before this. 310 00:21:01,980 --> 00:21:06,280 So this was newish tech for the time. 311 00:21:06,360 --> 00:21:09,480 It did some fancy things, but in the terminal world, it was new. 312 00:21:09,600 --> 00:21:10,960 In the real computer world. 313 00:21:11,560 --> 00:21:12,760 Most people were actually using X terms. 314 00:21:13,180 --> 00:21:18,600 And if we go back to this Flash.C, it says in the middle that it's actually expecting 315 00:21:18,600 --> 00:21:19,640 to target an X term. 316 00:21:20,420 --> 00:21:24,020 But let's just be time appropriate here. 317 00:21:24,480 --> 00:21:33,360 So what happens if we simulate receiving Flash.C terminal DOS attack? 318 00:21:35,380 --> 00:21:35,980 Oops. 319 00:21:40,160 --> 00:21:40,760 Sorry. 320 00:21:40,760 --> 00:21:41,080 Sorry. 321 00:21:48,900 --> 00:21:49,180 Sorry. 322 00:21:50,420 --> 00:21:50,700 Okay. 323 00:21:54,220 --> 00:21:55,540 I'm just going to have to switch inputs. 324 00:21:55,700 --> 00:21:58,240 This is going to be a bit crazy because I thought I had this video copied. 325 00:21:58,340 --> 00:21:58,700 Oh, don't. 326 00:22:01,220 --> 00:22:01,500 Okay. 327 00:22:10,020 --> 00:22:10,580 That works. 328 00:22:11,260 --> 00:22:11,540 Oh. 329 00:22:18,820 --> 00:22:23,080 Yes, I'm sorry this flickers both because the video recording of this is kind of bad, but also 330 00:22:23,540 --> 00:22:30,340 there's an issue with the projector. But what happens is the screen goes blank anyway. So this 331 00:22:30,340 --> 00:22:34,560 is kind of annoying. So if you send this to a real terminal, a reboot of it takes several 332 00:22:34,560 --> 00:22:38,980 seconds. So you literally send escape and C and the real terminal breaks. If we remove the 333 00:22:38,980 --> 00:22:43,780 escape and C and we send that, then we see roughly what we would have seen in Xterm or another 334 00:22:43,780 --> 00:22:49,940 terminal. So it starts flashing a lot and inverts the background. And now when we start typing, we 335 00:22:49,940 --> 00:22:57,560 also see our characters are not the normal characters. So there's a special alphabet that is 336 00:22:58,100 --> 00:23:05,620 designed for box drawing and things, a bit like we saw the PC had its own. So, yeah, we then went 337 00:23:05,620 --> 00:23:07,560 reset and we can see what happened. 338 00:23:07,820 --> 00:23:08,760 But notice reset. 339 00:23:08,980 --> 00:23:13,720 It didn't reset the fact that the screen was inverted. So this really messed up your terminal. 340 00:23:13,760 --> 00:23:24,320 Send these things to the terminal and it's kind of broken. So, yeah. So let's go to 2003. So HD 341 00:23:24,320 --> 00:23:29,780 Moore did some research and published this article in Bug Track and Full Disclosure at the time. 342 00:23:30,020 --> 00:23:36,360 Terminal emulator security issues. And now this is some of the first proper research. CVEs were 343 00:23:36,360 --> 00:23:37,800 still relatively new at the time. 344 00:23:37,960 --> 00:23:38,900 That's in quotes. 345 00:23:38,980 --> 00:23:46,440 Because it was rather than CVEs, it was actually CANs. It's just going to go blank for a second. So these were 346 00:23:46,440 --> 00:24:02,600 CANs and they later became CVEs. So is that going to come back? Let me just type XR on our commands for a 347 00:24:02,600 --> 00:24:02,860 moment. 348 00:24:20,880 --> 00:24:21,360 , 349 00:24:22,540 --> 00:24:23,020 ? 350 00:24:23,020 --> 00:24:31,180 So, yeah. So this was among the first published CVEs. And so one of the ones in here is about Window Title 351 00:24:31,180 --> 00:24:38,020 Reporting. So I've mentioned this in some previous talks. So sorry if you've seen that before. But I think it's 352 00:24:38,020 --> 00:24:38,960 important to repeat this. 353 00:24:38,960 --> 00:24:45,440 some of this. So what does this look like? So if we print something to the screen, like 354 00:24:45,440 --> 00:24:49,740 I showed before, you can print escape sequences to change positions on the screen and move 355 00:24:49,740 --> 00:24:55,620 things around. This is a special escape sequence to set the title. So your shell prompt will 356 00:24:55,620 --> 00:25:01,320 often include the command to set the title, for example. So what happens if we set it 357 00:25:01,320 --> 00:25:07,720 to calc.exe? Well, that's not bad. But some terminals have a special sequence to ask for 358 00:25:07,720 --> 00:25:12,980 a reply of what the title is. So you ask for what the title is, it replies with what the 359 00:25:12,980 --> 00:25:26,440 title is. So then this looks something a bit like this. Which one are we doing? This one. 360 00:25:28,020 --> 00:25:35,080 So we do something like this, and calc.exe opens back here. So it's literally that simple. 361 00:25:36,840 --> 00:25:50,360 But, yeah. So this is interesting. This wasn't an odd vulnerability. I just ran that 362 00:25:50,360 --> 00:25:57,500 against con EMU, which is a Windows terminal. So that vulnerability from Xterm also exists on 363 00:25:57,500 --> 00:26:04,360 con EMU and Swiftterm and Westterm. The con EMU one is the most concerning, though, because 364 00:26:05,080 --> 00:26:13,600 actually what I did back here, if you look closely, I put a slash X zero D, which is also known 365 00:26:13,600 --> 00:26:18,900 as carried return. So in this case, I didn't have to press enter. The terminal pressed enter for 366 00:26:18,900 --> 00:26:25,840 me. That makes these attacks much more scary. It's either before you had to social engineer the 367 00:26:25,840 --> 00:26:33,600 user into pressing enter somehow. But it turns out actually you don't always have to do that. So 368 00:26:35,080 --> 00:26:39,760 there are other ways to deliver this. I use printf. Obviously, a user running printf is a bit like 369 00:26:39,760 --> 00:26:43,900 someone in a JavaScript console. Ask them to open the JavaScript console and attack themselves. 370 00:26:45,900 --> 00:26:51,540 But you can run curl on things. And curl is just like cutting something to the screen. So here I 371 00:26:51,540 --> 00:26:59,220 have an evil example.com that when you query it and get the code with curl, it says, I am good. 372 00:26:59,360 --> 00:27:03,980 You pipe it to shell, and it says, I am evil. And down at the bottom, I've piped it to cat minus v, 373 00:27:04,120 --> 00:27:05,000 so you can see what's happening. 374 00:27:05,080 --> 00:27:11,680 And actually, there's a comment and then a lot of backspace characters. So basically, what you see 375 00:27:12,240 --> 00:27:19,140 is I am good when you cat it. But what the shell sees is I am evil followed by a comment. So you can 376 00:27:19,140 --> 00:27:24,280 basically make the user run one, see that they're going to run one thing, and then run something 377 00:27:24,280 --> 00:27:28,320 different. This is actually kind of interesting because some recent attacks in the past year were 378 00:27:28,320 --> 00:27:34,940 called trojan source, where people used Unicode to achieve similar effects in text 379 00:27:35,080 --> 00:27:40,320 editors. So these kind of things have been possible in basically anywhere you have text. Sometimes 380 00:27:40,320 --> 00:27:45,940 text is not just pure ASCII. There are other things in it. Escape characters, Unicode, right to left 381 00:27:45,940 --> 00:27:53,740 overrides, other things. So these things can apply in various ways. So cat minus v is considered 382 00:27:53,740 --> 00:28:02,640 good. In the 80s, the original Unix people from Bell Labs wrote an article called cat minus v 383 00:28:02,640 --> 00:28:05,060 considered harmful because they thought that cat should be considered good. So they thought that cat 384 00:28:05,080 --> 00:28:08,240 shouldn't actually have any command line options and it should be its own thing, which is kind of 385 00:28:08,240 --> 00:28:14,400 amusing. But cat minus v is a good idea. Anything that can potentially write raw text to your 386 00:28:14,400 --> 00:28:19,580 terminal is harmful, so you can pipe it to cat minus v and see what's happening. Alternatively, 387 00:28:19,820 --> 00:28:26,680 the program itself, if it expects to print to a terminal, should just do the escaping itself. So 388 00:28:26,680 --> 00:28:33,060 if you use curl, what else can we do? Well, it's always DNS, isn't it? So this is a bit crazy, but 389 00:28:33,520 --> 00:28:35,060 it turns out you can put lots of things in there. So if you use curl, what else can we do? Well, it's 390 00:28:35,080 --> 00:28:36,040 a bit crazy, but it turns out you can put lots of things in there. So if you use curl, what else can 391 00:28:36,040 --> 00:28:41,100 things in DNS. So the easy place to do this is text records. I can put stuff in text records. But 392 00:28:41,100 --> 00:28:48,620 it turns out I can also in many cases put funny characters just in the host name itself. The 393 00:28:48,620 --> 00:28:53,820 interesting thing about that is depending what libc you're running, it may or may not resolve. So 394 00:28:54,760 --> 00:29:00,560 what I'm sort of proposing is a slightly crazy delivery method here is someone finds out that 395 00:29:00,560 --> 00:29:04,980 they can't access this host, but someone else says they can, so they start debugging it. And 396 00:29:04,980 --> 00:29:10,420 it turns out that on Alpine, you can ping the relevant host, because it does follow the 397 00:29:10,420 --> 00:29:21,080 chain to the crazy thing. But glibc doesn't. And actually, due to unrelated vulnerability, I 398 00:29:21,600 --> 00:29:27,740 actually made OpenBSD change their behavior. So OpenBSD, older versions for about 7.3, this does 399 00:29:27,740 --> 00:29:32,520 work. Newer versions, it doesn't. You can also use this to tell the difference between Mac OS and 400 00:29:32,520 --> 00:29:34,960 Windows, potentially. So I reckon there's some, like, five or six different versions of OpenBSD. 401 00:29:34,960 --> 00:29:41,720 Fun things there. Anyway, that's kind of an aside. So what can we do with this? So Windows 402 00:29:41,720 --> 00:29:53,280 terminal, it turns out, supports some kind of music thing. So, yeah. Let's see if we can make 403 00:29:53,280 --> 00:30:02,100 this work. I just, that's not going to work, because I don't have any sound. 404 00:30:12,800 --> 00:30:19,000 Yeah, okay. So, yeah. Because I've changed device here, that sound output's not going to work. 405 00:30:19,100 --> 00:30:26,060 But anyway, so you run that in Windows, and it turns out it starts playing music here. Which is 406 00:30:26,060 --> 00:30:33,460 kind of fun. But what did I just actually show you? Well, I showed you that if you run NS lookup, 407 00:30:34,960 --> 00:30:40,600 it actually just writes raw things to the screen. So it turns out, if you actually have another bug, 408 00:30:40,900 --> 00:30:46,600 say, for example, you have a bug in a tool called Commander, which actually is based on CONEMU that 409 00:30:46,600 --> 00:31:01,720 I just spoke about. So here we are. We just do an NS lookup on CONEMU.lab. So, yeah. The 410 00:31:01,720 --> 00:31:03,580 interesting thing about this is... 411 00:31:06,360 --> 00:31:11,400 The interesting thing about this is I previously disclosed this is a bug. But actually, this still 412 00:31:11,400 --> 00:31:18,980 works on the fixed version. I had a bit of a discussion with the author of CONEMU about this. But 413 00:31:18,980 --> 00:31:25,740 if you look very closely, I'm using Control-O here, rather than pressing Enter. So it turns out 414 00:31:26,180 --> 00:31:32,980 what the author did to fix this originally was look for carriage return and line feed. But it 415 00:31:32,980 --> 00:31:34,940 turns out, if you're using a read line feed, you're going to have a bug. So if you're using a read 416 00:31:34,960 --> 00:31:41,420 line compatible shell, and this is Commander, which adds a bunch of Unix tools to Windows, then 417 00:31:42,740 --> 00:31:49,760 basically, it's... Yeah. It runs... When you press Control-O, it actually is bound to something 418 00:31:49,760 --> 00:31:54,820 like history add and accept, which is the same as pressing Enter as far as anything is concerned. 419 00:31:55,060 --> 00:32:01,120 So, yeah. That's a thing. So... Sorry. I'm going to have to speed up slightly because we've got a 420 00:32:01,120 --> 00:32:01,960 few more of those to come. 421 00:32:06,100 --> 00:32:13,360 Yes. It turns out there are other tricks you can play with DNS. So, Apple Terminal is actually 422 00:32:13,360 --> 00:32:18,960 relatively secure. I looked at it for a while and didn't find anything. But it does have a way of 423 00:32:18,960 --> 00:32:23,180 setting what your working directory is. So the way this works is the same way that I said that a 424 00:32:23,180 --> 00:32:28,120 title integrates with your shell prompt, you also integrate with your shell prompt and send the 425 00:32:28,120 --> 00:32:33,680 terminal a hidden escape sequence that tells it what working directory you're currently in, which 426 00:32:33,680 --> 00:32:34,800 is, you know, that makes sense. 427 00:32:34,960 --> 00:32:41,520 It's useful to tell the terminal what working directory you're in, because it turns out up here you 428 00:32:41,520 --> 00:32:47,420 have a thing that tells you that you're in either the temp or your home directory and it gives you a 429 00:32:47,420 --> 00:32:51,600 pretty icon of your home if you're in your home directory. Anyway, if you actually read what's 430 00:32:51,600 --> 00:32:58,440 happening on the slide here, so I can print users DGL, first of all. That works fine. I can also, it 431 00:32:58,440 --> 00:33:04,940 turns out, put a host name in the file URL. And over here, it does a DIN. So, it's a DIN that's 432 00:33:04,960 --> 00:33:11,360 a DIN lookup on that file URL. So, it's not like the end of the world, but it's an information 433 00:33:11,360 --> 00:33:19,480 leakage thing in that if I, for some reason, am connected to a server that maybe doesn't have access 434 00:33:19,480 --> 00:33:24,780 to the outside world, but the computer that I'm connecting from does, some malicious thing on that 435 00:33:24,780 --> 00:33:30,260 server could write something to my terminal and then it could actually, through DNS, tunnel it 436 00:33:30,260 --> 00:33:34,940 back to wherever. So, you can use DNS as an exfiltration method. So, you can use DNS as an 437 00:33:34,960 --> 00:33:40,860 exfiltration method via the terminal that some user is connected via. So, I reported this to Apple 438 00:33:40,860 --> 00:33:47,560 in, as you can see here, October 2022 and I unfortunately haven't fixed it yet. So, they're quite 439 00:33:47,560 --> 00:33:51,640 past the disclosure deadline for this one. It still works as far as I'm aware. I haven't tested 440 00:33:51,640 --> 00:33:57,920 the very latest version, but it turns out there's also another bug, which is kind of fun. If you 441 00:33:59,220 --> 00:34:04,940 actually get to about 1,024 characters, somewhere around 1,000, that's a lot of data. So, if you 442 00:34:04,960 --> 00:34:04,980 go back to your browser and you open up the new tab, you'll see the tab here on the screen down 443 00:34:04,980 --> 00:34:12,580 there. When you open a new tab, what it tries to do is change into that directory and this sort of CD 444 00:34:12,580 --> 00:34:18,360 command you see on screen here is what it wrote. So, it does that sort of for you. It turns out if 445 00:34:18,360 --> 00:34:24,260 it's longer than the allowed directory name length on Mac OS, then it just hangs the entire terminal. 446 00:34:24,560 --> 00:34:29,420 So, it's kind of a bit like a sleeping time bomb in that if you run that, then the terminal, next 447 00:34:29,420 --> 00:34:34,740 time someone opens a new tab, will just hang and they won't actually know why. The one saving 448 00:34:34,960 --> 00:34:39,700 there is if this actually is integrated for your prompt, the next time the prompt is displayed 449 00:34:39,700 --> 00:34:45,520 it will clear that and then it won't work. So it's not the end of the world. So back 450 00:34:46,500 --> 00:34:53,360 in HD Moore's paper in 2003, there was a fictitious case study and it spoke about Apache logs. 451 00:34:53,900 --> 00:35:00,760 What's more relevant today? So developers run things like Python for HTTP command lines 452 00:35:00,760 --> 00:35:07,340 and if you saw stocks talk, I'm afraid this is basically the same demo. But which one 453 00:35:07,340 --> 00:35:16,020 are we on? There we go. So we've got an attacker down here and a victim up the top. We can't 454 00:35:16,020 --> 00:35:22,080 put characters in via just using curl, but if we write raw to the screen, sorry, the 455 00:35:22,080 --> 00:35:27,340 screen, if we write using netcat, we can write things and it turns out Python didn't escape 456 00:35:27,340 --> 00:35:28,920 the log file. 457 00:35:30,760 --> 00:35:35,780 So we can change the color of the logs. Fine. That's kind of fun. So it turns out if we've got a 458 00:35:35,780 --> 00:35:42,080 terminal vulnerability, we can do things with that as well. This is item two. The vulnerability 459 00:35:42,080 --> 00:35:47,420 was that it would reply with a user controllable string. So what it's supposed to be used for is 460 00:35:47,420 --> 00:35:53,060 querying the color. So here it's telling us that the color is 31M red. I just changed the color 461 00:35:53,060 --> 00:36:00,740 back to the default color and it says it's zero. But it turns out, yeah, if we put something in the 462 00:36:00,760 --> 00:36:05,540 terminal, if we put something else there, then it replies up here with something else. And it 463 00:36:06,120 --> 00:36:11,860 turns out that can be anything else. So the real insight is that we can put control C there. So by 464 00:36:11,860 --> 00:36:20,080 putting control C there, it will basically kill Python. And at that point, we can run anything we 465 00:36:20,080 --> 00:36:26,560 want in the terminal. So we put control C. Because of how this works in item two, we kind of have 466 00:36:26,560 --> 00:36:30,740 to split each control character into its own escape sequence. But it turns out, by the way, 467 00:36:30,760 --> 00:36:33,900 Python just doesn't care what the HTTP request looks like. So we can just really put any 468 00:36:33,900 --> 00:36:38,140 characters we feel like in the request thing. And eventually calculator pops up. 469 00:36:38,380 --> 00:36:54,580 So that was a bug in item two that was fixed. And as I mentioned, the request selection or 470 00:36:54,580 --> 00:36:59,420 setting, which is a specific escape sequence used for querying the color and thing. There was an 471 00:36:59,420 --> 00:37:00,740 implementation bug in item two. So that was a bug in item two that was fixed. And as I mentioned, the 472 00:37:00,740 --> 00:37:05,580 communication was not very good in that. And yeah, it unexpectedly echoed a string back. Control 473 00:37:05,580 --> 00:37:10,580 C is also a part of a string. So we can press control C. And I call this a full echo back. 474 00:37:13,240 --> 00:37:27,500 So how about another one? So this is Git for Windows. Also known as Git bash. So here we're 475 00:37:27,500 --> 00:37:30,720 cloning a Git repository. We're going to do a follow-up. So let's go into the base if we can get a 476 00:37:30,740 --> 00:37:33,940 into this repository, it's called bad Git. We run Git log. 477 00:37:35,140 --> 00:37:38,380 So what does Git log normally do? Well, Git log opens 478 00:37:38,380 --> 00:37:45,960 calculator today. So if we pipe that to cat minus V, we'll see 479 00:37:45,960 --> 00:37:48,940 up here it says just below is an escape sequence and that's 480 00:37:48,940 --> 00:37:53,660 actually in the commit log of this Git repository. And it's 481 00:37:53,660 --> 00:37:56,440 also a cross-platform exploit as you might notice because this 482 00:37:56,440 --> 00:38:01,260 is actually the same bug as the item 2 one. It's deck RQSS and 483 00:38:01,260 --> 00:38:07,860 it turned out on Windows was vulnerable to this. So, yeah. 484 00:38:14,480 --> 00:38:19,760 Sorry. Slides loading slowly here, but we'll get there. So, 485 00:38:19,800 --> 00:38:23,680 yeah. So Git uses less as a pager and less had a bug in its 486 00:38:23,680 --> 00:38:26,320 OSC 8 hyperlink handling. So there's a 487 00:38:26,320 --> 00:38:26,420 way to get rid of the hyperlink handling. So there's a way 488 00:38:26,440 --> 00:38:30,620 of embedding hyperlinks in terminals now. Less supports 489 00:38:30,620 --> 00:38:33,580 this so that things like man pages can make their links in 490 00:38:33,580 --> 00:38:38,180 them clickable. It turns out that if you've got a bug in 491 00:38:38,880 --> 00:38:41,680 less and a bug in a terminal, then it means that anything you 492 00:38:41,680 --> 00:38:45,920 write to basically anywhere in Git, you can then write that 493 00:38:46,500 --> 00:38:49,460 escape characters to the screen. So the actual issue is that 494 00:38:49,980 --> 00:38:53,120 here, where I've got, if you remember earlier, I said that 495 00:38:53,760 --> 00:38:56,140 escape C is like a terminal reset thing. 496 00:38:56,140 --> 00:39:01,240 It turns out that less wasn't looking for particular escape 497 00:39:01,240 --> 00:39:06,100 characters ending something. So here, this ended it as far as 498 00:39:06,740 --> 00:39:09,700 min-tity was concerned, but it didn't end the escape sequence 499 00:39:09,700 --> 00:39:15,220 inside the OSC 8 as far as less was concerned. And the funny 500 00:39:15,220 --> 00:39:22,000 thing about this is this bug was disclosed earlier this year. In 501 00:39:22,000 --> 00:39:26,120 the posting of it, I sort of said it was a denial of service attack. And then later on, I said it was a denial of service attack. And then later on, I said it was a denial of service attack. And then later on, I said it was a denial of service attack. And then later on, 502 00:39:26,120 --> 00:39:28,640 I said it was a denial of service attack. And then later on, I said it was a denial of service attack. And then later on, I said it was a denial of service attack. And then later on, I worked out actually this can also be used for a bit more 503 00:39:29,280 --> 00:39:34,080 when I found a terminal exploit. I don't know if anyone else noticed that, but anyway. 504 00:39:35,500 --> 00:39:38,600 So we're getting a bit low on time, so I'm going to run a little bit faster here. 505 00:39:38,800 --> 00:39:43,840 So basically this DeckRQS thing I showed was actually 506 00:39:43,840 --> 00:39:47,640 an extern bug, and it's funny because it was documented incorrectly in 507 00:39:47,640 --> 00:39:52,040 the extern manual of control sequences, and then various other 508 00:39:52,040 --> 00:39:55,560 terminals have recreated similar bugs. 509 00:39:57,140 --> 00:39:59,880 So let's just go to a slightly different thing. 510 00:40:00,200 --> 00:40:04,580 So we've got a shared system, administrator gets an alert about excessive memory usage, 511 00:40:04,860 --> 00:40:05,540 starts debugging. 512 00:40:06,000 --> 00:40:07,300 What command do you run? 513 00:40:09,760 --> 00:40:11,240 Top, someone said, good. 514 00:40:13,340 --> 00:40:15,200 So what if there was a bug in top? 515 00:40:24,100 --> 00:40:25,380 Which video is it? 516 00:40:25,520 --> 00:40:26,080 That one. 517 00:40:26,760 --> 00:40:31,720 So here I've got top, oops, that went too quickly, anyway, I've got a script running 518 00:40:31,720 --> 00:40:35,480 on the, sorry, we'll try that again. 519 00:40:35,800 --> 00:40:37,560 I forgot how short this video was. 520 00:40:38,240 --> 00:40:45,180 So here we're running top minus capital O, sorry, minus O capital O. 521 00:40:45,200 --> 00:40:48,320 And on the left-hand side we've got a little script running. 522 00:40:49,440 --> 00:40:50,600 And what just happened? 523 00:40:50,800 --> 00:40:51,620 Well, you got owned. 524 00:40:52,620 --> 00:40:57,260 So it turns out this is an Xterm bug in its optional graphic support. 525 00:41:05,500 --> 00:41:07,820 So basically we can do something like this. 526 00:41:08,680 --> 00:41:12,020 So we can get escape sequences into top. 527 00:41:13,720 --> 00:41:14,220 Okay. 528 00:41:14,620 --> 00:41:15,120 So. 529 00:41:15,120 --> 00:41:21,580 Regis is a particular graphics thing that Digital invented in the 80s, and some of their 530 00:41:21,580 --> 00:41:22,720 terminals supported this. 531 00:41:23,120 --> 00:41:28,740 So if you tell Xterm to run in compatibility mode of one of those terminals, and then you 532 00:41:28,740 --> 00:41:35,480 print some special sequence, it replies with something like A1 equals XXX, and the exploit 533 00:41:35,480 --> 00:41:39,220 essentially is you can build up a, you don't get very many characters, but you can build 534 00:41:39,220 --> 00:41:42,140 up an environment variable if you run about ten different processes. 535 00:41:43,640 --> 00:41:45,040 So it's a bit obscure. 536 00:41:45,040 --> 00:41:47,920 The Xterm regis support isn't default. 537 00:41:48,260 --> 00:41:57,620 Some distributions enable it, hello, Nixos, but top 3.x doesn't do escaping, 4.x does. 538 00:41:57,860 --> 00:41:58,960 Very few distros have it. 539 00:41:59,520 --> 00:42:02,100 I failed to convince the top authors that this is a security problem. 540 00:42:02,380 --> 00:42:05,460 As far as they're concerned, there are other ways to spoof things in top. 541 00:42:05,700 --> 00:42:07,700 So it's actually the terminal's problem. 542 00:42:08,620 --> 00:42:11,680 And this can also be used as a Docker escape. 543 00:42:11,940 --> 00:42:15,020 So if you're running as the administrator outside of Xterm. 544 00:42:15,040 --> 00:42:17,980 And you run top, you still see processes inside Docker. 545 00:42:18,220 --> 00:42:21,400 So potentially there are other avenues to this. 546 00:42:23,560 --> 00:42:26,020 But we can also attack Kubernetes here. 547 00:42:26,620 --> 00:42:29,580 So let's have a look at Windows Terminal. 548 00:42:33,000 --> 00:42:34,580 So this is Windows Terminal. 549 00:42:36,040 --> 00:42:42,680 Here I'm just running BusyBox, and deliberately running this as a nobody user, so this is, 550 00:42:42,700 --> 00:42:43,820 you know, not... 551 00:42:46,140 --> 00:42:47,820 No special privileges here. 552 00:42:48,060 --> 00:42:50,240 But there is this interesting file called termination log. 553 00:42:51,200 --> 00:42:57,340 And so if we write an exploit to termination log, and then actually we just exit. 554 00:42:57,520 --> 00:42:59,240 For whatever reason, this container's just died. 555 00:42:59,380 --> 00:43:01,800 So the administrator starts debugging what's happening here. 556 00:43:02,340 --> 00:43:03,700 So how do you debug Kubernetes? 557 00:43:03,880 --> 00:43:04,580 You run kubectl. 558 00:43:05,500 --> 00:43:07,100 So there's this pod running. 559 00:43:07,680 --> 00:43:09,360 It just said it's restarted. 560 00:43:09,420 --> 00:43:12,180 So maybe the administrator got an alert it restarted or something. 561 00:43:13,220 --> 00:43:14,940 So the first thing you run is kubectl. 562 00:43:15,040 --> 00:43:16,240 So it will describe that pod. 563 00:43:17,300 --> 00:43:18,880 And then nothing really happens. 564 00:43:19,880 --> 00:43:22,140 There's no message, which is a bit strange. 565 00:43:22,660 --> 00:43:25,100 But yeah, the victim over here doesn't really know what's happening. 566 00:43:25,500 --> 00:43:26,600 They start debugging a bit more. 567 00:43:26,720 --> 00:43:29,780 So they decide that they're going to duplicate this tab so they can have more context. 568 00:43:32,240 --> 00:43:33,840 So this was... 569 00:43:42,440 --> 00:43:43,480 I'm going to skip that one. 570 00:43:43,920 --> 00:43:45,020 So that was a Windows Terminal. 571 00:43:45,040 --> 00:43:45,380 That was a Windows Terminal bug. 572 00:43:47,280 --> 00:43:50,140 But what can we do with documented escape characters? 573 00:43:50,980 --> 00:43:54,600 Well, it turns out there's certain replies that do interesting things. 574 00:43:55,100 --> 00:44:02,720 So if, for example, we ask what color something is, colors in some terminals are separated 575 00:44:02,720 --> 00:44:04,180 with slashes. 576 00:44:05,920 --> 00:44:08,100 And where can we find lack of escaping? 577 00:44:08,520 --> 00:44:12,680 So we can put files on disk and see if things do that. 578 00:44:13,100 --> 00:44:14,520 It turns out NMCLI. 579 00:44:14,520 --> 00:44:18,160 On Linux, if we happen to connect to a wireless network with an escape character in it, we'll 580 00:44:18,160 --> 00:44:18,740 happily... 581 00:44:18,740 --> 00:44:22,360 I've got a network at home called Red, which is actually the color red. 582 00:44:23,780 --> 00:44:26,560 Bluetooth CTL on Linux mostly doesn't escape things. 583 00:44:27,640 --> 00:44:28,840 Some of these are hard to exploit. 584 00:44:28,900 --> 00:44:29,980 You don't get very many characters. 585 00:44:30,160 --> 00:44:34,600 But like I showed at the top, you could maybe create lots and lots of devices or networks 586 00:44:34,600 --> 00:44:35,080 or something. 587 00:44:35,380 --> 00:44:37,240 And, you know, comment forms on website. 588 00:44:38,120 --> 00:44:39,900 See Stock's talk for more on that. 589 00:44:40,420 --> 00:44:41,540 But let's have a quick demo. 590 00:44:41,760 --> 00:44:42,840 I'm running out of time. 591 00:44:42,880 --> 00:44:44,340 So I'm going a little bit faster. 592 00:44:44,520 --> 00:44:45,140 More than expected here. 593 00:44:45,300 --> 00:44:52,180 But here I've got a script that sets up a particular thing in slash TMP. 594 00:44:52,500 --> 00:44:55,380 And so now I'm just typing CD slash TMP. 595 00:44:56,960 --> 00:45:01,360 And I hit tab, like you often do, right, to see what exists in that directory. 596 00:45:02,340 --> 00:45:04,000 So when I hit tab, what happens? 597 00:45:04,080 --> 00:45:05,180 My cursor disappears. 598 00:45:05,380 --> 00:45:05,840 That's strange. 599 00:45:07,620 --> 00:45:08,980 So I'm not sure what happens. 600 00:45:09,060 --> 00:45:11,660 I press control C. And then I hit enter. 601 00:45:11,800 --> 00:45:13,500 And everything goes red. 602 00:45:13,580 --> 00:45:14,500 And it all gets a bit broken. 603 00:45:14,500 --> 00:45:18,420 So what just happened there? 604 00:45:24,260 --> 00:45:26,040 So the slides for this will be up later. 605 00:45:26,180 --> 00:45:27,040 I'm going to skip this quickly. 606 00:45:27,260 --> 00:45:29,080 But basically you hide the cursor. 607 00:45:29,360 --> 00:45:30,620 You ask what color it is. 608 00:45:30,760 --> 00:45:36,160 And because you're in a control directory slash TMP, you created a file that the shell 609 00:45:36,630 --> 00:45:37,440 printed out on screen. 610 00:45:37,880 --> 00:45:43,380 And basically, magically, the user basically has to hit enter. 611 00:45:43,560 --> 00:45:44,020 You make it. 612 00:45:44,500 --> 00:45:48,500 And then you use the escape sequence for xterm that makes it so control C just writes some 613 00:45:48,500 --> 00:45:49,660 escape characters to the screen. 614 00:45:49,900 --> 00:45:54,000 So there's literally nothing you can do except close the terminal or press enter. 615 00:45:54,240 --> 00:45:57,820 So you social engineer the user into they probably press enter because they think their 616 00:45:57,820 --> 00:45:58,420 terminal's broken. 617 00:46:00,200 --> 00:46:02,340 So how do you protect yourself from all this? 618 00:46:02,720 --> 00:46:04,760 So terminals are just like a browser. 619 00:46:04,920 --> 00:46:06,120 They deal with untrusted inputs. 620 00:46:06,820 --> 00:46:08,180 Ensure correct output escaping. 621 00:46:08,500 --> 00:46:10,380 C, this talk, stock talk. 622 00:46:11,500 --> 00:46:12,280 Secure settings. 623 00:46:12,700 --> 00:46:13,600 That's a fun one. 624 00:46:14,500 --> 00:46:17,400 turn to. Just go and turn this on right now unless you actually use the shell integration 625 00:46:17,400 --> 00:46:25,780 features. There might be bugs there. Who knows? Also, ST is a cool terminal. You should use 626 00:46:25,780 --> 00:46:28,900 it. It's like really basic. If you don't actually want the features your terminal gives you, 627 00:46:28,980 --> 00:46:34,740 then you limit your exposure by using basic things. There's probably some end days here. 628 00:46:37,340 --> 00:46:42,680 So I'm crazy, and if you've got a phone or something, try a search into that particular 629 00:46:44,160 --> 00:46:53,880 IP address. It's on the DEF CON network. It will test your terminal. And I want to make 630 00:46:53,880 --> 00:47:00,480 this repository public. Yeah, yeah, yeah. How many buttons do you have to press to do 631 00:47:00,480 --> 00:47:09,360 that? Really? Okay. Anyway, so I just published that. 632 00:47:16,180 --> 00:47:21,800 So in writing that tool, I found another bug. Yeah. That's all the bugs I found. 633 00:47:21,800 --> 00:47:23,220 Thank you very much. 634 00:47:23,220 --> 00:47:25,440 Thank you. 635 00:47:25,440 --> 00:47:32,920 Thank you.