-: Welcome back.
Time to start coding our back door.
So in this video we're going to start
with the base of our program.
Remember that we must create 2 different programs,
a server and a back door.
And it doesn't matter with which one we start.
So let's, in this case start coding the server first.
We're going to code both of them simultaneously.
So what I'm going to do is I'm going to open the terminal.
And remember that we have this tools directory
from the first project.
Let us navigate to there, and instead of the port scanner
we're going to make another sub directory
inside of here and call this back door.
Then we're going to change the directory
to the sub directory, and here we are going
to code our tools.
So as I said, let's start with server first.
To do that we type server.py.
We open it using nano and here
we're going to code our project.
So what's the first thing that we must do?
Well, since these 2 programs are going to communicate
with one another, they must establish a connection first.
And we know that we can do that
with the help of a socket library.
So socket allows us to initiate an internet connection
between 2 machines.
So how are we going to do that?
Well, first thing that we must do
and that we also did inside of our report scanner
is we must initiate a socket object.
And I'm going to call my socket object sock
inside of the server.py
and to initiate it I will type socket.socket.
So we are doing this with the help
of the socket library and here
in the brackets we want to specify 2 different parameters,
socket.AF_INET,
socket.SOCK_STREAM.
And I know what you're thinking.
What even are these arguments?
Well, this socket.AF_INET tells our program
that we are going to make a connection
over IPv4 address.
And this socket.SOCK_STREAM tells our program
that we're going to use the TCP connection.
So simple as that.
Now the next thing that we want to do is we want
to bind the ip address and the port.
This is something that we already did inside
of our first project, so there's nothing really too much
to explain right here.
We just type sock and use the method .bind,
open 2 and close 2 brackets.
And the first parameter is going to be the ip address
of our cal Linux machine.
In my case, if I go and check it out,
this is .1.12.
So we'll go right here and specify 192.68.1.12
as the first parameter.
And remember it must be between the quotes.
Then I will specify ,
and the port that we are going to use,
well, we can just go with port 5555.
Why not?
It doesn't even matter to be honest.
After we do all of this, we binded the ip address
with the port.
And now the next thing that we must do
is we must start listening for the incoming connections.
Remember this was the crucial part in the reverse shells.
The target executes the payload
but we must listen for the incoming connections.
And that is exactly what our server program will do.
It will listen for the connections and
once the target executes a payload,
they will connect to our server program.
So to do that, we can first print like this,
let us print Listening For The Incoming Connections
just so we know at which part of the program we are.
And below that we can type sock.listen
and we are going to specify 5 in the brackets,
meaning that we're going to listen
up to 5 different connections.
Okay, so our program will now be stuck on this
part until the connection is established.
Once the target tries to connect back to us
we need to store their connection in a few variables.
In other sense, we need to store their socket object
that we're going to use to communicate with the target
and we're also going to split that into an ip address.
So what I'm going to do is I'm going
to type target, ip.
These are going to be 2 separate variables
and they're going to be equal to sock.accept.
And this accept method is simply just
accepting the incoming connection and storing the target's
socket object right here and the ip address
in the second variable.
Simple as that.
Once we do that and once the connection is accepted
we can print that Target Connected
and we can add From.
And what we are going to do here is we are going
to close the quote and add a + sign and then the string
of the ip variable.
And once again, remember the ip variable will store
the ip address of the target.
So what we are essentially doing right here
is we're printing that we got the connection
from the target's ip address.
Okay, we need to close one more bracket right here.
And what we're going to do at the end
is we're just going to enter a function
called target_communication.
Now of course this function doesn't exist and
we're going to code it in some future video.
But for now let's just leave it right here.
We successfully created a socket object,
binded the ip address with the port.
We listened for the incoming connections
and at the end we accepted the connection
from our target system.
We're going to leave it at this for now
on our server program.
And let's go to our backdoor program.
Now we need to figure out the code for our backdoor
to make it connect to our server.py.
So first thing that we're going to do is
of course to import the socket library.
Then as in the server program
we need to initiate the socket object.
And I'm not going to call it sock right here,
I'm just going to call it s.
And here we're going to specify the same parameters,
socket.AF_INET,
socket.SOCK_STREAM.
We already explained what these are.
And the only thing that we need to do right here
is we need to connect to our target machine.
But we're not going to use the connect method right here.
What we are going to do is we are going
to call the connection function.
And of course this is a function that doesn't exist.
So we must code it up here.
Let's define it first.
So define connection.
It will take no parameters between the brackets.
And what we are going to do is we are going
to type right here, try, which is the try statement.
So it will try to connect to our cal Linux machine.
And remember the connect function requires 2 open and
2 closed brackets the same way
that the bind function does.
And it also takes 2 parameters, which the first one
is the ip address of the machine that we want to connect to.
So this will be the ip address
of cal Linux machine once again.
And the port will be once again the port 5555
because we want to connect to that port,
since our server program will be listening on that port.
If it manages to connect, we're going to enter
a second function which is going to be called shell.
And this shell function also doesn't exist.
We're going to code it and what will be the purpose
of this shell function is executing the commands.
So for now on, we're just going to leave it right here,
and we're going to code it later.
Once we leave the shell function, we can close
this socket object and in the except statement,
we can call again
this connection function.
Now you might be wondering why are we doing this?
Well if I add something like this right here.
So I go and type while True,
which is remember the infinite loop
and I tab all of these commands once
so they can belong to the while True loop.
And right here I'm going to also
add a statement, time.sleep.
And gimme just one second, I will explain
why I'm doing this.
Let me just code the function till the end.
I will break right here and I will import the time library
because we are going to need it since we use it right here.
So what are we doing right here?
We're calling the connection function.
This connection function starts an infinite while True loop.
This infinite while True loop sleeps for 20 seconds,
and then it tries to connect to our cal Linux machine.
If it manages to connect, it will go inside
of the shell function where we will execute the commands
on the target system.
If it doesn't manage to connect, it will go
into this except statement and it will call
the same function once again.
So, what does this tell you?
It will run this function infinitely
until it manages to connect.
So this is good because of one reason.
We don't want our target to start the payload
and not be able to connect
just because we haven't started the server yet.
We want to be able to connect
to the target system whenever we want.
So this function will tell the payload to try to connect
to every 20 seconds.
Every 20 seconds while their machine is running,
this program will try to connect our cal Linux machine.
So we can start the server at any point of time,
and after 20 seconds it will establish a connection to us.
So this is just calling this function over and over again
until this line right here works, and they connect to us.
Then we enter a second function which is the shell function.
Okay, this is the base of our backdoor.py.
Here is how we are going to connect to our server.
And in the next video we're going to see
what we're going to do with the contents
of the shell function,
and if I save this control O
and also the target communication function.
So these are the functions that will receive
and execute commands.
Feel free to post any question if you have about the code.
If there is something that you do not understand.
And in the next lecture we are going to continue
with the coding of our programs.
See you there.