Hello everybody and welcome back.
And right now let us start off first by coding a server connection with this socket library.
Now before we actually begin let me just tell you a few things.
Basically things will break from time to time.
So don't let that upset you or discourage you from actually finishing this code at the end.
It should all work properly but if you have some problems make sure to check out on the Internet basically
some of the functions may be outdated on your python version or something like that and they may have
a new name or a different name or something like that.
Basically I will code right here in python 2 so but I will also sometimes use the the syntax for Python
3 so that you actually can use this on both pythons but most likely some of the tools right here will
be from Python 2 or basically libraries.
So right now let us get started with our server coding.
So what we want to do is as I said in the previous video we want to create our connection with the socket
library.
So let us do that.
So first of all let us make a directory for reversal.
So let's call it reverse and we have our directory out here so we don't mix these Python basic basic
programs with our reverse shell so let's change our directory to the reverse shell.
Now lets now know our server.
So we will start off with coding the server so server not be why we should start off as a as in every
previous video we the path to the actual python.
So hash tag I mark and then slash user share item.
This is something that you need to have included in all of your python codes.
And after that we want to import the library that we will use which is called socket library.
So import socket right now what we will do is we will basically create a simple connection with our
server.
So let us do that.
We will use first of all what we have to do is basically we have to declare our socket so our socket
will be IP before and it will run over DCP.
Now the function that specifies that is specified like this.
So we will type here as equals socket not socket and then open parentheses loops parenthesis socket
dot a f underscore iiNet which basically stands for IP before address.
So we want to make a server that police are on on IP address and then comma socket dot SOC on the score
stream which specifies that we want to have a DCP connection between our server and our client.
So this is the beginning code of actually defining the socket itself the socket descriptor which which
will be written into the memory and actually accepting the connections after that.
So this is how you do that in Python.
It is very similar in other programs as well.
So this f underscore iiNet and soc underscore stream is something that you probably saw if you coded
some DCP servers in other programming languages as well.
So after we do that what we want to do is we want to set some of set some of the options for our socket.
So just follow up with me with the DOT set sock hop.
I believe that is I will specify this function and involvement type a socket that S O L on the socket
comma socket that has 0 for use HDD R comma 1.
So this is just some of the basic Socket options.
This socket that s o underscore reuse ADR basically allows the process to connect to a port our local
hosting port even while it is in time.
Wait so it will make more sense later.
But let's see our picture and what we have to run next in over in order to create our server.
So we set up our socket with this command right here.
We set up some of the options so we specified our socket.
Now what we have to do is actually bind the socket itself with or basically bind the port that we will
listen on and also bind the IP address.
So we have to specify the IP address that we want to listen on.
Now in the process of actually creating this we want to specify a local host IP address which is basically
same for everyone which is 1 27 0 0 0 that one since we want to be able to run both client and both
server from our calendar next machine.
Once we actually notice that everything is running smooth and good and this program doesn't break then
we can switch the IP address to our local IP address or port forwarded or basically.
Yeah basically a local IP address or of the an IP address if we run the attack over the Internet.
But if you're on the attack on local host you want to have your local IP address in the US that bind
the function that we will write right now and you can specify any port you want as long as it isn't
used currently by the system so don't specify a port like 80 or 22 or something like that.
If you have a search open specify some random high port so let us write the function which is bind so
as not bind.
Now make sure that whatever you call your socket right here which in my case I only called S.
Make sure to type it as well in other functions so for example if I call this SOC I would have to write
here as well SOC that bind and soc.
That said stock options so make sure to follow up with that.
So don't actually name something different and then use a different name later on you have to use the
same socket name for the entire program.
So let us bind our port and our host.
We do that with open double parentheses so open two parentheses then type here your local host IP address
which is why not in 127 0 0 that 1 Clause the apostrophe and we can see this is actually a string so
it is under 2 double quotes.
And right now we want to specify a comma and after the comma we specify the port that we want to listen
on which in my case I want to specify five four three two one doesn't even matter as long as the port
isn't used by anything so close double parentheses.
And right now we were successfully specified our IP address which is our local host IP address and our
port that we will listen on.
As I said before this is something that you will change after we finish the entire program so you can
use it on any other machine right now as we can see right here.
The next thing we want to specify is the listening we want to listen for the incoming connections so
you can do that with as not listen.
And let's specify one in the parentheses.
Basically it means it will listen for 1 collections if you specify 2 it will listen for 2 connections
let's specify 5 connections so it licenses for 5 connections after we do that let us actually accept
the connection itself.
And if you see right here the accept e doesn't show what it has to take but basically what it has to
take is the target and the IP address the target is a sock.
Basically the file descriptor that it will store in memory in the IP is the IP address of the target
machine.
So let us name it something like this.
So Target
comma IP equals as Dot accepts and you open and close the parentheses.
Now in the accept function you do not have to specify anything in the parentheses.
You can basically just specify the parentheses opened and closed which means it on the use function
without the actual input in that function.
So with this we get the file socket socket descriptor for our target and it stores it and we also get
the IP address of our target which we will use in order to actually print out from which IP address
we received the connection.
Right now we can save this program as this is this most simple use of server after we accept the connection
we can print just so we can see that it does work so print for example target or targets connected.
Exclamation mark.
We put that in the in the double quotes and we can then close the connection so as not close.
And this is our server finished.
So let's actually try to see if we can receive a connection with this program.
It might break.
I didn't double check this before I already encoded before.
So we will fix anything that actually breaks in our coding process.
So one thing I want to specify right here is when it starts to listen I want to print
listening listening for incoming connections so we want to print once it listens and then we will try
to connect with our other terminal and if it connects successfully it will print Target connected and
then it will close the connection.
So let us save this so control low and through and then control X and then we want to see more of this
so it can actually be an executable and we want to run this.
Permission denied.
Why is the permission denied.
Bad interpreters so let's see your share python.
Okay well let me just see what our other programs so cat.
Python 3.
User.
Oh yeah.
It's user bin python.
It's not user share python.
Pardon me.
So we need to specify what was the directory in reverse.
We want to specify actually user bin python it's not user share so change it is not really sure why
I typed user share.
Probably because I was talking about this point and then that site is located in user share so just
change this to user slash pain slash python.
Save this exit and now see page mode it will did each month so we can actually run the server right
now as we can see it just prints out right here listening for incoming connections so we know it performed
all of those functions correctly and now it is listening for an incoming connection now we can specify
an incoming connection with a different terminal so let's open up a new window let's actually enlarge
this a little bit so something like this and right now let's try to not get on our local coast I'm not
really sure that's impossible it should be so and see what is the five four three two one and here we
can see that we successfully connected to our program as it as we can see it said targets connected
so we successfully connected to it and it close the connection so our server works it can accept the
connection and it will print out target connected once someone connects to our server so good now that
works so that is the most simple coding of the server we will fix that code a little bit that something
here and there and then we will continue with our client afterwards.
Now in the next section we will or not the section in the next video we will actually code the simple
version of our reverse shell of our client or basically our payload that we will deliver which will
successfully connect to our server that be wise we can run to python programs and see if they interact
well with each other.
So that would be about it for this story so I hope I see in the next one and take care my.