Instructor: Welcome back.
Now that we covered our shell
and target communication functions,
it is time we employ the reliable send
and reliable receive functions.
So if I go to my server code right here,
you will notice that we did code a part of the
target communication function,
but this is not really working.
These functions do not exist.
We must create them first,
in order to be able to successfully send and receive data.
Let's start with the reliable send function first.
Since that one is easier, we just need to send the data.
And for this we're going to use the JSON library
in order to more easily parse the data.
So down here I'm going to define reliable_send
and reliable send will take one parameter.
As we can see right here,
it has to be the data that we are going to send.
So we're just going to name that parameter, data.
Then let's add two dots.
And inside of the function
we are going to define something called JASON data.
And this is going to be a variable that is going to
store the output of the dumps method
from the JASON library onto our data.
Now keep in mind,
that this data is the actual command in our case.
Since inside of the target communication function
once we call the reliable send
it parses the command inside of the brackets.
So you can just change those commands
to be this data right here.
Then we perform the JASON dumps onto that command
and we store it inside of JASON data.
And finally we can use the send function
from the socket library
onto our target to send the actual data.
So we can type JASON data.
And just to remind you,
this target is the actual target socket object
that we get once we accept the connection.
We're just using a socket send function
onto this target object once we are sending the data.
But one more thing before we finish
with the reliable send function
is that in Python three,
once we are sending the data over sockets,
we need to encode that data.
So we can do that by
simply specifying inside of the brackets,
our data and then encode function onto it.
Then it will first encode the data
and then it will send it with the send function.
Simple as that.
Now the same thing we need to do with our reliable receive,
so I'm going to code it right here.
Reliable receive.
And we know that
the reliable receive doesn't take any parameters
inside of the brackets.
And what we are going to do to receive the data
is we are first going to define a variable
to be an empty string.
We're just going to call it data.
And we are going to then enter inside of the infinite
while true loop.
Inside of that loop,
what we're going to try,
is we are going to try to get the data
by typing data=data+target.receive.
And just to remind you,
the receive function is the function
from the socket library.
So we are just using it once again
on to our target connection.
And this receive function takes one parameters.
That is the amount of bites that we want to receive.
In our case,
we are just going to specify thousand and 24 bites.
But this is not everything that we must do.
Remember that once we are sending the data
we actually encode the data.
Well in order for us to get the data
as it was before it got encoded,
we must then decode the data once we receive it, right?
And at the end we are going to add
a strip function onto all of this.
Okay, great.
After that, once we receive the data,
we can return from this function the Jason.loads of our data
and this is just the format that we're going to output it.
Since remember we must return from this function,
as it does get stored inside of a variable.
Our result will be our data.
And in the accept statement,
of course, we must not forget that.
So I'm going to go down here and add except.
And what we are going to accept is the value error.
So in case we get the value error,
we are simply going to continue with the execution.
So once again, what is this function doing?
Well, it initiates data variable to be empty string.
Then we try to get thousand and 24 bytes from our target.
If we add it to the previous data that we received,
then we decode the data of course before adding it
and then we strip it from any additional characters.
We then return the JSON loads of that data
and that will be stored inside of the results variable
that we then print to our screen.
So all we need to do right now
is we need to add these two functions
to our backdoor code as well.
So copy them.
Save the server code and then nano backdoor.
Go up here
and paste the functions.
However, there is one thing that we must change
and that is this target.send right here.
Since we are not sending to the target from our backdoor,
we are sending to the server
and we initiated this socket object to be S.
So this is our only socket object
and we are going to use that to send our data.
The same thing we want to do inside of the
reliable receive function.
We are not receiving from the target
using the S socket object.
Since if we left here target,
target is undefined inside of our backdoor code.
Therefore, it will throw us an error.
Everything else can stay the same.
And now that we got these two functions ready,
in the next video
we are going to write the part of the code
that will execute the command.
It is once again stored in this L statement right here.
We for now just put the comment
and in the next video we're going to use the
sub process library to perform the action
of executing the command.
See you in the next video.