简体   繁体   English

可以使用VpnService实现来捕获和发送数据包吗?

[英]Possible to use VpnService implementation to capture and send packets?

I'm considering the possibility of using the new android (4.0) VpnService interface to implement simple packet capture and analysis. 我正在考虑使用新的android(4.0)VpnService接口来实现简单的数据包捕获和分析的可能性。 Does anyone know if it's possible to take the packets you receive in a VpnService implementation and simply write them out to the active/default network device? 有谁知道是否可以在VpnService实现中获取您收到的数据包并将其写入活动/默认网络设备? To receive data, I'd have to be able to read from the network device as well, of course. 当然,要接收数据,我必须能够从网络设备中读取数据。 If it is possible, what APIs can be used to write to the network device(s)? 如果可能,可以使用哪些API写入网络设备?

tPacketCapture creates a second socket to the remote machine to forward the packets. tPacketCapture为远程计算机创建第二个套接字以转发数据包。 I have looked at tPacketCapture using adb shell netstat : 我使用adb shell netstat查看了tPacketCapture

Proto Recv-Q Send-Q Local Address              Foreign Address            State 
tcp        0      0 192.168.1.126:49828        97.74.42.79:80             ESTABLISHED
tcp6       0      0 ::ffff:127.0.0.1:5000      :::*                       LISTEN
tcp6       0    522 ::ffff:10.8.0.1:50294      ::ffff:97.74.42.79:80      ESTABLISHED
tcp6       0      0 ::ffff:192.168.1.126:34210 ::ffff:74.125.141.188:5228 ESTABLISHED
tcp6       0      1 ::ffff:192.168.1.126:43379 ::ffff:74.125.224.174:80   CLOSE_WAIT
tcp6       0      1 ::ffff:192.168.1.126:60217 ::ffff:74.125.239.14:443   CLOSE_WAIT

Note 97.74.42.79:80 twice. 注意97.74.42.79:80两次。

Guess I'll have to do the same unless someone has a better idea. 猜猜我必须做同样的事情,除非有人有更好的主意。

I have worked with the VPN API. 我使用过VPN API。 You have a tun device where you can specify the routes. 您有一个tun设备,您可以在其中指定路由。 But after having read you need to take care of the packets yourself. 但阅读完之后,你需要自己处理这些数据包。 Usually that means handing them over to the VPN Server. 通常这意味着将它们交给VPN服务器。 The Android SDK provides a simple ToyVPNServer Example. Android SDK提供了一个简单的ToyVPNServer示例。

But a simple pcap like interface is difficult (if even possible) to implement without implementing VPN too. 但是,如果没有实现VPN,很难实现简单的pcap接口(如果可能的话)。

You could parse the TCP/UDP header create an own socket with the same src/dest port/ip, use protect() on this socket so it not routed over tun0. 可以解析TCP / UDP标头创建一个具有相同src / dest端口/ IP的自己的套接字,在此套接字上使用protect(),以便它不通过tun0路由。 Since tPacketCapture only supports udp/tcp and not icmp they might use this approach. 由于tPacketCapture仅支持udp / tcp而不支持icmp,因此他们可能会使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM