Time to code our download and upload functions.
So for now, recorded a bunch of different options, such as executing commands, cutting out the shelf,
changing directories, clearing the screen.
But let's also see how we can create two functions that will allow us to download and upload files on
the target machine.
So let's start with server first.
I'm going to navigate to my tools and to the backdoor directory here, our NENO, our server and inside
of the server down here, we want to code the download function first so Don't Function will be used
to download the files from the target machine.
And the reason why I'm saying this is because even though it is obvious from our server, we will be
downloading files that from our backdoor code, we will be uploading files.
What I mean by that?
Well, since Back-Door will be running on the target system for that machine, the code will upload
a file to our server, while from our server perspective we will be downloading that file to our Linux
machine.
So inside of the server here, we want to add an option or else if the command and then first eight
characters and you might be wondering why.
First eight characters.
Well, because Download has eight characters and we're comparing if the command starts with download,
then we're going to download a specific file.
And to do that, we're going to use download, underscore file function.
This function will take the parameters of the file name, so we are going to type command and from the
ninth character till the end, of course, this function, we are going to code right now.
But just to clarify why I wrote it like this is because once again, if the command is something like
this, download file that the.
We're comparing first eight characters, if they're equal to download, if they are.
That means we are downloading Apple and then we will paste this to this download file function since
we are going to strip first nine characters of which will be the download world and the empty space.
OK, now that we did this, let us code the download function up here, so we are going to code it below
the reliable receive.
Let us define it first.
So define the download underscore file.
As we already know, this function takes one parameter, which is the file name.
And this is going to be a little bit of a hard function to code, but let's give it a try.
We're going to start like this.
We're going to initiate a file object and how we do that in Python, we simply specify the file object's
name and then we open it.
But besides of opening it, we must specify the way we want to open it, whether we want to open it
for reading or for writing.
That is a second parameter to this open function.
In this case, since we're going to download the file, we want to write the content that we receive
from our back door to the file that we create on our Linux machine.
Therefore, inside of our server code, we want to write bytes.
So what does this mean?
Well, we're opening this file object to store the contents of the file that we want to download.
That's why we're going to write that content.
And this W stands for right while this B stands for bytes.
So we're essentially writing bytes to our file.
Then another thing that we must add is the target date set time out.
This is a function that we get from the socket library and we are going to set the timer to be one in
this case.
Why?
Well, sometimes if we don't set the time out, it might actually get stuck and not allow us to download
the file.
Of course, this time out is something that at the end of this function, we must remove so it doesn't
interfere with other commands and connections.
But for this function, we must set the timer.
So our program doesn't crash.
Great.
Once we initiate a timeout, we're going to initiate a variable called chunk and this chunk will be
a small part of data that we're going to receive multiple times.
So we're going to type target that receive and we're going to specify inside of the brackets amount
of bytes that we want to receive.
In this case, we are going to use a thousand and twenty four bytes.
So how are we going to keep receiving data until the file sizes over?
Well, we can just type while Chunk and this while Chunk will simply run this while loop, as long as
there is something inside of the chunk variable and if there is something inside of the chunk variable,
we want to write that something to our file.
And we do that using our file object that we called F and using the write function onto that object
so far.
That's right.
And we are writing the chunk inside of that file.
Then after we write that, we're going to try to receive the chunk once again, so Chank equals target
dot receive, we are going to go with two thousand twenty four bytes once again.
And if we run into a timeout, so socketed a timeout as error, we are going to simply break out of
this while chank loop because that would mean that we reached the end of the file.
And at the end, let's not forget to set the time out to nonexistent so we can do that by specifying
target date, set time out to none.
We are simply just removing this statement right here that we initiated at the beginning of the download
file function.
And the one last thing that we must do is any time that we open a file object, once we are finished
with that object, we must close it inside of our function.
So I would just type F that close.
Great.
This is our download file function that we're going to run inside of the server code.
Now let's see how we can create the correspondant function to this inside of the backdoor.
Could remember from here we're downloading a file, but from the backdoor we are uploading file.
So what we must do right here is inside of the shell function, we must add and else if statement so
elusive, command first eight characters are equal, equal to download.
We are doing the same thing that we did in our server just here.
We are going to call, upload file on to the file name.
So from the ninth character till the end.
Since our back is uploading a file, the function will be quite a lot easier to code.
So let let's go up here and below the connection function, we can define upload, file.
This, of course, will take filename as the parameter and the only thing we need to do is open the
file that we want to upload to the server.
In this case, we are going to define the file name as the first parameter, just as we did inside of
our download file in the server code.
Just this time we are going to read bytes from this file.
Why?
Well, from the backdoor.
We're not writing or storing a file onto the system.
We're simply just reading the contents of the file that the server wants to download.
And we are going to send that content to our server program.
So how can we do that in Python?
Well, we can type as the tent and the thing that we are sending is file dot, right?
This is the entire upload file function, but before we actually close off this video, we must also
go the upload function.
What if we want to upload a file to the target system from our Kalinda's machine?
Well, it is pretty much the same thing, we just need to reverse these two functions, which are upload,
file and download file.
So what I'm going to do is I'm going to copy this function, save this program.
Go to my server code and I'm going to paste the upload file function right above the download file just
here.
I'm going to change the S DOT sent into Target, DOT sent one.
Will this function run?
Well, it will run if the command starts with upload.
So let's define it down here in the target communication or else if command and first six characters
are equally quick to upload.
Then we are calling the upload file function onto the command and from the seventh character till the
end, once again from the seventh character, because after the seventh character starts the name of
the file that we want to upload.
OK, great.
And one more thing that we must do is now we must copy this download file function and add it to our
backdoor code.
Let's save this, go to our backdoor code.
And since we already have the upload file function.
Right below it, we're going to add the download file function, but of course, we must change this
target into S because Target is not initiated inside of our backdoor code here as well.
Inside of this statement as well, we must change.
And at the end, once we remove the timeout, we must also add.
S great.
Now that we did all of this, our functions should run just before we close off this video.
We must also add the upload option inside of our shell function.
So let's just go down here below the download and type or else if command.
First, six characters are equal, equal to upload.
Then what we want to do is we don't want to upload the file, we want to download a file, and it is
the same principle as with the previous function, since if the server sends the upload command, it
wants to send its own file to our backdoor.
Therefore, from the backdoor perspective, we are downloading that file to the target system.
And what we must specify is command from the seventh character till the end.
And that would be about it, we go there to download and upload functions now, we are not going to
test them inside of this video, but in the next video, we're also going to test our previous comments
and see if our back door works on the target system, if our quick command works, if execution of the
commands work, if we can change the directory and at the end we're going to see whether we can upload
and download the file from that target machine see in the next video.