Instructor: Welcome back.
Time to code our download and upload functions.
So for now, we coded a bunch
of different options such as executing commands, quitting
out to the shell, 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 back door directory.
Here I will nano our server
and inside of the server down here we want to
code the download function first.
So download 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,
but from our backdoor code we will be uploading files.
What do I mean by that?
Well, since backdoor will be running
on the target system for that machine,
the code will upload the file to our server
while from our server perspective
we will be downloading that file to our Kali Linux machine.
So inside of the server here,
we want to add an option, 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 are comparing, if the command starts with download
then we are going to download a specific file.
And to do that, we're going to use download_file function
This function will take the parameter of the file name
so we are going to type command
and from the ninth character till the end.
Of course, this function we're 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.txt.
We're comparing first eight characters
if they're equal to download.
If they are, that means we are downloading a file
and then we will paste this to this download file function.
Since we are going to strip first nine characters off
which will be the download word
and the empty space.
Okay, now that we did this
let us code the download function up here.
So we're going to code it below the reliable receive.
Let us define it first, so define download_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 harder 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 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 Kali Linux machine.
Therefore, inside of our server code
we want to
write
bytes.
So what does this mean?
Well, we are 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 are essentially writing bytes to our file.
Then another thing that we must add
is the target.settimeout.
This is a call function that we get from the socket library
and we are going to set the timeout to be one in this case.
Why?
Well sometimes if we don't set the timeout it might
actually get stuck and not allow us to download the file.
Of course, this timeout 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 timeout
so our program doesn't crash.
Great.
Once we initiate the timeout,
we're going to initiate a variable called chunk
and this chunk will be a small parts
of data that we're going to receive multiple times.
So we're going to type target.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 1024 bytes.
So how are we going to keep receiving data
until the file size is 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 call F
and using the right function onto that object.
So file.write 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 chunk = target.receive.
We're going to go with 1,024 bytes once again.
And if we
run into a timeout, socket.timeout
as error,
we are going to simply break out of this wild chunk loop
because that would mean that we reached the end of the file.
And at the end, let's not forget to set the timeout
to nonexistent.
So we can do that by specifying
target.settimeout 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 anytime
that we open a file object, once we are finished
with that object, we must close it inside of our function.
So I will just type f.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 correspondence function
to this, inside of the backdoor code.
Remember, from here we are downloading a file
but from the backdoor,
we are uploading a file.
So what we must do right here
is
inside of the shell function
we must add an else if statement.
So else if command, first eight characters
are == to download, we're doing
the same thing that we did in our server.
Just here, we're going to call upload file
onto the file name.
So from the ninth character till the end.
Since our back door is uploading a file
the function will be quite a lot easier to code.
So let us go up here and
below the connection function we can define upload file.
This of course will take file name as the parameter
and 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 back door we are 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 s.send
and the thing that we are sending is file.read.
This is the entire upload file function.
But before we actually close off this video,
we must also code the upload function.
What if we want to upload a file
to the target system from our Kali Linux machine?
While 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
(keyboard typing)
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.send into target.send.
When will this function run?
Well, it'll run if the command starts with upload.
So let's define it down here in the target communication.
Else if command
and first six characters are == 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.
Okay, 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
else if
command
First six characters are == to upload
(keyboard typing)
Then what we want to do is we don't want to upload the file,
we want to download 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 back door.
Therefore, from the back door 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 coded the 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 commands and see if our back door works
on the target system, if our quit 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.