How to make an HTTP GET request manually with netcat?

时间:2015-09-01 21:56:18

标签: http get netcat

So, I have to retrieve temperature from any one of the cities from http://www.rssweather.com/dir/Asia/India.

Let's assume I want to retrieve of Kanpur's.

How to make an HTTP GET request with Netcat?

I'm doing something like this.

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

I don't know exactly if I'm even in the right direction or not. I am not able to find any good tutorials on how to make an HTTP get request with netcat, so I'm posting it on here.

6 个答案:

答案 0 :(得分:20)

Of course you could dig in standards searched for google, but actually if you want to get only a single URL, it doesn't worth the effort.

You could also start a netcat in listening mode on a port:

nc -l 64738

...and then do a browser request into this port with a real browser. Just type in your browser http://localhost:64738 and see.

In your actual case the problem is that HTTP/1.1 doesn't close the connection automatically, but it waits your next URL you want to retrieve. The solution is simple:

Use HTTP/1.0:

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

or use a Connection: request header to say the server you want to close after that:

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

Extension: After the GET header write only the path part of the request. The hostname from which you want to get data belongs to a Host: header as you can see in my examples. This is because multiple websites can run on the same webserver, so the browsers need to say him, from which server they want to load the page.

答案 1 :(得分:10)

这对我有用:

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

然后点击双<enter>,即一次用于远程http服务器,一次用于nc命令。

来源:pentesterlabs

答案 2 :(得分:4)

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

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

    import java.util.Iterator;
    import java.util.NoSuchElementException;
    import java.util.PriorityQueue;
    
    public class ComplexIterator implements Iterator<Integer>{
    
        private class IndexedArrayValue implements Comparable<IndexedArrayValue> {
            int arrayId;
            int index;
            int value;
    
            public IndexedArrayValue(int arrayId, int index, int value) {
                this.arrayId = arrayId;
                this.index = index;
                this.value = value;
            }
    
            @Override
            public int compareTo(IndexedArrayValue other) {
                return this.value - other.value;
            }
        }
    
        private int[][] lists;
        private PriorityQueue<IndexedArrayValue> minHeap;
    
        public ComplexIterator(int[][] lists) {
            minHeap = new PriorityQueue<IndexedArrayValue>();
            int numOfLists = lists.length;
    
            this.lists = lists;
            for (int i = 0; i < numOfLists; i++) {
                minHeap.add(new IndexedArrayValue(i, 0, lists[i][0]));
            }
        }
    
        @Override
        public boolean hasNext() {
            return !this.minHeap.isEmpty();
        }
    
        @Override
        public Integer next() {
            if (!hasNext())
                throw new NoSuchElementException();
    
            IndexedArrayValue indArrVal = minHeap.poll();
            int arrayId = indArrVal.arrayId;
            int index = indArrVal.index;
            int value = indArrVal.value;
            int nextIndex = index + 1;
    
            if (nextIndex < lists[arrayId].length) {
                minHeap.add(new IndexedArrayValue(arrayId, nextIndex, lists[arrayId][nextIndex]));
            }
    
            return value;
        }
    
        public static void main (String[] args) {
            int[] arr1 = { 1, 2, 3 };
            int[] arr2 = { 1, 4 };
            int[] arr3 = { 2, 5, 7, 8 };
    
            int[][] arrs = new int[][] {arr1, arr2, arr3};
    
            ComplexIterator it = new ComplexIterator(arrs);
            while (it.hasNext()) {
                System.out.print(it.next() + " ");
            }
    
        }
    }
    

答案 3 :(得分:2)

在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,需要主机标头,并且如果要禁用keepalive,则需要“连接:关闭”。

答案 4 :(得分:1)

使用python3 http.server

在本地进行测试

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

python3 -m http.server 8000

然后在第二个外壳上,发出一个请求:

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

答案 5 :(得分:0)

我知道它不是正确的答案,但是我使用nc和ncat进行了测试,目的是为HTTP或https进行Web服务的轻量级调试,并且得出结论,使用curl是获得最有用的输出的最佳选择。 / p>

这是我的Linux bash单行函数:

% http_debug() { bash -c "curl -v -I --insecure ${1} 2>&1 | egrep -v '^> |^< |^{|^}|^* T|^* AL|^  0' "; };

用法:

% http_debug https://duckduckgo.com/

输出:

* Connected to duckduckgo.com (107.20.240.232) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* Server certificate:
*  subject: C=US; ST=Pennsylvania; L=Paoli; O=Duck Duck Go, Inc.; CN=duckduckgo.com
*  start date: Sep 18 00:00:00 2018 GMT
*  expire date: Sep 23 12:00:00 2019 GMT
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x55f9d1e1e900)
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
  0  5418    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Connection #0 to host duckduckgo.com left intact
HTTP/2 200 
server: nginx
date: Wed, 15 May 2019 18:02:26 GMT
content-type: text/html; charset=UTF-8
content-length: 5418
vary: Accept-Encoding
etag: "5cdc4dc7-152a"
strict-transport-security: max-age=31536000
x-frame-options: SAMEORIGIN
content-security-policy: default-src https: blob: data: 'unsafe-inline' 'unsafe-eval'; frame-ancestors 'self'
x-xss-protection: 1;mode=block
x-content-type-options: nosniff
referrer-policy: origin
expect-ct: max-age=0
expires: Wed, 15 May 2019 18:02:25 GMT
cache-control: no-cache
accept-ranges: bytes