00:00:00.000-->00:00:05.005 >>Hello, I’m Egypt. >>Hi, I’m WVU. >>And together we are NCIS 2 idiots on 1 keyboard. 00:00:08.175-->00:00:13.180 >>Absolutely. [laughter] >>So this talk is intended to be bash basics and basically 00:00:18.518-->00:00:23.524 accelerating hacking through bash. We originally submitted this as a DEFCON 101 talk. Um, 00:00:25.893-->00:00:31.932 but they put us in this room and um it’s a little bigger than I was expecting. [laughter] So 00:00:31.932-->00:00:36.937 we’re going to be switching back and forth between slides and a shell to demo as much of this as 00:00:42.709-->00:00:47.714 possible. Um, I hope that goes okay. We’ve tested it but DEFCON. This isn’t Linux system 00:00:54.721-->00:01:01.094 administration. Uh, but there’s a bit of overlap there. Like Linux system administrators have 00:01:01.094-->00:01:06.099 to use bash a lot so a lot of what we’re going to be talking about fits in that same realm. 00:01:09.136-->00:01:12.372 Many of the things that we’re going to show you are things that you would use as an 00:01:12.372-->00:01:18.445 administrator. Bu ta lot of them are also going to be things that only hackers would really care 00:01:18.445-->00:01:22.883 about. So we will get that when we get there. Um but we are definitely not going to be 00:01:22.883-->00:01:27.888 reading the man page. Because, >>You can read it yourself. >>Yeah. Um, and man bash is 00:01:30.791-->00:01:35.796 cavernous. Uh, I don’t know how many of you have ever opened that up but it’s pretty 00:01:38.332-->00:01:42.869 enormous. There’s a, an awful lot of stuff in there and we’re going to try and highlight some 00:01:42.869-->00:01:48.842 of the things that we think are most important. And the things that we use all the time. Um, 00:01:48.842-->00:01:53.847 but we’re glossing over a lot. There’s much in man bash that I’ve never even read. So, take 00:01:58.485-->00:02:03.423 what were’ saying with a grain of salt in that respect. But there are resources to help. Man 00:02:07.260-->00:02:12.366 bash isn’t the only place where you can learn about using bash. Um one of my favorites is 00:02:12.366-->00:02:17.371 explain shell dot com. And I wanna see a show of hands. Does anybody know what that 00:02:20.173-->00:02:25.178 incantation is? That little bit of art kane stuff. Okay so a few of you know what it is a few of 00:02:27.381-->00:02:32.386 you don’t. This is a fort bomb. Um it’s making a function called colon that calls itself 00:02:34.988-->00:02:41.628 repeatedly in the background piping to itself and then runs it. So it, this will just spin 00:02:41.628-->00:02:46.500 out of control making as many processes as your system will allow. Um, if you don’t have a U 00:02:46.500-->00:02:51.505 limit set, that means all of them. But explain shell shows you what’s happening in this 00:02:54.141-->00:02:59.413 command line. Um so you can see here it says this is making a shell function. This is a 00:02:59.413-->00:03:04.017 compound command, this is a pipe. This is a ampersand which puts the whole thing in the 00:03:04.017-->00:03:10.490 background. So explain shell is a great way to take a command line that you either don’t 00:03:10.490-->00:03:15.495 understand or want to more fully understand, plop it into explain shell and it’ll give you a, a 00:03:18.365-->00:03:23.737 breakdown of how the whole thing works. >>And before you try it actually just parses it, it 00:03:23.737-->00:03:28.742 doesn’t actually run the code. [laughter] So no RC there. >>Hopefully, we tried. 00:03:34.247-->00:03:39.319 [laughter] So in the abstract I wrote this little piece of stuff here that sometimes you just 00:03:39.319-->00:03:44.324 need to pull out the third column of the csv file and this is less true as of y- y- a 00:03:46.393-->00:03:51.398 couple of days ago when blood hound switched to JSON. [laughter] But a lot of what we 00:03:56.470-->00:04:03.076 do as hackers is parsing stuff output from one tool to get, uh into the right format so that 00:04:03.076-->00:04:08.081 other tools can use it. So this sort of manipulation is very common in dealing with all the 00:04:10.217-->00:04:15.222 various kinds of tools that we use. So it gets weird sometimes with like randomly cased stuff 00:04:18.558-->00:04:25.365 and for some reason the some of the are quoted, some of them are not so dealing with the weird 00:04:25.365-->00:04:30.370 output on a command line is sometimes tricky, um so we’re going to just step through some 00:04:32.706-->00:04:37.711 of th- our solutions to do exactly what this abstract thing said. Um, so one thing that a 00:04:41.748-->00:04:46.753 lot of people do and I’m, I’m a big proponent of is the up edit enter repeat loop. So you start 00:04:51.591-->00:04:55.061 on the command line, you type out, oh I’m interested what does this file contain? So I’ll head 00:04:55.061-->00:05:00.133 this file or I’ll cap this file then I’ll pipe that to grep to pull out just the stuff I’m 00:05:00.133-->00:05:05.138 interested in. Then I’ll pipe that to cut to get just the f- the uh substring that I’m 00:05:08.041-->00:05:13.046 interested in. Um, all, all of those steps can be done with 1 awk command line. Because awk is 00:05:15.582-->00:05:19.920 very powerful. Awk is a full programming language and we’ll talk more about awk in, in a 00:05:19.920-->00:05:24.925 minute. Um, but there are a bunch of people who are t- who will tell you that this is 00:05:27.460-->00:05:33.099 specifically a useless use of cat. Those people are jerks, don’t listen to them. [laughter] 00:05:33.099-->00:05:38.104 >>It’s normal and okay. >>Hack however you want to hack. >>So there are a few different 00:05:43.176-->00:05:49.482 conventions that we’ll have here. Uh usually stuff in man section 1 will be your normal 00:05:49.482-->00:05:55.589 system utilities and tools. So most of the stuff we’ll talk about will be in there. Um, if 00:05:55.589-->00:06:00.427 you ever want to know man, man and it will show you all of the sections for uh the various man 00:06:00.427-->00:06:06.166 pages. Uh there’s some overlap obviously with syscalls which is section 2. Uh C functions 00:06:06.166-->00:06:11.171 section 3 and config files and other stuff. Uh section 5. >>And man dash K will also help you 00:06:15.709-->00:06:20.714 search through which section you’re actually interested in. >>One does not simply. This is a 00:06:22.749-->00:06:27.754 Unix system. I know this. Meme the memes. >>So these are the basic things that we’re gonna 00:06:34.561-->00:06:37.931 cover today. Uh beginning with pipes and redirection. We’ll step through each of these 00:06:37.931-->00:06:42.936 individually. We’ll look at them on a command line and, and see um typing how they work. Um 00:06:45.071-->00:06:48.441 pipes and redirection are probably one of the most fundamental things that would 00:06:48.441-->00:06:53.446 use all the time. Um variables and substitution are useful in uh a lot of cases um many times 00:06:55.949-->00:07:00.287 you can get away with not using variables at all. Um, we’ll talk about a bunch of the standard 00:07:00.287-->00:07:05.759 tools. Um, the various kinds of expansions you can do and then we’ll go through some examples 00:07:05.759-->00:07:10.764 at the end. Because you can’t, you can’t just say everything is a file. Please. It- it’s a file. 00:07:16.970-->00:07:21.975 [laughter] It’s really it’s a file. >>Alright to the terminal. Here we go. There’s my, there 00:07:28.648-->00:07:33.653 okay. So, everyone’s familiar with echo. That puts stuff on standard outs. Cat takes stuff 00:07:42.162-->00:07:47.167 on standard in so we can echo wut, pipe to cat. Most of these spaces are superfluous, so you 00:07:52.172-->00:07:58.812 can say echo wut, pipe cat. That’s fine. This again is a useless use of cat. [laughter] 00:07:58.812-->00:08:03.750 We can, hah yes, hack how you want. Um, the angle bracket operator redirects to a file so 00:08:13.860-->00:08:18.865 you can say, echo wut redirect to a file called wut then we can cat wut. That’s actually not a 00:08:22.001-->00:08:27.006 useless use of cat. [laughter] Um, but interestingly that doesn’t have to be at the end of 00:08:31.111-->00:08:34.381 a line. You see it most of the time it’s going to be at the end of the line but it doesn’t have 00:08:34.381-->00:08:39.386 to be. You can say, wut echo wut, that totally works. You can do the same thing with input as 00:08:43.289-->00:08:48.294 well. So you can say, wut cat. Also totally works. I find that useful when I’m doing something 00:08:52.132-->00:08:57.137 where I want to edit the end of the line a lot. >>Like grep. Exactly like grep. >>So um, 00:08:59.172-->00:09:04.110 you’re taking a bunch of stuff in and you wanna read the same file a lot of times, hit up 00:09:06.513-->00:09:11.651 enter, up, edit some stuff, enter. Having a file at the beginning of the line means you 00:09:11.651-->00:09:16.656 don’t have to use your arrow keys to go through and, and deal with all of that. Um, there’s 00:09:19.325-->00:09:24.330 also this magical command called exec. Which doesn’t actually do anything except for the file 00:09:27.400-->00:09:32.505 descriptor redirections, um if you don’t give it a command. So, you can say like exec. >>Yeah 00:09:32.505-->00:09:36.576 don’t actually run that unless in a subshell here. Because we’ll lose it. >>Right, it’ll l- 00:09:36.576-->00:09:39.913 yeah, I’m not, I’m not going to run it. >>The redirections fine. >>It’s okay if you do something 00:09:39.913-->00:09:45.385 like this. We’ll talk about dev tcp in a minute. Um, but what, what exec does here is it’s 00:09:45.385-->00:09:50.490 making a new file descriptor called number 3, it’s opening it for input and output and it 00:09:50.490-->00:09:55.195 opens the file. We could do, it doesn’t have to be dev tcp, this could be wut right here and how 00:09:55.195-->00:10:00.200 I can say, cat and 3. Er, excuse me. Cat. >>Yeah so normally the exec will replace a process 00:10:05.205-->00:10:10.210 image but you can use it to setup arbitrary redirections like here. >>So that’s fun. Um 00:10:13.680-->00:10:18.685 it gets especially fun later on when we do a t- d-, dev tcp. But you’ll see that later. Right 00:10:23.356-->00:10:28.361 there, there’s an example of it but we’ll talk more about it later. Oh, I did the same thing 00:10:30.597-->00:10:35.602 I told you not to do. There we go. Mmm, I’m sorry it broke open. >>Yeah um, so built-ins 00:10:38.972-->00:10:45.445 are built into the shell. They’re not actually commands in your path like ya know um, cat, 00:10:45.445-->00:10:52.152 set, grep, what not. Um if you can use them, it’s a little more preferred, it, cause it’s not 00:10:52.152-->00:10:57.190 actually shelling out to uh, a different process it’s all built within the shell and you can do 00:10:57.190-->00:11:02.128 quite a lot with that. Looping, uh echoing, checking conditions. >>So grep it the standard thing 00:11:09.168-->00:11:13.940 for searching stuff. If you’re coming from the windows world this is sort of similar to 00:11:13.940-->00:11:18.945 f=Findster. Um if I mean, Findster is kind of, hmm. [laughter] I’m, I’m trying to 00:11:23.216-->00:11:28.221 come up with a nice way to say this. [laughter] no it’s just, it’s just not good. That’s, um 00:11:32.625-->00:11:37.864 so if you’re not familiar with regular expressions like these 3 little thing here are probably 00:11:37.864-->00:11:44.337 gonna get a major portion of what you actually wanna do with regular expressions. So the dot 00:11:44.337-->00:11:50.944 start means dot match any character, star means any number of times zero or more. Um and 00:11:50.944-->00:11:55.949 that, f- is a whole lot of what you want to do with grep most of the time. Um, the carat anchors 00:11:58.384-->00:12:04.457 at the beginning of a line so make sure the match um, is at the beginning or dollar match at 00:12:04.457-->00:12:09.462 the end. Um and those 3 things are most of what you need for an awful lot of, of most people’s 00:12:11.898-->00:12:16.836 grep usage. And if you’re not familiar with regular expressions this is a great way 00:12:16.836-->00:12:21.841 to get started. Just use it a little bit, and read more when you need more. Um, a wise man 00:12:25.311-->00:12:30.316 once told me that the best way to get good at programming is to read a little, or read a lot, 00:12:32.885-->00:12:37.890 write a little. Repeat. And it’s been my mantra for years that if you read a lot and you write a 00:12:41.728-->00:12:46.733 little and then you start over and do it again, you will improve. >>It’s all on the 00:12:48.868-->00:12:53.873 practice >>And sed is an editor. It’s, it’s a string editor, that’s what it, that’s what the 00:12:58.778-->00:13:03.716 name means. Um, so it does a lot more than just uh substituting. This is the, this is the sort of 00:13:08.187-->00:13:12.992 thing that everybody uses sed for. If you’ve seen someone use sed, this is probably what they 00:13:12.992-->00:13:17.997 did with it. The S command um, does a substitution. So in this case we’re taking a file called 00:13:20.266-->00:13:26.239 foo we’re changing all of the instances of foo inside that command or inside that file and 00:13:26.239-->00:13:31.244 we’re changing them to bar. In this particular command line we’re also deleting the first 00:13:33.846-->00:13:38.851 line. The reason you might want to do that is cutting off a header. So that’s the, the m- 00:13:42.155-->00:13:47.160 primary usage that I get out of sed is cutting off headers with 1 D or N D, any number D will 00:13:50.963-->00:13:55.968 delete that many lines, um and a substitution like that. Sed does a lot more than this but this is 00:13:58.738-->00:14:04.677 most of what you need to get started. Um, you can also separate stuff inside quoted 00:14:04.677-->00:14:08.848 string if you pass the whole thing as an argument. So I guess I should talk about quotes huh? 00:14:08.848-->00:14:13.853 >>Yeah. >>On, on a bash command line, quotes make things 1 argument. So at the lower level 00:14:19.525-->00:14:26.466 there’s a, a com- or there’s a function called exec V or exec V E that takes the binary you 00:14:26.466-->00:14:31.471 wanna run, in this case it’s sed and then each of the arguments that get sent to it. In th-this 00:14:33.706-->00:14:40.580 first example here this is 1 argument, and this is 1 argument and this is 1 argument and the 00:14:40.580-->00:14:44.350 spaces are what separate them. The shell decides where that happens. >>It’s called wart 00:14:44.350-->00:14:49.355 splitting. >>With quoting, this is 1 argument. The spaces don’t um separate things because the 00:14:55.528-->00:15:01.400 s- she uh quotes make the spaces not special. >>And it also has the effect of escaping any 00:15:01.400-->00:15:07.106 special characters like semicolon which would normal separate commands out. >>Right. 00:15:07.106-->00:15:12.111 So we’ve got our w-what file here. We can pipe that to sed. We changed wut into lol. 00:15:16.215-->00:15:21.220 >>Useless, use of cat. >>And that’s a useless use of cat. [laughter] Because we could get 00:15:25.992-->00:15:30.997 the exactly same way like this. >>Or without redirection at the end. Yeah so it’s picky. >>Okay. 00:15:38.104-->00:15:43.109 So that was fun. Sed also has this dash I thing to change it in place. It’s not exactly in 00:15:47.914-->00:15:52.919 place. It’s, it’s kind of in place. >>Yeah that arguments different between operating 00:15:54.954-->00:16:00.626 systems so be careful how you use it. Some will ask you to specify more arguments. This 00:16:00.626-->00:16:05.631 example here actually deletes from a log file, uh that IP address. Any lines containing 00:16:09.035-->00:16:14.040 that IP address it just deletes it in place. Might be useful. [laughter] >>And ED is the 00:16:19.212-->00:16:24.217 standard editor. Wait I have to go. Ed is the standard editor. [laughter] if you read man ed it 00:16:30.089-->00:16:34.794 says that it’s the standard editor. >>And that’s a running joke >>And >>Yeah >>It’s the 00:16:34.794-->00:16:39.799 standard editor >>It’s the standard editor. Okay. Do you want to talk about cut? >>Yeah, 00:16:43.302-->00:16:48.307 cut so um, w- we’ll cover awk in a bit here. But cut can do a field separation. Um you can 00:16:52.111-->00:16:57.116 pull out fields from a string. Or a file. Uh strings within a file. So you can specify a 00:16:59.719-->00:17:04.924 separator dash D uh it’s a delimiter, uh in this case it’s comma. If you’re doing CSV or 00:17:04.924-->00:17:10.463 anything like that, that’s quite common. And you can select on certain fields. Like 1 to 3. So 00:17:10.463-->00:17:16.168 that’s fields 1 to 3 and then also 5. Now the prob is that the delimiter is usually a single 00:17:16.168-->00:17:21.173 character so, uh if you have arbitrary white space it’s not gonna work so well. Which is why 00:17:27.380-->00:17:32.385 awk might be the superior solution. Uh, yay. One thing to note is that cut can’t reorder 00:17:34.887-->00:17:39.892 anything. Yeah it’s basically left to right. >>Yeah. >>And sort, sorts stuff. It’s what it 00:17:43.863-->00:17:48.868 does. Says it right there on the tin. Uh but it has this awesome flag dash U. Which unique stuff. 00:17:53.005-->00:17:57.843 So it will only give you lines that are unique among the file so you don’t get multiples, so 00:17:57.843-->00:18:02.782 if something contains uh 2 copies of the same address then you will only see 1 of them. And 00:18:08.587-->00:18:13.593 cat can at, concatenate stuff. Hack how you want. Um, unique is very often um, taking the input 00:18:24.070-->00:18:29.875 from sort. So it’s really common for people to say, sort, pipe, unique. Most of the time you 00:18:29.875-->00:18:36.782 don’t need to do that because sort has dash U. So unique is pretty useless unless you wanna 00:18:36.782-->00:18:41.787 count stuff. Um and unique dash C uh, lets you count the number of multiples. So sort will give 00:18:44.223-->00:18:50.830 you everything in a sorted order uh dash U will remove the multiples or pipe to unique dash 00:18:50.830-->00:18:55.835 C and then you get a count of how many exist in the file. So. Oh, hang on. Awww. >>Yeah. >>Now 00:19:03.142-->00:19:08.147 we don’t have the header. >>You can use head for that too. >>Yep. And then, these are all 00:19:18.891-->00:19:23.896 uniques, so doesn’t matter. >>It would work in theory. >>Eh, it’s, t- I tested this when I 00:19:26.065-->00:19:33.039 was sober. [inaudible audience] That was supposed to have a, that right there. Awk is a 00:19:33.039-->00:19:38.044 language. It’s s-so much more than just printing a single file or a, or a single field from a 00:19:41.914-->00:19:47.853 file. Uh but most of the time that’s really all you need. Um, it’s a replacement for cut in a 00:19:47.853-->00:19:52.858 lot of ways. And I personally never use cut because I usually have to reach for awk afterwards 00:19:55.961-->00:20:01.867 anyway and then I just have to rewrite my cut line so I might as well just start with awk. Um. 00:20:01.867-->00:20:07.373 >>And if it looks like pearl that’s cause awk preceded pearl and >>Yeah. >>Basically defined 00:20:07.373-->00:20:12.378 all of this text processing stuff >>Yeah and there’s, there’s a lot more to awk uh awk 00:20:14.447-->00:20:19.452 in GNU awk in particular has a full TCP stack so you can write uh, a full reverse shell payload 00:20:28.194-->00:20:33.199 in just awk. Which. >>Terrifying. >>Yeah [laughter] Um, hahaha variables, don’t, 00:20:38.337-->00:20:43.342 don’t mind me editing as we go. Uh, variables, variables are good. So what’s happening here 00:20:49.949-->00:20:54.954 is we say foo equals echo wut. That’s processed s- substitution happening there. We’re making a 00:20:59.258-->00:21:03.329 subshell. >>Command substitution >>I’m sorry, command substitution, not the same 00:21:03.329-->00:21:08.334 thing. >>And we’ll cover that later. >>Uh so now we have a variable called foo. Because 00:21:15.774-->00:21:22.748 echo wut echoed the wut and then the whole thing here is the value of foo. So this is exactly 00:21:22.748-->00:21:27.753 equal to foo equals wut. If we said foo equals echo wut then we could just say dollar foo and it 00:21:33.792-->00:21:40.533 runs the command, which is important. If you accidentally put something blank. So say you 00:21:40.533-->00:21:45.538 have 2 variables, we have another one called bar and it’s empty. Say bar, foo, it runs the 00:21:48.841-->00:21:54.346 second one. Be careful. [laughter] Be very careful. >>Yeah this is how people have 00:21:54.346-->00:22:00.452 been bitten writing shell scripts. Especially stuff that is privileged. >>Um, there was 00:22:00.452-->00:22:05.457 an especially, f-fun bug. Um with, was it Steen a few years ago? I think it was Steen. Where 00:22:11.030-->00:22:16.802 there was a script that did something like >>Oh yeah it was so, I think it was a Linux Steam 00:22:16.802-->00:22:21.807 client and they forgot to quote something and >>It was something like that >>Yeah don’t actually 00:22:24.677-->00:22:29.682 run that >>But foo was empty [laughter] >>Yeah you should be protected against this on most 00:22:33.052-->00:22:38.057 modern systems but don’t try your luck. [laughter] [inaudible audience] [laughter] >>Um, what 00:22:44.797-->00:22:49.802 I actually don’t use enough is a variable substitution where you can take a piece of a variable 00:22:53.105-->00:22:58.110 and run various kinds of transforms on it. A lot of what I use sed for can be done if you 00:23:00.479-->00:23:05.484 already have the text in a variable. Just with variable substitution. So this here takes 00:23:08.854-->00:23:13.859 foo and sets it to my current path with colons replaced with spaces. So echo path, has all of 00:23:19.932-->00:23:24.937 that stuff, and echo foo has the same stuff but all the spac- the colons are replaced with spaces. 00:23:27.139-->00:23:32.478 >>You solve what you want it to iterate as a list perhaps, with >>Exactly >>Loops later >>So 00:23:32.478-->00:23:37.483 we’ll talk about loops in a second. Um there’s a special variable in bash called IFS, w- 00:23:39.685-->00:23:44.690 we’ll talk more about it in a moment. But IFS is the input field separator. It’s mostly 00:23:51.530-->00:23:56.535 spaces. >>There’s usually 3, I think it’s uh tab, space, new line, something like that. >>New 00:24:01.974-->00:24:06.979 line, yep. Um. >>Might be missing one. >>It’s fun with uh, a post exploitation to be able 00:24:09.748-->00:24:16.255 to run commands. Uh for situations where you can’t use spaces for whatever reason. So 00:24:16.255-->00:24:21.727 say you have a command injection vulnerability in a web page but they’re stripping spaces out. 00:24:21.727-->00:24:26.732 You replace it with the RFIS, then still works. So it’s fine. I use history expansion all the 00:24:43.282-->00:24:48.320 time, you probably saw some earlier when I was just noodling around on the, on the shell. Um 00:24:48.320-->00:24:53.325 but bang, bang is your whole previous line. And you’ll see that it gets echoed here as 00:24:57.529-->00:25:02.468 well. So this is my whole line. This echo comes from here and this is the result. We can, th- 00:25:08.040-->00:25:13.045 there’s other special things you can get from, uh history here. So let’s start with. No those 00:25:20.319-->00:25:25.324 need to be spaces pardon me. Bang, colon, 2 is the second argument. You can do that with 00:25:32.331-->00:25:37.336 any and uh I find this super useful for a lot of things, um the one I use probably almost as 00:25:43.041-->00:25:48.046 often is bang star. Which is all the arguments. Oops. Um, bang, dollar is the last argument. 00:25:57.890-->00:26:02.594 Bang carat is the first argument. >>Anything is that, instead of bang dollar you could 00:26:02.594-->00:26:09.034 actually there’s a bunch of read line keyboard shortcuts you can use. Like if you hit alt period 00:26:09.034-->00:26:14.506 for instance right there, it will drop that last argument right in there. And it can cycle 00:26:14.506-->00:26:18.544 it too. So if you really need to tab something in quickly and, and drop the last argument 00:26:18.544-->00:26:23.549 that’s what you’d use. >>Banh minus 2 is the second command in your history. >>Previously >>Um 00:26:36.328-->00:26:41.333 you can also do a range. And history expansion actually expands so when it goes into y- 00:26:52.578-->00:26:57.583 into your history you get the result rather than the bang substitution. Etcetera. All of 00:27:10.996-->00:27:16.001 this is controlled by your tilt slash dot input RC. Which is your read line um, configuration 00:27:18.770-->00:27:23.775 file. This is my input RC. So a lot of the things you’re seeing me do on the command line here 00:27:27.646-->00:27:32.651 uh are result of this input RC configuration. Uh magic space means that when I do a 00:27:37.389-->00:27:44.229 substitution and hit the spacebar it actually replaces it in my current command line. 00:27:44.229-->00:27:49.234 Super useful for speeding up all kinds of substitutions. Um, it makes it so that your history is 00:27:52.137-->00:27:57.142 no longer a thing that you, you operate on as itself. But as part of your working command 00:28:01.246-->00:28:06.251 line in your up enter, uh, up edit enter loop. History search backwards. A lot of people use 00:28:13.225-->00:28:19.731 the control R h- uh key for that and it’s really useful when you do it that way. But I’ve also 00:28:19.731-->00:28:24.736 set it to the up arrow so if I say, I don’t have an awk. I have a-, what do I have I have a set. 00:28:31.276-->00:28:36.281 So if I type sed and then the up arrow it gets the last command in my history that began with 00:28:38.450-->00:28:43.455 sed. So it does a reverse um search just with the up arrow. This is s-such a huge time 00:28:45.490-->00:28:50.696 saver. It’ll change your life. Make this change and thank me later. >>And definitely check 00:28:50.696-->00:28:55.701 out more of read lines library a lot, that handles this line editing stuff. Um you can do, 00:28:55.701-->00:29:01.039 they’re tons of key buys, like control P which is right close to your home row is actually up 00:29:01.039-->00:29:06.044 arrow. >>Oh my God. So many things, there we go. Braces. >>Oh so brace expansion. Um, we 00:29:18.657-->00:29:24.696 haven’t talked about yet. Uh but it, it is related to the IFS stuff. Uh, we’ll, we’ll cover 00:29:24.696-->00:29:29.701 that later. But it allows you to take a list of, of, oh strings or what not and expand them out. 00:29:33.238-->00:29:40.078 Normally people will use seek, SEQ, uh historically, it’s um, i-it’ll print out ranges. You 00:29:40.078-->00:29:45.083 can do seek 1 to 10 and it’ll print out 1 to 10. But brace expansion is built right into 00:29:47.786-->00:29:53.191 the shell. It’s basically like a, a built in, in the shell grammar. So you can do list like 00:29:53.191-->00:29:58.196 this very, very quickly. It takes steps too so you can jump 2. Uh 0 padding, a bunch of 00:30:00.866-->00:30:05.671 different things. Really useful in scripts or if you want it to, to grab ya know a bunch of 00:30:05.671-->00:30:10.676 numbered files for instance. >>Tilde expansion is kind of similar to uh, s- variable 00:30:18.417-->00:30:24.956 substitution in that it happens um when the command is read not when it is run. Uh so that 00:30:24.956-->00:30:29.194 changes the meaning of somethings, there’s some confusion about this when I 00:30:29.194-->00:30:34.199 tweeted about it a few days ago. >>Haha >>Um tilde plus is your current working directory. Tilde 00:30:41.239-->00:30:46.244 minus is your old working directory. >>And none of those are absolute paths. [applause] 00:30:51.983-->00:30:56.988 >>Um. [audience inaudible] This is d- tilde plus is different from dot. Because it happens on 00:31:01.626-->00:31:06.698 the command line before the command gets run. This is really important for scripts that 00:31:06.698-->00:31:12.871 change their directory before they look at their arguments. So if you um, in particular 00:31:12.871-->00:31:17.976 eyewitness on Cali, I’m looking at you. Not that I’m gonna name any names but eyewitness on Cali 00:31:17.976-->00:31:22.981 I’m looking at you. It changes it’s working directory before it looks at its arguments so if you 00:31:25.016-->00:31:30.021 say ya know, eyewitness foo dot or dash X foo dot XML, it, it’ll be looking in it’s installation 00:31:35.127-->00:31:42.033 directory for foo dot XML it won’t be able to find it and it will complain. Um but if you put 00:31:42.033-->00:31:47.038 tilde plus then it works. It doesn’t work if you say dot slash because the dot is just a 00:31:49.674-->00:31:54.679 literal dot. >>You can kind of think of it as the difference between dot um, um having 00:31:56.715-->00:32:03.522 special meaning to the program or side to the, to the program um, it’s a relative path 00:32:03.522-->00:32:08.527 actually and, and whereas the tilde expand tilde uh, plus here expands that out and actually 00:32:11.329-->00:32:16.334 interprets it. >>So loops are a really useful programming concept but they end up being 00:32:20.071-->00:32:26.511 pretty useful for dealing with all kinds of weird data too. Um so I mentioned earlier that you 00:32:26.511-->00:32:32.784 can read in a file at the beginning. This is exactly where I use it all the time. Um while 00:32:32.784-->00:32:37.789 read is fantastic for dealing with um, lines of a file 1 at a time this dollar apply here is 00:32:43.195-->00:32:50.168 the default variable that wild read will pull in, well read in particular pulls in. Um, you can 00:32:50.168-->00:32:55.173 also give it a name but note that this is, it’s while read a name, it’s not a dollar name. 00:33:00.745-->00:33:05.750 Cause that actually expands it first and you get, it’s not what you want, don’t do that. Um I 00:33:08.720-->00:33:15.260 use 4 loops a lot specifically for dealing with lots of files that I wanna change. So this 00:33:15.260-->00:33:20.398 line inside here can actually all be done with brace expansion which I do all the time too. So 00:33:20.398-->00:33:25.403 you say move foo dot 1 dot txt to I want it to be a CSV so I’ll say CSV. What’s happening here 00:33:30.542-->00:33:35.547 is it’s replacing this with this on another argument. So if I put it here you can see that it gets 00:33:38.416-->00:33:43.421 both arguments there. If I do it with move, now I LS foo star and foo dot 1 is now CSV. But that 00:33:52.497-->00:33:57.502 doesn’t work when you want to do lots of them. So command the, the foo or the 4 loop lets you 00:33:59.704-->00:34:04.643 do all of that in a loop. >>We already did that. >>Yeah shut up. [laughter] >>There’s also a 00:34:08.713-->00:34:13.551 rename command a couple different ones that can do this but remembering the syntax is a 00:34:13.551-->00:34:19.925 little bit harder. >>Yeah and r-rename is super unportable. It sometimes it’s a pearl version, 00:34:19.925-->00:34:24.629 sometimes it’s the GNU version. And they take completely different arguments and have 00:34:24.629-->00:34:29.634 completely different semantics. So just use a loop it’s easier. >>Because Unix >>Because Unix. 00:34:33.538-->00:34:40.145 Process command substitutions. >>Yeah this is a fun one. Now I get the keyboard. So we, we talk 00:34:40.145-->00:34:46.685 a little bit about command substitution and process substitution but mostly in 00:34:46.685-->00:34:51.690 passing with the previous slides. Or text file >>Slide-ish >>Cl-clear the screen here. So 00:34:54.292-->00:34:59.297 command substation is a way of substituting a command as text. And process substation is a way 00:35:02.200-->00:35:07.205 of substituting a command as a file. So text you could do echo I’m a quote all my variables in 00:35:10.275-->00:35:15.280 command substitutions. Uh echo, echo wut, is that what I write? >>Yep >>So it basically echoes 00:35:18.216-->00:35:23.221 wut, substitutes as text and the passes it to echo so it's just echoing wut. You could also do 00:35:25.323-->00:35:30.328 cat >>Useless use of echo >>Sure [laughter] Well. We didn’t cover that 1. But uh process 00:35:36.201-->00:35:41.206 substitution has this syntax here and now this will try to echo wut as a file and it will 00:35:45.443-->00:35:52.250 try and cat that out, set it out. Um and, and it takes actually a couple different 00:35:52.250-->00:35:57.255 directions. Command substation about is quite, uh quite simplistic. Um any time you want 00:35:57.255-->00:36:03.528 to replace some command, some complex command and shove into another command as a text field 00:36:03.528-->00:36:08.533 that’s what you want. But ProxSub is a little bit more nuanced. Um we can see here. So 00:36:18.176-->00:36:23.181 it h- it’s actually a symbolic length to a pipe. In a pipe. Have to remember control D, I’m 00:36:25.850-->00:36:31.823 using your setup. Pipe is write on 1 end, read on another. But you can notice the permissions 00:36:31.823-->00:36:37.395 are different here so the left pointy bracket for process substitution. Substitution of 00:36:37.395-->00:36:42.400 file but it’s for reading only. And you can see that right here the R. And the other 1 which is 00:36:46.071-->00:36:51.076 a write, that’s permission there. So you can only do it 1, 1 direction. Um, so confuse 00:36:53.111-->00:36:58.116 those at your peril. Um, usually uh commands that take in a, a import file. You could take 00:37:02.487-->00:37:08.960 another command and substitute in as a ProxSub and you’ve basically read a, a command as a 00:37:08.960-->00:37:13.965 file. Um for others you know, a command that takes a write. You could write to do some stuff and 00:37:18.636-->00:37:23.641 you're basically writing to another process uh, uh through a pipe. So pretty useful for 00:37:25.810-->00:37:31.883 complex command lines. >>I use dif. >>I use this for dif a lot >>Oh yeah this is a good one. 00:37:31.883-->00:37:36.888 >>So you can say like, dif foo sort foo. So then like that. So it’s making a sorted version of 00:37:41.025-->00:37:46.030 foo and then telling me what’s different about the file on disk. You can alias stuff. So 00:37:50.902-->00:37:56.774 things that you type a lot you don’t have to type quite so much. Um, this is 1 of my 00:37:56.774-->00:38:02.680 favorites. L dot shows you only things that begin with dot. So it shows you hidden files. It’s 00:38:02.680-->00:38:08.186 kind of handy. Um you can alias anything including substitutions. And they will 00:38:08.186-->00:38:13.625 happen at the time that you run it. Which is handy sometimes. >>For more complex things you 00:38:13.625-->00:38:18.163 can write a shell function but we don’t’ really over that here. It’s, it’s in the man page. 00:38:18.163-->00:38:23.168 >>Okay, so now we’re gonna mostly switch back to, why are these still in here? Skip, skip, 00:38:26.905-->00:38:33.778 skip there we go, aliases are good. Exampled. So I used to use this big long command line with 00:38:33.778-->00:38:38.783 uh, sort. What is that K for? Column something, I, I dunno. >>Oh it’s for, uh, uh f- f- th- 00:38:41.853-->00:38:48.326 the f- the key depth. >>Key, that’s right. >>Key depth >>Key >>The key >>Um yeah but there’s 00:38:48.326-->00:38:54.232 actually a GNU version of sort that just takes dash V and it does the right thing. >>Now in 00:38:54.232-->00:39:00.071 BSD and OSX. >>Yay. Um sort dash, dash debug is actually really cool for viewing how it 00:39:00.071-->00:39:06.978 decides the stuff is equal or not. So y- y- gets like a little underscore below the, the pieces 00:39:06.978-->00:39:13.017 it sees as uh, uh comparable. >>And that’s the GNU version uses underscore the others have 00:39:13.017-->00:39:18.022 some arcane outputs so good luck. >>Um, this I really like this, this uh small little awk 1 00:39:22.327-->00:39:27.465 liner. I actually have this as an alias now cause it’s neat. Um but it lets you print only 00:39:27.465-->00:39:33.004 unique lines without having to sort. And sometimes that’s important. Um, b-basically 00:39:33.004-->00:39:38.009 what’s happening here is awk has a, an array called seen with keys being the whole line. And 00:39:41.079-->00:39:46.084 if its’ been seen before it will be 0 because it hasn’t been set uh and after that increment line 00:39:49.420-->00:39:54.425 it’ll be uh a non 0 value and it won’t get printed. It’s kinda neat. >>We’re not gonna run this 00:39:56.861-->00:40:01.866 stuff, cause uh networking is hard. But uh there’s some remote stuff that you can do with SSN. 00:40:04.302-->00:40:09.307 I’m sure most of you already know this. Maybe, maybe not. Um dash capital J is an option for 00:40:12.043-->00:40:18.983 proxy jump I believe. Which was at a semi recently in SSH. So if you ever use like proxy command 00:40:18.983-->00:40:24.756 or whatever and fed it to, eh, fed netcat to it. You can basically obsolete that and just 00:40:24.756-->00:40:30.628 jump through another SSH host as a proxy, using just dash capJ. And you can shove that in your 00:40:30.628-->00:40:35.633 config if you’d like. Uh different things. And most of you have used d- capital D. 00:40:35.633-->00:40:41.105 That’s a sox proxy, starts a sox 5 proxy locally. Um you can change the bind port and, and 00:40:41.105-->00:40:46.277 all that. Uh so you can feed other tools through it, through your SSH tunnel. Uh you’ve got 00:40:46.277-->00:40:52.083 local and reverse forwards of course. Local it binds locally here, w-what you’re doing is uh 00:40:52.083-->00:40:58.823 you’re basically forwarding 3, 3, 8, 9 which I think is already P from the open up port locally 00:40:58.823-->00:41:03.761 and it forwards it through user at host 1 to desktop 1, 3, 3, 8, 9, so you can connect using our 00:41:05.763-->00:41:11.903 desktop local host 3 3 8 9. It will go through that jump host and then to desktop 1. >>What’s 00:41:11.903-->00:41:16.975 fun is changing these. >>Oh yeah. >>So you can say SSHJ to a host and then on that host you 00:41:16.975-->00:41:21.980 say SSH dash J to another hose and then on that host you say SSH-L, you listen locally, it 00:41:26.584-->00:41:31.022 tunnels through all of them and then it’s like you’re hacking in Hollywood, you got behind 7 00:41:31.022-->00:41:37.028 proxies. >>Speaking of. >>Super Psyched >>Speaking of that shells, reverse tunnels uh you 00:41:37.028-->00:41:42.400 can catch a shell remotely and have it forwarded through your tunnel down locally which is 00:41:42.400-->00:41:48.873 real good for um hiding where things come from I guess. Or bypassing uh restrictions 00:41:48.873-->00:41:55.046 between uh networks. And these 2 last examples are just, kind of show, you try to illustrate the 00:41:55.046-->00:42:01.386 redirections you can do. You normally SCP does this under the covers but you could use T or 00:42:01.386-->00:42:06.357 cat and redirect input output and read and write files remotely. And that is not a 00:42:06.357-->00:42:11.362 useless use of cat. >>Bash can do a lot of really ridiculous things in, with uh TCP. Um so we 00:42:16.567-->00:42:23.374 mentioned this fake file dev TCP. Um it’s, it’s not actually a file. It doesn’t really exist. 00:42:23.374-->00:42:28.012 Uh if you look on the file system you, you won’t find it. Um it’s actually a special case 00:42:28.012-->00:42:34.452 inside bash in, in the C code for bash uh there’s a special case for a file called dev TCP 00:42:34.452-->00:42:40.658 and it lets you just make arbitrary TCP connections. Uh so yeah explain your shitty port 00:42:40.658-->00:42:45.663 scanner. >>Yeah, thanks. Uh this shitty port scanner here, it uses your 4 loop, your brace 00:42:48.166-->00:42:54.639 expansion, couple redirections, couple command separators and y- it’s all on a loop. Uh I’ll, I 00:42:54.639-->00:42:59.644 guess I could just run it. Did I do that right? Control B 0, yay. Um >>I, I >>I’m trying to 00:43:03.314-->00:43:07.351 remember it now >>I don’t think we have enough time. >>Yeah alright I’m not gonna run it but 00:43:07.351-->00:43:13.591 you should run that command. [laughter] and it’s using all built ins. >>Yeah this is 00:43:13.591-->00:43:18.596 actually really fast. It’s ridiculously fast for how simple it is. Uh I use xsel dash B all 00:43:21.399-->00:43:27.939 the time for uh copying files to the primary copy buffer in. >>Clipboard selection >>Yeah 00:43:27.939-->00:43:33.511 it’s clipboard selection >>You have all the selections. >>Yeah, um this pipe end here is a 00:43:33.511-->00:43:39.684 special syntax in bash that lets you do both. Uh standard out and standard error piped to the same 00:43:39.684-->00:43:44.689 place also very useful. Now for the hacking. You dirty hackers. >>Yeah we’ll probably have to 00:43:48.326-->00:43:51.529 run through this quickly. >>Yes, um there’s a lot of things that snitch on you if you’re not 00:43:51.529-->00:43:55.933 careful. Um, top has history is a file that gets written with all of your commands that you 00:43:55.933-->00:44:01.873 run. Uh so there are a couple of ways that you can get around that, onset HISTFILE which is 00:44:01.873-->00:44:08.579 the variable that stores where that file gets written to. >>Or put it to devnull. >>Or you can 00:44:08.579-->00:44:13.584 set it to devnull. Or you can link the actual file to dev null but don’t ever do this because 00:44:16.454-->00:44:21.125 then there’s a link there and the actual file gets blown away and if someone opens up their 00:44:21.125-->00:44:25.363 shell and suddenly has no history forever. >>They’ll’ going to be sad very sad. 00:44:25.363-->00:44:28.032 >>They’ll be sad and then they’re gonna start looking for you, so don’t do that. 00:44:28.032-->00:44:33.037 [applause] Um, yeah. So these are other w-, you can read the slides online also. We are gonna 00:44:37.041-->00:44:41.879 skip over this. STFU >>Different ways to hide your history and, and >>Yeah different ways to 00:44:41.879-->00:44:46.884 STFU. Yeah commands sometimes snitch on you as well so SSH will use your current username 00:44:49.420-->00:44:54.492 to whatever you connect to uh unless you explicitly tell it otherwise. Um our desktop in 00:44:54.492-->00:44:59.497 xfreerdp do the same thing. FTP does but it doesn’t actually send it until you tell it to log 00:44:59.497-->00:45:04.435 in. Um there’s a dot net RC that will allow you to um, store an anonymous 1. Um, you can do the 00:45:11.976-->00:45:16.981 same thing with aliases to say, don’t do that. >>Oh, forgot about this 1. [laughter] Uh 00:45:21.385-->00:45:26.390 there’s differ ways to hide spaces. You can use brace expansion. >>Yay! Um if you 00:45:29.193-->00:45:35.266 don’t have LS, echo star works just as well. Find dot also works well. Uh if you don’t have 00:45:35.266-->00:45:41.005 cat, uh while read also works. >>Or had with a really long ya know number. >>There are a ton 00:45:41.005-->00:45:47.645 of ways to read binary text over, or binary over a text only link. Um and there are quite a 00:45:47.645-->00:45:52.950 few ways of writing binary over a text only link. Um but all of those you can do Metasploit for 00:45:52.950-->00:45:58.122 free. Uh. >>Host some stuff. >>You can host some stuff. >>Shellcode >>Convert shellcode 00:45:58.122-->00:46:01.258 to hex bytes, that’s fun. >>I wish I could show that but we’re out of time. >>Yeah we’re out of 00:46:01.258-->00:46:06.831 time. Uh read these things they’re cool. >>And come find us. >>And come find us, uh I 00:46:06.831-->00:46:11.836 don’t have my useless use of cat. I’ve gotta go find that. [laughter] [applause]