英文:
Java Socket Packet Interception
问题
以下是您提供的内容的翻译部分:
我正在尝试使用Java编写一个拦截数据/数据包的套接字程序。
我已经成功地在Python中编写了这个程序:
import socket
def createListener(port):
srvSock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
srvSock.bind(('localhost', port))
srvSock.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
raw_data, addr = srvSock.recvfrom(65536)
print('data: ', raw_data)
createListener(80)
这是我基本的Java套接字程序:
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(80);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = (String) dis.readUTF();
System.out.println("data: " + str);
ss.close();
} catch (IOException i) {
System.out.println(i);
}
}
然而,当运行时,它不能像Python程序那样拦截通过端口传输的所有数据。特别是Python脚本中的这一行srvSock.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
启用了套接字监听该端口,并捕获/拦截通过它的全部数据。
我无法找到Java中的替代语法或任何解决我的问题的解决方案。我会很感激关于如何使用套接字拦截网络端口上的数据包的任何帮助。
谢谢。
英文:
I'm trying to write a socket program in Java that intercepts data/packets.
I've successfully written this in python:
import socket
def createListener(port):
srvSock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
srvSock.bind(('localhost', port))
srvSock.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
raw_data, addr = srvSock.recvfrom(65536)
print('data: ' , raw_data)
createListener(80)
This is my basic Java socket program
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(80);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = (String)dis.readUTF();
System.out.println("data: "+str);
ss.close();
} catch(IOException i){
System.out.println(i);
}
}
However, when run, it doesn't intercept all data moving through the port on the network like the python program does. Specifically this line srvSock.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
in the Python script enables the socket to listen to the port and capture/intercept the entirety of the data going through it.
I cannot find a Java alternative to this syntax or any solution to my problem at all for that matter.
I would appreciate any help in how to use sockets to intercept packets on a network port.
Thanks
答案1
得分: 1
我相当确定你试图使用Java无法实现的方法。看起来你在尝试使用“混杂模式”,但是Java套接字无法在混杂模式下启动。Java套接字是端到端的实现:它们无法监听网络端口以获取所有流量。当然,网络端口必须处于混杂模式,但我认为Java不是合适的选择。
我能想到的唯一可能达到目标的方法可能是在类似JNI的东西中进行本地调用,但我甚至不知道从何开始。
这里有一个我找到的非常旧的帖子,与此有点相关:https://stackoverflow.com/questions/10408505/java-socket-and-web-programming
英文:
Im fairly certain what you are trying to do cannot be done with Java. It looks like you are trying to use "promiscuous mode", but Java sockets cannot be started in promiscuous mode. Java sockets are an end-to-end implementation: they can't listen on the network port for all traffic. For sure the network port would have to be in promiscuous mode, but I don't think Java is the right choice for you.
The only thing I can think of that might get you there would be doing a native call in something like JNI, but I wouldn't even really know where to start with that.
Here is a really old post that I found that is kind of related: https://stackoverflow.com/questions/10408505/java-socket-and-web-programing
答案2
得分: 0
看起来你正在尝试将传入的字节数组读取为字符串行。如果是这样的话,以下是我在Kotlin中读取行而不漏掉任何一行的方法:
socket.getInputStream().bufferedReader(Charsets.UTF_8).forEachLine {
it -> { /* 根据你的需求处理输入内容 */ }
}
在Java中,这个过程要少一些抽象:
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8), 8 * 1024);
然后,使用这个缓冲读取器中的行作为一个行序列来读取你的传入行。
英文:
From the looks of it, you're trying to read incoming bytearrays as string lines.
If that is so, this is what I do to read lines without missing a single line (In Kotlin):
socket.getInputStream().bufferedReader(Charsets.UTF_8).forEachLine {
it -> { /* Do what you wanna do with the input */ }
}
In Java, it's much less abstract :
BufferedReader(InputStreamReader(socket.getInputStream(), Charsets.UTF_8), 8 * 1024)
Then, use lines from this buffered reader as a line sequence to read your incoming lines.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论