gpt4 book ai didi

java - 即时重写 tcp 流 : how difficult is it? 如何转储所述流?

转载 作者:IT王子 更新时间:2023-10-29 00:49:58 24 4
gpt4 key购买 nike

我正在尝试编写一个 tcp 流“隧道”(类似于默认情况下 SSH 处理的隧道),但有一个异常(exception),我必须在它流经时重写某些信息。

我确定那里有类似的东西,但我没能找到它。我有三个主要问题:

  • 有没有一种简单的方法来保存 TCP 流以供观察? (即使用 netcat,或 ssh -r/-l/-D,或一起使用一些其他实用程序)
  • 即时重写流有多难?

编辑:被重写的信息只是初始身份验证。

最佳答案

可以从现有(或容易找到的)实用程序中拼凑出带有日志记录的直通隧道。

socat -v -x tcp-l:8080,fork,reuseaddr tcp:localhost:80 2>log

In this example, connecting to http://localhost:8080/ will pass through to http://localhost:80/, and log data transferred to log.

The tool TCPreen is specialized for this exact purpose.

If you have root privileges, there are many analyzers such as tcpdump and tcpflow which can capture packets directly from the network, without having to redirect traffic.

socat can also do some very basic stream modification with the ,cr and ,crnl options, which strip/add/replace \r characters.

In any case, back to the original question… It's been ages since I've written any Java, and this is totally untested, but a tunnel that can modify traffic before retransmitting isn't difficult.

public class ForwardAndChangeCaseThread extends Thread {
private Socket in, out;
public ForwardAndChangeCaseThread(Socket in, Socket out) {
this.in = in; this.out = out;
}
public void run() {
byte[] buf = new byte[4096];
InputStream in = this.in.getInputStream();
OutputStream out = this.out.getOutputStream();
int count;
while ((count = in.read(buf)) > 0) {
for (int i = 0; i < count; i++)
if (buf[i] >= 0x40) buf[i] ^= 0x20;
out.write(buf, 0, count);
}
}
}
public class TcpForwarder {
public static void main(String[] args) {
ServerSocket listen = new ServerSocket(8080, 1);
for (;;) {
Socket local = listen.accept();
Socket remote = new Socket("localhost", 80);
new ForwardAndChangeCaseThread(local, remote).start();
new ForwardAndChangeCaseThread(remote, local).start();
}
}
}

关于java - 即时重写 tcp 流 : how difficult is it? 如何转储所述流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2166399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com