Hey everybody welcome to the next video.
So in this video we're gonna be going over how to handle sockets in python.
Now as we just to recap a socket is when you make a connection with a port on a remote host.
Now this is gonna be pretty important for a red team as far as the client type of code is going.
We're gonna be talking about.
So there's two sections to this particular video.
We're going to be creating a basic server that can be that can do some sort of logic when it's connected
to.
And then we're going to make a client now like I said the client part is gonna be your most important
aspect that you're going to want to focus on because you can use this for connecting to f or sending
off custom packets that you want to.
So it's important to understand the client side but understanding still how the server aspect works
means that you can make applications that you wanted to at a low level.
Part of the language such as what we're going to be working with now.
So the first thing we're going to want to do is we're going to work with the server.
We're going to import socket which is Python's internal library for handling low level networking so
the first thing we want to do is create a socket object name that SOC can and will do is equal to socket
socket.
Right.
So this creates our initial socket object for us to use during this particular class.
Now we need to set up a host so we can do this two ways you can give it a IP address or the local host
in this particular case however we'll let Python handle the local stuff for the server.
So what we're going to do is render socket
dot get hostname.
That's just going to get the hostname of the local machine which is local hosted in this particular
case could be manually entered.
But for sake of the server we'll just let it do its thing.
We need the port as well as at the port of Port for four 4 just for sake of I'm not using that currently.
Now we need to bind the port and host to the machine.
Now this is doing this to and it actually opens up the port and makes it available so the OS does not
allow it to be used by some some other service.
So what we need to do is create as we've done before a couple which doesn't change.
So if we do socket and then we set that to host in port.
So again couples don't change.
So this is static.
We're not going to change this which is fine.
We don't need it shouldn't be changed for a socket.
Next thing we can do is do a bind.
So we're going to actually bind the data or the socket to the host so we'll do the socket and phone
perfect.
So now we've bound it and registered it to the OS that we are going to be using this service is using
this port.
And that when the data comes in over this port it goes to this service or this naming pipe which we've
talked about before.
That's how this works.
So what we're going to need to do now is tell the server how many maximum connections we expect to have
at a time.
So we'll do SOC Dot.
Listen we're gonna give it five more just a five for this point in time and we'll print out starting
server.
Right starting server 2 3.
Call right now if we ran this it would lock the port up.
However when we fill in the application closes it would release the port but what we're gonna want to
do since this is the server we want to run forever.
Right.
So what we want to do is we want to check for any type of connection that might be coming in and grab
the Client Socket object and then the address of the client.
To do that we're going to get two variables.
We're going to do.
We're gonna grab the client object and the address from socket.
Except
perfect.
So that's going to do is that when we get an x when the accept object spelled.
So when a client connects this will just keep running as soon as the client connects.
This will return a client and address the client object to interface with the socket through the client
or interface with the client through the socket.
And then we just have the address of the particular machine.
So we'll do print.
I can spell today print connection from and then we'll we'll just give it that
and we'll do address.
Right.
So right now this will connect to the machine that will accept the socket and then print us the connection
to us.
And the more we're going to want to do is we're going to send data to the client so toward the way this
works over sockets as you can send and receive data.
So what we're gonna do is we're gonna send data we're gonna send.
Thanks for connecting
them all we're gonna do is we're going to close the connection completely.
So client not close to the a few interesting concepts that are happening here.
So we've got our socket dot or SOC dot except which is the SOC object up here except when it accepts
the client gives us the client object and the address.
So the client object here is similar to the SOC object up here next we're printing it out just so we
know that somebody's connected then we're gonna send data over the wire.
So the Sunnis send it over the socket to the remote end point that made the connection it's going to
say thanks for accepting or for connecting and then we're just gonna close the SOC entirely.
So this is going to send a thin type DCP packet to the host machine and then that will close the TCB
connection.
So that's all we're gonna need for this particular.
This is our server now or our newsroom to create a new file and we'll call this call this client.
Right.
So what we're gonna do is we're going to forego the requirement of using main or anything like that
for this particular one.
We're just going to right this wrong.
So we need to first import socket just like we do with the other one.
Then we're going to create a new SOC object
equal sign here.
No we got a socket object.
Now we're gonna want to get the hostname or IP that we want to connect to in this particular case.
We're gonna set it to socket dot get hostname just because we already are on the same host.
This would be changed to an IP address so we can do 1 2 7 0 0 0 dot one for the local host or a remote
IP address whatever it ends up being.
Again we're gonna have a port 4 4 4 4 and then we have our socket info because we still need to know
what it was.
So host port right now what we've done over here is we did a bind.
What we're going to do here is do a connect socket connect and then socket in look very similar.
However in this particular case it's not binding to a port locally it's connecting to a port going out
then we're going to want to do past this point.
We're going to want to print out the received data because if you remember what we're doing is we're
sending data the second we get a connection from the server.
So in this case we're gonna want to print out
data received and the way we receive the data is through socket dot or ECB.
So SOC dot our E CV and then the amount of data we want to do.
So 10 24 we'll use the standard one killer.
Right.
So we're grabbing one copy of data and we're printing it to the screen.
Now you would need to receive more and more and more just consistently allow loop to get the full amount
of data if you had sent a large amount.
However in this case we're just sending a little bit and that's it and we're receiving it.
And then we can do SOC clubs.
So we're closing the binder itself on the set.
Now just as a quick overview.
So we create a socket object.
We set the hostname or IP we're setting the port and then we're sending our trouble for socket socket
info.
Now you could just copy this into here and do that and that would work perfectly fine.
However in our case we're just going to use it completely separately.
So then we create will we receive our data.
In this case we're not sending that much data.
So we should be able to receive it in one shot and then we're just going to close a socket we're and
do try and do whatever on our server.
So that's not a capital S. That's a lowercase us
but it's later.
Perfect.
So we got our server Python client perfect so it can see we've got our connection from here's my local
IP and then the local internal port that we're connecting on technically.
So don't worry about this per say.
And then we'll see data received.
Thanks for connecting.
So we can take this if we really wanted to step further right.
So we can send you can send a web request with this if you know how the DP protocol works so if we sent
a get request to the root directory for HDP slash one that one enter enter.
Now if we sent this exact packet to read termination dot com as an example then we would get the front
page of the Web site.
So you'd have to you're working with things at a low level so you have to understand what the what data
is actually being sent across the wire.
So it's important to understand that when you're looking at using something like sockets versus what
we've talked about before and a small example which is you are earlier to that makes the web request
for you.
So you just call open you url and it would just give you the data.
In this case you're needing to make it from the lowest level and then sometimes as a red team or this
is the only viable option you have because you might be working with a protocol you don't understand
or that might not be a module built for it yet.