In numerous instances during penetration tests.
We as hackers have found ourselves in situations where we urgently required a TCP client, whether it
was to test services, send malformed data, perform fuzzing or tackle a range of other tasks.
So the need for TCP client has been prevalent.
However, operating within the confines of large enterprise environments often means forfeiting the
luxury of creating our tools.
So now we will write our code and I will explain.
First, we will import the sockets here.
Socket is a socket here and we will, for example, let's make the target host.
Target host.
Let's make the google w-w-w dot google.com.
Of course, the port of Http is 80 target target port here is 80 http protocol.
And now we will create a socket object.
So client here we will socket dot socket here.
So firstly we will here, let's try it.
And the second first that it gets address family and then socket kind.
Right.
So socket.
That a of init here and socket.
But sock stream.
And here then we will connect the client.
So client dot.
Connect.
Connect here and now.
Firstly, we will enter the target host and target port, right?
And here we will send some data.
In order to do that, we will use the client dot send method and here we will get we will enter B here
get.
Http and 1.1 R and n host.
Now we will enter Google.com and after that we will inside this.
Print.
N r n here.
And after that we will after sending, we will need to receive data to show print on the screen.
Right?
So now we will print the response.
Response dot decode.
And after that.
It will add another here.
Client that clause.
That's it.
So here we after that we send sending some data.
Of course, we need to get the create this response here.
And in order to do that, of course, we need to personally do this like that.
And after that, getting the response from the client.
We will open the here and now we will get the response.
So response.
Client client.
Client that rests here.
And 4096.
That's it.
So here, let's run this.
And as you can see here, we got this.
Let's actually change this to double, double, double dot.
Auxilia.
Com and here.
House.
And as you can see here, we got a bad request here.
We have a firewall on our website.
So to create a TCP client, we begin by initializing this.
Actually, I'll get my pen, my pen here, and I will draw things on the screen.
So to create a TCP client.
We begin by initializing.
A socket object.
A socket object with n parameters.
A if init and sock stream.
And the af init parameter signifies the use of the standard IPV four ipv four protocol and the address
or hostname.
While the SOC stream here indicates that this is t c p protocol.
Right.
So and indicates that the client will operate over TCP.
Next, we establish a connection between the client and server here.
Connect with this code here.
Actually.
Yes.
So we next we establish the connection with client and server, ensuring that they can communicate to
send data to the server.
We convert the we convert data into bytes by bytes and transmitted.
So the final statement was receiving a response from the server which is here.
So the and after that we are printing out here, right?
So and after that we are closing that.
So the code here provides a simple and commonly used form of TCP client.
However, it's important to acknowledge that certain assumptions made by these quotes, this code here
regarding sockets here.
So firstly, it assumes that the connection will always succeed.
Secondly, it assumes that the server expects the client to send data first, even though some servers
may anticipate sending data to the client and the waiting a response.
Lastly, it assumes that the server will consistently return data promptly so these assumptions are
primarily made for the sake of simplicity.
So while there are various approaches to handling, blocking sockets, exception handling and similar
aspects, penetration testers often prioritize efficiency and expedience when building quick and dirty
tools for reconnaissance or exploitation purposes.
Consequently, in this lecture we will omit the inclusion inclusion for more advanced socket handling
techniques.