rest - 在Golang中使HTTP请求非法路径

标签 rest api http go networking

我有一个问题,出于安全目的,有时使用非法路径发出HTTP请求非常有用,例如:http://localhost/%x,在Golang中似乎不是这样做的方法,有人可以给这个问题一些启发吗?
到目前为止我尝试过的是:
-.get()函数

response, err := http.DefaultClient.Get("http://localhost/%x")
if err != nil{
    panic(err) // net/url.EscapeError
}
  • .Do()函数

  •     requestObject, err := http.NewRequest("GET", "http://localhost/%x", nil)
        if err != nil {
            panic(err) // panics!
        }
    



        requestObject, err := http.NewRequest("GET", "http://localhost", nil)
        if err != nil {
            panic(err) // Not panic
        }
        requestObject.URL.Path = "/%x"
        response, err = netClient.goHttpClient.Do(requestObject)
        if err != nil {
            panic(err) // Not panic
        }
    

    这最后一个看起来很有希望,但是是对%25x而不是%x的请求,有人可以帮忙吗?

    更新:
    Go可以在请求中发送%x,但是在查询部分,而不是路径中,我想在路径中发送%x,所以我的问题没有解决:

    requestObject, err := http.NewRequest("GET", "http://localhost", nil)
        if err != nil {
            panic(err) // Not panic
        }
    requestObject.URL.Path = "/%x"
    requestObject.URL.RawQuery = "%x"
    

    http://localhost/%25x?%x发出请求对路径中的%x进行编码,但在查询中未进行编码。

    更新:

    @Cerise给出的答案是我使用的答案,它可以正常工作,但要考虑一些极端情况。
    1-代理:由于某种原因,如果您必须将请求转发到代理,则HTTP框架不知道如何发送代理可以理解的HTTP请求,要解决此问题,在Burp套件中,您必须启用不可见代理如此处记录:https://portswigger.net/support/using-burp-suites-invisible-proxy-settings-to-test-a-non-proxy-aware-thick-client-application
    这仍然是一个问题,当我不得不调试一些HTTP请求时,我注意到了,我无法理解Burp Suite告诉我的内容,然后在Wireshark中检查请求,如果使用.Opaque,我知道出了点问题+ BurpSuite,BurpSuite将修改一些请求,这是第二期的内容,请继续阅读。
    2-Opaque不能很好地处理以“//”开头的路径,尽管我也不认为这是一个错误,众所周知https://stackoverflow.com也可以重写为//stackoverflow.com,我认为这是采用的逻辑如果您这样做,还是可以考虑:

    requestObject, err := http.NewRequest(methodStr, "http://localhost", nil)
    requestObject.URL.Opaque = "//something"
    

    这是Wireshark的HTTP请求:
    GET http://something HTTP/1.1
    Host: localhost
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Language: en-US,en;q=0.9,ca;q=0.8
    Cache-Control: no-cache
    Connection: close
    Pragma: no-cache
    Sec-Fetch-Dest: document
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    Accept-Encoding: gzip
    

    注意第一行,尽管不是第一行,对吧?

    我曾经使用Burp Suite调试HTTP请求,但让我们使用相同的代码,然后使用以下命令将请求转发给Burp:

    http.DefaultTransport.(*http.Transport).Proxy = http.ProxyURL("http://localhost:8080")
    

    不要忘记在BurpSuite上启用隐形代理,否则它将拒绝该请求。 Burp Suite显示的内容是:
    GET / HTTP/1.1
    Host: localhost
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Language: en-US,en;q=0.9,ca;q=0.8
    Cache-Control: no-cache
    Connection: close
    Pragma: no-cache
    Sec-Fetch-Dest: document
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    Accept-Encoding: gzip, deflate
    
    

    !!,Burp完全改变了我们的要求!!!
    外卖:
    -不要使用您的HTTP代理来调试用Opaque发出的请求
    -不要将.Opaque与以双斜杠开头的路径一起使用,请使用以下命令:

           if strings.HasPrefix(yourPath, "//") {
                requestObject.URL.Path = fmt.Sprintf("%s", yourPath)
            } else {
                requestObject.URL.Opaque = fmt.Sprintf("%s", yourPath)
            }
    

    最佳答案

    将Request.URL.Opaque设置为无效路径。 Opaque数据按原样写入网络。

    requestObject, err := http.NewRequest("GET", "http://localhost", nil)
    if err != nil {
        panic(err) 
    }
    requestObject.URL.Opaque = "/%x"
    response, err = netClient.goHttpClient.Do(requestObject)
    if err != nil {
        panic(err)
    }
    

    此代码创建无效的请求。 net/http服务器通过响应400 Bad Request处理无效请求。其他服务器和代理可能会执行不同的操作。

    Run it on the playground

    关于rest - 在Golang中使HTTP请求非法路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61332752/

    相关文章:

    REST 资源路径设计

    rest - 在 ASP.Net Web API 中,如何伪造 PUT 和 DELETE?

    java - 带有特殊字符的 HttpResponse

    api - 在全局速卖通上获取产品的推荐(合作伙伴关系)链接?

    javascript - Chrome 扩展中的 API 调用需要按两次按钮

    python - 流式传输解压存档

    c# - Django与C#桌面应用程序的接口(interface)

    rest - 如何通过将 blob 拆分为 block 并使用 REST 和 PHP 调用 PutBlockList 将其上传到 windows azure

    C malloc 增加缓冲区大小

    android - 错误:无法启动守护进程:无法为对象堆保留足够的空间