When I receive an IP Packet with my Android VpnService I read the headers first (as here), then I try to print the received data as follows:
int lengthRemaining = packet.remaining();
if (lengthRemaining > 0) {
byte[] data = new byte[lengthRemaining];
packet.get(data, packet.arrayOffset(), lengthRemaining);
Log.d(TAG, "Packet-Data: " + new String(data, Charset.forName("UTF-8")));
}
This results in out put like the following:
Packet-Data: ����5��(��������������������www�google�com������
Or another example from unencrypted webpage:
Packet: IP Version=4, Header-Length=20, Total-Length=60, Destination-IP=xx.xx.xxx.xx, Hostname=yyyyy.zzz, Source-IP=10.1.10.1, Protocol=17, Data-Remaining=40
Packet-Data: N���5��(@�F�����������������m�postimees�ee������
I've tried with a couple of different String encoding types, I don't think it is the issue.
What step am I missing?
I've solved this myself. The answers I needed were on this article about handwriting DNS packets and the UDP protocol in addition to the UDP headers as discussed here.
The problem was that the data as parsed above still contained UDP and DNS headers, we had only parsed the application layer headers
The following code will read DNS Domain from a DNS packet
And produce output something like this: