如何使用netcat手动发出HTTP GET请求?

问题描述 投票:0回答:6

因此,我必须从 http://www.rssweather.com/dir/Asia/India 检索任意一个城市的温度。

假设我想取回 Kanpur 的。

如何使用 Netcat 发出 HTTP GET 请求?

我正在做这样的事情。

nc -v rssweather.com 80
GET http://www.rssweather.com/wx/in/kanpur/wx.php HTTP/1.1

我什至不知道我的方向是否正确。我找不到任何关于如何使用 netcat 发出 HTTP get 请求的好教程,所以我将其发布在这里。

http get netcat
6个回答
53
投票

当然你可以深入研究谷歌搜索的标准,但实际上如果你只想得到一个URL,这是不值得的。

您还可以在端口上以侦听模式启动 netcat:

nc -l 64738

(有时

nc -l -p 64738
是正确的参数列表)

...然后使用真正的浏览器向此端口发出浏览器请求。只需在浏览器中输入

http://localhost:64738
即可查看。

在您的实际情况中,问题是 HTTP/1.1 不会自动关闭连接,而是等待您要检索的下一个 URL。解决办法很简单:

使用HTTP/1.0:

GET /this/url/you/want/to/get HTTP/1.0
Host: www.rssweather.com
<empty line>

或使用

Connection:
请求标头来表示您要在此之后关闭的服务器:

GET /this/url/you/want/to/get HTTP/1.1
Host: www.rssweather.com
Connection: close
<empty line>

扩展: 在 GET 标头之后仅写入请求的路径部分。您想要从中获取数据的主机名属于

Host:
标头,正如您在我的示例中看到的那样。这是因为多个网站可以在同一个网络服务器上运行,所以浏览器需要说他,它想要从哪个网站加载页面。


32
投票

您甚至不需要使用/安装 netcat

  • 通过未使用的文件描述符创建 TCP 套接字,即我在这里使用 88
  • 将请求写入其中
  • 使用fd

    exec 88<>/dev/tcp/rssweather.com/80
    echo -e "GET /dir/Asia/India HTTP/1.1\nhost: www.rssweather.com\nConnection: close\n\n" >&88
    sed 's/<[^>]*>/ /g' <&88
    

31
投票

这对我有用:

$ nc www.rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.0
Host: www.rssweather.com

然后点击两次

<enter>
,即一次用于远程 http 服务器,一次用于
nc
命令。

来源:pentesterlabs


17
投票

在 MacOS 上,您需要 -c 标志,如下所示:

Little-Net:~ minfrin$ nc -c rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.1
Host: rssweather.com
Connection: close
[empty line]

响应如下所示:

HTTP/1.1 200 OK
Date: Thu, 23 Aug 2018 13:20:49 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

-c 标志被描述为“发送 CRLF 作为行尾”。

为了兼容 HTTP/1.1,您需要 Host 标头,如果您想禁用 keepalive,还需要“Connection: close”。


12
投票

使用

python3 http.server

在本地进行测试

这也是一种有趣的测试方式。在一个 shell 上启动本地文件服务器:

python3 -m http.server 8000

然后在第二个shell上,提出请求:

printf 'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n' | nc localhost 8000

HTTP 1.1 中需要

Host:
标头。

这显示了目录的 HTML 列表,就像您在以下位置看到的那样:

firefox http://localhost:8000

接下来您可以尝试列出文件和目录并观察响应:

printf 'GET /my-subdir/ HTTP/1.1\n\n' | nc localhost 8000
printf 'GET /my-file HTTP/1.1\n\n' | nc localhost 8000

每次请求成功,服务器都会打印:

127.0.0.1 - - [05/Oct/2018 11:20:55] "GET / HTTP/1.1" 200 -

确认已收到。

example.com

这个 IANA 维护的域名是另一个很好的测试 URL:

printf 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80

并与:http://example.com/

进行比较

https
SSL

nc
似乎无法处理
https
URL。相反,您可以使用:

sudo apt-get install nmap
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | ncat --ssl github.com 443

另请参阅:https://serverfault.com/questions/102032/connecting-to-https-with-netcat-nc/650189#650189

如果你尝试

nc
,它就会挂起:

printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc github.com 443

并尝试端口

80
:

printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc github.com 443

仅给出对

https
版本的重定向响应:

HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://github.com/
Connection: keep-alive

在 Ubuntu 18.04 上测试。


0
投票

使用nc连接后,只需指定路径即可,无需再次指定HOST名。

© www.soinside.com 2019 - 2024. All rights reserved.