Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

TCP client sending with python

This code is taken from the book titled "Black Hat Python". Specifically Ch.2 "Networks: The Basics".

Code
import socket

target_host = "www.google.com" target_port = 80

# create a socket object  
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the client  
client.connect((target_host,target_port))

# send some data  
client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

# receive some data  
response = client.recv(4096)

print response


My question's regarding this line:
Code
client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

I understand that client is sending to the tcp server but i don't understand the whole request. I know the GET requests information, but i don't understand anything after GET, and what does \r do?

Thank you
client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

GET - means send GET parameter.

after GET you specify to witch file you will send GET request.
etc... index.php?id=parameter.

HTTP/1.1 means version of protocol.
\r\n means new line.

Host: google.com - means to witch host.

😉
Considering you don't even know what \r does and you couldn't even bother googling it yourself, you should really learn the language more before going through this book.

(Mar 16, 2019 04:05 PM)Ramb0x Wrote:  client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

GET - means send GET parameter.

after GET you specify to witch file you will send GET request.
etc... index.php?id=parameter.

HTTP/1.1 means version of protocol.
\r\n means new line.

Host: google.com - means to witch host.

😉

GET is not a parameter. GET is a HTTP request method or a HTTP verb. There are no parameters in this request.

\r\n does not mean newline. It is a carriage return/newline pair.
(Mar 17, 2019 04:47 AM)moonchild Wrote:  Considering you don't even know what \r does and you couldn't even bother googling it yourself, you should really learn the language more before going through this book.


GET is not a parameter. GET is a HTTP request method or a HTTP verb. There are no parameters in this request.

\r\n does not mean newline. It is a carriage return/newline pair.

Sory mate i was write fast.
It's defined by the HTTP protocol.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

SP = space
CRLF = \r\n
(Mar 17, 2019 04:47 AM)moonchild Wrote:  Considering you don't even know what \r does and you couldn't even bother googling it yourself, you should really learn the language more before going through this book.


GET is not a parameter. GET is a HTTP request method or a HTTP verb. There are no parameters in this request.

\r\n does not mean newline. It is a carriage return/newline pair.

I'm sorry, i didn't really get involved in the networking part of Python, i usually used it for problem solving so i thought it'd be a good idea to get into the networking side of python with this book (based on a friend's recommendation who knows my python level), but thanks for your reply, with your reply + looking up (to understand what you meant by carriage return) it made me understand.




(Mar 16, 2019 04:05 PM)Ramb0x Wrote:  client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

GET - means send GET parameter.

after GET you specify to witch file you will send GET request.
etc... index.php?id=parameter.

HTTP/1.1 means version of protocol.
\r\n means new line.

Host: google.com - means to witch host.

😉

(Mar 17, 2019 04:58 AM)Vuln Wrote:  It's defined by the HTTP protocol.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

SP = space
CRLF = \r\n


Thank you guys so much. I've bookmarked that link and will probably get the book just to read some sections to clear my concern about basic stuff.
It’s asking for the root URL (/), I.e the home page from google

This should give u the same page that you would get if u just typed

Google.com

The code before that is making a TcP connection to the server.

So it’s HTTP over TCP where you are using a GET request to get googles homepage
(Mar 17, 2019 10:30 PM)Asdethprime Wrote:  It’s asking for the root URL (/), I.e the home page from google

This should give u the same page that you would get if u just typed

Google.com

The code before that is making a TcP connection to the server.

So it’s HTTP over TCP where you are using a GET request to get googles homepage

Or you could write /index.php :)
get homepage using HTTP 1.1

Followed by CLRF + New Line

host is google

This is followe by 2 x CLRF + New Line to show end of request


(Mar 17, 2019 10:34 PM)Ramb0x Wrote:  Or you could write /index.php :)

If the home page if called index.php that would work.

There is however no requirement for it to be so. In fact no reason ta PHP ; a web server running on Node will have .js extension. You will also get a different extension under other language for example ASP.NET etc.

There is also no rule saying the home page has to be called index ; could be called anything as well.


On btw minor tidbit

You can change the second line to

Client = socket.socket()

Internet socket using TCP is the default and you can ignore that should u like.

Not mandatory obviously
(Mar 17, 2019 10:34 PM)Asdethprime Wrote:  You can change the second line to

Client = socket.socket()

Internet socket using TCP is the default and you can ignore that should u like.

Not mandatory obviously

Or you could try
Code
requests.get(url = URL, params = PARAMS)
hehe
(Mar 17, 2019 10:59 PM)Ramb0x Wrote:  Or you could try
Code
requests.get(url = URL, params = PARAMS)
hehe

That works and in normal world that is what u will do.

But this requires you to load request library. Request library under the hood uses socket library.

In fact every time 2 devices are talking to each other , under the hood all communication is done using socket.

So as a hacker trying to work directly with socket library is seen as a good thing as you now begin to understand how it really works under the hood .

I am not trying to yell at you or anything ; just wanted to clarify if that makes sense.


And yes your code works
(Mar 17, 2019 11:36 PM)Asdethprime Wrote:  That works and in normal world that is what u will do.

But this requires you to load request library. Request library under the hood uses socket library.

In fact every time 2 devices are talking to each other , under the hood all communication is done using socket.

So as a hacker trying to work directly with socket library is seen as a good thing as you now begin to understand how it really works under the hood .

I am not trying to yell at you or anything ; just wanted to clarify if that makes sense.


And yes your code works
Tnx for explanation +rep.
(Mar 17, 2019 11:56 PM)Ramb0x Wrote:  Tnx for explanation +rep.

Cheers man
(Mar 17, 2019 10:34 PM)Asdethprime Wrote:  If the home page if called index.php that would work.

There is however no requirement for it to be so. In fact no reason ta PHP ; a web server running on Node will have .js extension. You will also get a different extension under other language for example ASP.NET etc.

There is also no rule saying the home page has to be called index ; could be called anything as well.

Let's confuse people even further 😉

A "file" extension in an URL really doesn't mean anything. It doesn't necessarily point to a file.
Nothing is stopping you from making a non-PHP web server that handles a .php "file" URL.

Example: Spoiler (Click to View)


Possibly Related Threads...
Thread / Author Replies Views Last Post
by Bass_ReevesDec 4, 2019 07:11 AM 0 188 Dec 4, 2019 07:11 AM
Last Post: Bass_Reeves
by UrLocalNewbieNov 13, 2016 11:44 AM 11 225 Nov 13, 2016 11:44 AM
Last Post: ndemon
by The MackMay 8, 2016 08:48 PM 6 133 May 8, 2016 08:48 PM
Last Post: Trivexlium