【问题标题】:How to make an HTTP GET request manually with netcat?如何使用 netcat 手动发出 HTTP GET 请求?
【发布时间】:2015-11-27 06:35:50
【问题描述】:

所以,我必须从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 获取请求的好教程,所以我将其发布在这里。

【问题讨论】:

  • 这不是一个正确的答案,但我推荐curlwget 处理这类事情。

标签: http get netcat


【解决方案1】:

当然你可以在谷歌搜索的标准中挖掘,但实际上如果你只想得到一个 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: 标头,如您在我的示例中所见。这是因为多个网站可以运行在同一个网络服务器上,所以浏览器需要告诉他,它想从哪个网站加载页面。

【讨论】:

  • 谢谢。这有很大帮助。现在结果已经从 Bad Request 变成了禁止。服务器不允许我访问该文件。有没有办法解决这个问题? HTTP/1.1 403 禁止日期:2015 年 9 月 1 日星期二 22:14:50 GMT 服务器:Apache 连接:关闭内容类型:text/html; charset=iso-8859-1 403 ForbiddenForbidden 您没有权限访问此服务器上的/wx/in/kanpur/wx.php。


    Apache/1.3.41 服务器位于 www.rssweather.com端口 80
  • @AvinashBhawnani 谢谢。您始终可以编辑您的问题,而长复制粘贴在 cmets 中看起来不太好,在类似情况下,最好将它们编辑到您的问题中。而且,如果您还有其他问题,可以在新问题中提出,这不是问题(仅当您将同一问题问两次时)。
【解决方案2】:

这对我有用:

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

然后点击两次&lt;enter&gt;,即一次用于远程http服务器,一次用于nc命令。

来源:pentesterlabs

【讨论】:

  • 您应该使用HTTP/1.0,或者也提供Connection: close 标头。
  • 如果您收到“需要 HTTP/1.1 426 升级”,请尝试将 HTTP/1.0 更改为 HTTP/1.1
  • 这对我有用。按双 就可以了。
【解决方案3】:

你甚至不需要使用/安装 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
    

【讨论】:

    【解决方案4】:

    在 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”。

    【讨论】:

    • 这是 linux 的大写“-C”作为添加到此答案的内容,没有它,上述建议将不起作用。
    【解决方案5】:

    使用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
    

    Host: 标头在 HTTP 1.1 中是必需的。

    这显示了目录的 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/

    httpsSSL

    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 上测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多