我正在编写一个拦截代理工具,如Burpsuite用于安全测试。其中一个重要部分是发送格式错误的 HTTP 请求,在这种情况下,我们必须让用户完全控制请求!
所以,我在使用库时无法完全控制!我需要能够向目标主机发送原始 HTTP 请求,例如,
GET / HTTP/1.1
Host: google.com
我的尝试:-我尝试使用 Node JS net module ,并且我能够连接到端口 80 (HTTP) 上的主机,并且在连接到端口 443 (HTTPS) 时,建立了连接但返回一个空响应!
在一些研究中,我发现这与 SSL 有关,因为我尝试了 telnet,但它对于 HTTPS 连接也失败了,并查看了一些 stackoverflow 答案!
是否有任何选项可以直接从我的 Node 应用程序直接发送原始 HTTP/HTTPS 请求?
谢谢!
最佳答案
有一个模块http-tag ,它允许编写文字 http 消息,例如 -
const net = require('net')
const HTTPTag = require('http-tag')
const socket = net.createConnection({
host: 'localhost',
port: 8000,
}, () => {
// This callback is run once, when socket connected
// Instead of manually writing like this:
// socket.write('GET / HTTP/1.1\r\n')
// socket.write('My-Custom-Header: Header1\r\n\r\n')
// You will be able to write your request(or response) like this:
const xHeader = 'Header1' // here in the epressions you can pass any characters you want
socket.write(
HTTPTag`
GET / HTTP/1.1
My-Custom-Header: ${xHeader}
`
)
socket.end()
})
socket.on('close', hasError => console.log(`Socket Closed, hasError: ${hasError}`))
// set readable stream encoding
socket.setEncoding('utf-8')
socket.on('data', data => console.log(data))
关于TLS,目前正在研究内置 Node 模块,还没有查看tls。
关于node.js - 在 Node JS 中发送低级原始 HTTP/HTTPS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66842858/