1 00:00:01,140 --> 00:00:08,130 In this lecture, we will create a function to download files, this function will be used for downloading 2 00:00:08,130 --> 00:00:10,100 additional modules to the system. 3 00:00:12,210 --> 00:00:19,170 To do so, we will create a class called operations, and inside the class it will create two functions 4 00:00:19,770 --> 00:00:22,110 commandparser and downloadfile. 5 00:00:22,800 --> 00:00:29,280 The commandparser function will take the command as argument and it will call the corresponding function according 6 00:00:29,280 --> 00:00:30,020 to the command. 7 00:00:30,450 --> 00:00:34,380 So we will be using the commonparser function in our further operations. 8 00:00:35,320 --> 00:00:41,680 If the command contains download keyword, then the parser function must call downloadfile function. 9 00:00:43,480 --> 00:00:49,150 In order to download files and sending HTTP requests, we will be using Web client class. 10 00:00:52,400 --> 00:00:59,390 So let's start coding, but if you are new to this concepts, I highly encourage you to learn more about 11 00:00:59,870 --> 00:01:02,960 HTTP and Web client class before continue. 12 00:01:07,240 --> 00:01:08,230 Let's start coding. 13 00:01:10,570 --> 00:01:19,840 We will create a new class to do so click the new file button, I will call it ops.cs and copy the 14 00:01:19,840 --> 00:01:21,100 contents of the program.cs 15 00:01:21,100 --> 00:01:26,470 into the new one and change the class name. 16 00:01:27,320 --> 00:01:29,770 I will use operations as my class name. 17 00:01:36,170 --> 00:01:38,750 And let's create a constructor. 18 00:01:46,440 --> 00:01:53,910 We will be using our operations class in further operations, so we may need the information inside 19 00:01:53,910 --> 00:02:00,810 the generalinfo class, so we will create an instance of generalinfo class inside the operations 20 00:02:00,810 --> 00:02:01,230 class. 21 00:02:02,910 --> 00:02:09,510 I will call it the instance or I will call it an ninstance. 22 00:02:11,010 --> 00:02:12,120 It will be our 23 00:02:14,680 --> 00:02:23,800 instance of the generalinfo class and our constructor will take an instance of the generalinfo class 24 00:02:24,010 --> 00:02:24,910 as an argument. 25 00:02:28,980 --> 00:02:33,720 And we will initialize the ninstance value inside the constructor. 26 00:02:40,920 --> 00:02:41,310 So. 27 00:02:42,810 --> 00:02:48,150 Let's start creating functions, our first function will be the commandparser. 28 00:02:52,640 --> 00:02:54,410 CommandParser. 29 00:02:59,280 --> 00:03:02,770 command parser function will take a command as argument. 30 00:03:03,540 --> 00:03:11,430 So let's create a string argument and our command will be something like this. 31 00:03:12,980 --> 00:03:15,710 Yeah, like download and a URL. 32 00:03:16,980 --> 00:03:22,440 So we need to parse our command into two sections. 33 00:03:23,130 --> 00:03:27,150 One section is the command and second section is the arguments. 34 00:03:27,720 --> 00:03:30,720 To do so, we will use split function. 35 00:03:35,320 --> 00:03:48,460 I will call it cmd, and our command will be lets create a new string and our command will be the 36 00:03:52,540 --> 00:03:58,570 first element when we split the string according to the space. 37 00:04:03,370 --> 00:04:09,160 When we split it according to space,it will be that the first element will be download 38 00:04:09,160 --> 00:04:10,900 the second element will be the argument. 39 00:04:12,160 --> 00:04:16,040 So let's create a new variable argument. 40 00:04:18,040 --> 00:04:26,500 It will be the second element from the split according to the space. 41 00:04:29,850 --> 00:04:36,360 And if command contains 42 00:04:39,540 --> 00:04:48,310 download keyword our commandparser function will call the download file function. 43 00:04:48,900 --> 00:04:50,640 Let's create that function also. 44 00:04:54,550 --> 00:04:54,850 DownloadFile 45 00:04:56,470 --> 00:05:01,300 It will take URL as argument so lets create it 46 00:05:11,490 --> 00:05:15,910 What's the problem here ? Oh i see scope problem. 47 00:05:17,220 --> 00:05:22,030 I need to define it outside the scope of the commandparser. 48 00:05:23,370 --> 00:05:26,340 So thats it and 49 00:05:28,820 --> 00:05:40,250 let's call the downloadfile function inside the commandparser function file with the argument, 50 00:05:40,250 --> 00:05:49,400 which is your URL and inside the downloadfile function we will be using Web client class download method 51 00:05:49,400 --> 00:05:50,060 of WebClient class. 52 00:05:50,150 --> 00:05:51,680 To do so 53 00:05:52,370 --> 00:05:54,740 let's create an instance of the WebClient class. 54 00:05:57,570 --> 00:06:01,410 Or we can define it as a variable of our class. 55 00:06:04,100 --> 00:06:06,110 But it doesn't matter, actually. 56 00:06:09,280 --> 00:06:17,650 Yeah, just just create it inside the downloadfile function, it's OK. To do so, we will be using 57 00:06:17,650 --> 00:06:20,670 system.net namespace WebClient class 58 00:06:20,740 --> 00:06:22,860 let's create an instance of it 59 00:06:23,620 --> 00:06:30,820 I will call it winstance, and it will be. 60 00:06:37,080 --> 00:06:45,990 And we will be using download file method of the Web client class to do so, simply find function. 61 00:06:46,930 --> 00:06:48,390 It takes two arguments. 62 00:06:48,840 --> 00:06:55,530 The first argument is the URL, which we already have, and the second argument will be the filename 63 00:06:56,190 --> 00:06:58,020 and the save path. 64 00:06:59,130 --> 00:07:05,820 To do so, we will be using current users temporary folder. In order to find it 65 00:07:06,970 --> 00:07:13,350 we will be using GetTemp method of the Path class. 66 00:07:14,640 --> 00:07:18,120 To use it we will be using system.IO namespace. 67 00:07:19,620 --> 00:07:36,450 **unnecassarry talk** 68 00:07:36,960 --> 00:07:48,180 A this function returns something like this, like C:\users, blah, blah, blah, blah. 69 00:07:48,450 --> 00:07:53,210 So we need a file name to append it. To do so 70 00:07:53,340 --> 00:08:03,390 let's get the file name from the URL, because our URL will be something like this 71 00:08:03,390 --> 00:08:06,280 google.com/index.html you need to get that part (index.html part). 72 00:08:06,300 --> 00:08:14,300 So if we split the string by according to the slash, we can get this value. 73 00:08:15,090 --> 00:08:15,720 Let's do it. 74 00:08:19,700 --> 00:08:34,540 File name will be URL.split we will be splitting according to Slash and we need to take the latest 75 00:08:34,540 --> 00:08:37,090 element because it could be something like this also 76 00:08:37,090 --> 00:08:43,770 directory/index.html, we only about this part (index.html) 77 00:08:44,230 --> 00:08:53,110 So we need to get the latest element when we split it according to / to do so, we will get the latest element 78 00:08:53,110 --> 00:08:55,000 by using the split function again. 79 00:09:01,250 --> 00:09:04,820 latest element will be the length of 80 00:09:07,760 --> 00:09:19,640 Whats problem not back slash its slash. We need to get length of the splitted string 81 00:09:23,680 --> 00:09:31,870 And ee, if we do it like this it will be ok. Because the lenght of the google is starts from zero. 0 is G 1 is O 82 00:09:31,870 --> 00:09:37,540 2 is O 3 is G essetra. Its not starting from one. 83 00:09:37,550 --> 00:09:38,830 So we need to do this. 84 00:09:41,230 --> 00:09:42,850 So our file name is ready. 85 00:09:45,900 --> 00:09:56,700 So let's define let's create one more variable for savepath and it will be temppath +filename 86 00:10:00,650 --> 00:10:12,260 And let's use it. so we are ready, let's test our code, switch back to a program.cs file 87 00:10:12,320 --> 00:10:15,290 and create a new instance of the operations class. 88 00:10:17,540 --> 00:10:18,800 Operations. 89 00:10:22,690 --> 00:10:23,970 OpsObj 90 00:10:26,490 --> 00:10:33,090 It will take the instance of generalinfo class as an argument, so give the infoobj here 91 00:10:35,180 --> 00:10:41,090 And let's try to call our new function the DownloadFile 92 00:10:44,700 --> 00:10:52,460 Or actually, lets call commandparser and give a command something like download 93 00:10:54,360 --> 00:10:57,850 https://google.com/index.html. 94 00:10:59,340 --> 00:11:01,800 These commands will be coming from the attacker server. 95 00:11:03,300 --> 00:11:10,800 So let's try to see if we can download the index.html file into our temporary folder, which is here. 96 00:11:12,000 --> 00:11:13,350 This is my temporary folder. 97 00:11:18,140 --> 00:11:19,460 And let's see. 98 00:11:29,260 --> 00:11:35,330 Ooops, I'm in the wrong directory inside the terminal. 99 00:11:36,070 --> 00:11:37,990 Let me change my directory first. 100 00:11:51,770 --> 00:11:52,640 Where is my 101 00:11:56,530 --> 00:11:57,220 project ? 102 00:11:59,940 --> 00:12:02,230 Sorry about the situation but 103 00:12:08,550 --> 00:12:12,090 It doesn't make any sense. 104 00:12:13,190 --> 00:12:14,430 Where is my files ? 105 00:12:18,880 --> 00:12:26,510 Oh, these inside documents I see, so let me let me go back to documents directory inside the VSCODE's 106 00:12:26,530 --> 00:12:27,030 terminal. 107 00:12:30,250 --> 00:12:31,600 Sorry for the mistake, but. 108 00:12:34,840 --> 00:12:39,080 I won't close the video now because you can face the same errors 109 00:12:39,130 --> 00:12:42,160 So let me show you how to fix these kind of errors. 110 00:12:44,520 --> 00:12:48,430 Also my VSCODE seems like its crashed 111 00:12:49,600 --> 00:12:50,590 Now it's back. 112 00:12:50,810 --> 00:12:51,290 Nice. 113 00:12:53,670 --> 00:12:57,000 Go to documents and redteamdevelop, yep here we are. 114 00:12:57,660 --> 00:12:58,890 Lets try dotnet run 115 00:13:03,860 --> 00:13:08,350 No errors occurred, so let's check the temporary folder. 116 00:13:10,580 --> 00:13:16,490 Temporary folder, again, to do so, go to your temporary folder. 117 00:13:16,520 --> 00:13:24,100 It will be under the AppData\Local\Temp and as you can see, the index file is here. 118 00:13:24,740 --> 00:13:27,710 So we have successfully downloaded the file. 119 00:13:29,180 --> 00:13:30,320 That's it for this video. 120 00:13:30,560 --> 00:13:31,540 See you in the next one.