In this lecture, we will create a function to download files, this function will be used for downloading
additional modules to the system.
To do so, we will create a class called operations, and inside the class it will create two functions
commandparser and downloadfile.
The commandparser function will take the command as argument and it will call the corresponding function according
to the command.
So we will be using the commonparser function in our further operations.
If the command contains download keyword, then the parser function must call downloadfile function.
In order to download files and sending HTTP requests, we will be using Web client class.
So let's start coding, but if you are new to this concepts, I highly encourage you to learn more about
HTTP and Web client class before continue.
Let's start coding.
We will create a new class to do so click the new file button, I will call it ops.cs and copy the
contents of the program.cs
into the new one and change the class name.
I will use operations as my class name.
And let's create a constructor.
We will be using our operations class in further operations, so we may need the information inside
the generalinfo class, so we will create an instance of generalinfo class inside the operations
class.
I will call it the instance or I will call it an ninstance.
It will be our
instance of the generalinfo class and our constructor will take an instance of the generalinfo class
as an argument.
And we will initialize the ninstance value inside the constructor.
So.
Let's start creating functions, our first function will be the commandparser.
CommandParser.
command parser function will take a command as argument.
So let's create a string argument and our command will be something like this.
Yeah, like download and a URL.
So we need to parse our command into two sections.
One section is the command and second section is the arguments.
To do so, we will use split function.
I will call it cmd, and our command will be lets create a new string and our command will be the
first element when we split the string according to the space.
When we split it according to space,it will be that the first element will be download
the second element will be the argument.
So let's create a new variable argument.
It will be the second element from the split according to the space.
And if command contains
download keyword our commandparser function will call the download file function.
Let's create that function also.
DownloadFile
It will take URL as argument so lets create it
What's the problem here ? Oh i see scope problem.
I need to define it outside the scope of the commandparser.
So thats it and
let's call the downloadfile function inside the commandparser function file with the argument,
which is your URL and inside the downloadfile function we will be using Web client class download method
of WebClient class.
To do so
let's create an instance of the WebClient class.
Or we can define it as a variable of our class.
But it doesn't matter, actually.
Yeah, just just create it inside the downloadfile function, it's OK. To do so, we will be using
system.net namespace WebClient class
let's create an instance of it
I will call it winstance, and it will be.
And we will be using download file method of the Web client class to do so, simply find function.
It takes two arguments.
The first argument is the URL, which we already have, and the second argument will be the filename
and the save path.
To do so, we will be using current users temporary folder. In order to find it
we will be using GetTemp method of the Path class.
To use it we will be using system.IO namespace.
**unnecassarry talk**
A this function returns something like this, like C:\users, blah, blah, blah, blah.
So we need a file name to append it. To do so
let's get the file name from the URL, because our URL will be something like this
google.com/index.html you need to get that part (index.html part).
So if we split the string by according to the slash, we can get this value.
Let's do it.
File name will be URL.split we will be splitting according to Slash and we need to take the latest
element because it could be something like this also
directory/index.html, we only about this part (index.html)
So we need to get the latest element when we split it according to / to do so, we will get the latest element
by using the split function again.
latest element will be the length of
Whats problem not back slash its slash. We need to get length of the splitted string
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
2 is O 3 is G essetra. Its not starting from one.
So we need to do this.
So our file name is ready.
So let's define let's create one more variable for savepath and it will be temppath +filename
And let's use it. so we are ready, let's test our code, switch back to a program.cs file
and create a new instance of the operations class.
Operations.
OpsObj
It will take the instance of generalinfo class as an argument, so give the infoobj here
And let's try to call our new function the DownloadFile
Or actually, lets call commandparser and give a command something like download
https://google.com/index.html.
These commands will be coming from the attacker server.
So let's try to see if we can download the index.html file into our temporary folder, which is here.
This is my temporary folder.
And let's see.
Ooops, I'm in the wrong directory inside the terminal.
Let me change my directory first.
Where is my
project ?
Sorry about the situation but
It doesn't make any sense.
Where is my files ?
Oh, these inside documents I see, so let me let me go back to documents directory inside the VSCODE's
terminal.
Sorry for the mistake, but.
I won't close the video now because you can face the same errors
So let me show you how to fix these kind of errors.
Also my VSCODE seems like its crashed
Now it's back.
Nice.
Go to documents and redteamdevelop, yep here we are.
Lets try dotnet run
No errors occurred, so let's check the temporary folder.
Temporary folder, again, to do so, go to your temporary folder.
It will be under the AppData\Local\Temp and as you can see, the index file is here.
So we have successfully downloaded the file.
That's it for this video.
See you in the next one.