-: Welcome back.
Let us see how we can implement the changing
of the directory inside of our server and backdoor.
So, in the previous video we tested our program
for the first time
and we were able to execute all of the commands.
But, once we tried to change the directory
on the target system, we weren't able to do that.
We were stuck in the same directory.
So let's see how we can bypass this and
how we can be able to change the directories
inside of our backdoor code.
The first thing that we must do is,
we must nano our back door,
and down in the shell function- here we must add an option
if the command starts with CD
First, Python has a library that is called OS Library.
And with the help of the OS library,
we can change the directory with just one single command.
So first thing that we must do is we must import
that library, go up here
and import OS.
Once you do that, down here in the shell function
here we must add the command
or the statement
which will say elif
command
and then equals equals to cd
empty space
then we will change the directory.
But you might notice that there is something wrong
with this statement right here.
For example, we are comparing the command
with CD and empty space
but most likely the command will be something like CD
and then desktop.
So if it compares this
with this, these two will not be the same
even though they start the same.
So what we must do instead of comparing the entire command,
we must compare just the first three characters.
And we can do that by specifying square brackets,
and then two dots up to third character.
In other words, all of this just means
that we are comparing the first three characters
of the command with CD and empty space.
And empty space is the reason why we're comparing
three characters and not two.
I have to re-compare this and they do match.
Then, we can use the OS library and function
which is called chdir which stands for change directory
onto the command and then,
right here we need to do the quite opposite thing
that we did which is from the third character
and then till the end.
Since this function will be something like this,
if we ran the command CD desktop,
then this function will do something like this os chdir
and then desktop.
So what we essentially did is we compared this
and then if they did match, we then used this part
of the command to change the directory.
And this is pretty much it.
We don't need to add anything else.
This will change directory for us.
However, this is not everything that we must do.
What else we must do is we must go
to the server code and add the same command
inside our target communication function.
So right here we can add another elif statement
elif command equals equals to
cd
empty space
And here remember that we also must add
from the beginning up to third character.
Just in this case we are not going to do anything.
We're going to just pass because in server
we don't want to do anything, we just want to go back
to the beginning where we can send the next command.
And in the backdoor code we simply just
change the directory of our program.
Now, there is one more command that I want to add
in this video and that is the clear command.
This is not really that useful, but once you have a bunch
of commands and outputs on your desktop
you can use something similar to this
that we use right here.
So if for example, type sudo ifconfig
and it outputs me with the ifconfig command
I can just type clear to clear the screen.
So let us do something similar inside of our code.
First we are going to go with this server
and if we send the command clear,
this command will get sent to the target.
However, we want to execute it
right here inside of our server
since there is no point in executing the clear command
inside of our backdoor code on the target system.
So what we are going to do right here is we are
going to add another elif statement-
elif command equals equals to clear.
We're going to use once again the OS library
and we are going to call the system command
which will execute the clear command.
This system function allows us to specify any
command in the brackets
which will then get executed inside of our terminal.
Of course, since this is inside of a server code
we must import the OS library here as well.
And since the command will get sent
to the back door as well, we want to cover that step also
inside of our shell function.
So let us go right here
and add elif statement command equals equals to clear.
Just in this case, we will pass and not do anything.
Okay, simple as that.
And these are just two commands that I wanted to cover
in this video which only this CD command
is really important for us right now.
But in the next video, what we are going to do
is we are going to see how we can download
and upload files to the target system.
Then at the end we are going to test our final version
of program and see whether all of these commands that we
imported work or if there is something that we must change.
So for now on we got execution of the command,
changing of the directory.
We can clear the screen if we got too many things happening
and we can quit outside of the shell.
Great, let us continue coding in the next lecture.