Skip to content
Snippets Groups Projects
Commit f5a6b9a5 authored by Rob Adams's avatar Rob Adams
Browse files

Don't include ethernet padding when parsing ipv4 packets by computing the length correctly

parent 6179fc66
No related branches found
No related tags found
No related merge requests found
......@@ -60,7 +60,7 @@ public class Data extends BasePacket {
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
this.data = Arrays.copyOfRange(data, offset, data.length);
this.data = Arrays.copyOfRange(data, offset, offset + length);
return this;
}
......
......@@ -371,10 +371,14 @@ public class IPv4 extends BasePacket {
} else {
payload = new Data();
}
this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
int payloadLength = this.totalLength - this.headerLength * 4;
int remLength = bb.limit()-bb.position();
if (remLength < payloadLength)
payloadLength = bb.limit()-bb.position();
this.payload = payload.deserialize(data, bb.position(), payloadLength);
this.payload.setParent(this);
if (this.totalLength != length)
if (this.totalLength > length)
this.isTruncated = true;
else
this.isTruncated = false;
......
......@@ -283,7 +283,8 @@ public class TCP extends BasePacket {
}
this.payload = new Data();
this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position());
int remLength = bb.limit()-bb.position();
this.payload = payload.deserialize(data, bb.position(), remLength);
this.payload.setParent(this);
return this;
}
......
......@@ -69,19 +69,43 @@ public class IPv4Test {
assertTrue(Arrays.equals(expected, actual));
}
// A real TLSv1 packet
byte[] pktSerialized =
new byte[] { 0x45, 0x00,
0x00, 0x2e, 0x41, (byte) 0xbe, 0x40, 0x00, 0x40, 0x06,
(byte) 0xd4, (byte) 0xf0, (byte) 0xc0, (byte) 0xa8,
0x02, (byte) 0xdb, (byte) 0xd0, 0x55,
(byte) 0x90, 0x42, (byte) 0xd5, 0x48, 0x01, (byte)
0xbb, (byte) 0xe3, 0x50,
(byte) 0xb2, 0x2f, (byte) 0xfc, (byte) 0xf8,
(byte) 0xa8, 0x2c, 0x50, 0x18,
(byte) 0xff, (byte) 0xff, 0x24, 0x3c, 0x00,
0x00, 0x14, 0x03,
0x01, 0x00, 0x01, 0x01
};
@Test
public void testDeserialize() {
// A real TLSv1 packet
byte[] pktSerialized = new byte[] { 0x45, 0x00,
IPv4 packet = new IPv4();
packet.deserialize(pktSerialized, 0, pktSerialized.length);
byte[] pktSerialized1 = packet.serialize();
assertTrue(Arrays.equals(pktSerialized, pktSerialized1));
}
@Test
public void testDeserializePadded() {
// A real TLSv1 packet with crap added to the end
byte[] pktSerializedPadded = new byte[] { 0x45, 0x00,
0x00, 0x2e, 0x41, (byte) 0xbe, 0x40, 0x00, 0x40, 0x06,
(byte) 0xd4, (byte) 0xf0, (byte) 0xc0, (byte) 0xa8, 0x02, (byte) 0xdb, (byte) 0xd0, 0x55,
(byte) 0x90, 0x42, (byte) 0xd5, 0x48, 0x01, (byte) 0xbb, (byte) 0xe3, 0x50,
(byte) 0xb2, 0x2f, (byte) 0xfc, (byte) 0xf8, (byte) 0xa8, 0x2c, 0x50, 0x18,
(byte) 0xff, (byte) 0xff, 0x24, 0x3c, 0x00, 0x00, 0x14, 0x03,
0x01, 0x00, 0x01, 0x01
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
};
IPv4 packet = new IPv4();
packet.deserialize(pktSerialized, 0, pktSerialized.length);
packet.deserialize(pktSerializedPadded, 0, pktSerializedPadded.length);
byte[] pktSerialized1 = packet.serialize();
assertTrue(Arrays.equals(pktSerialized, pktSerialized1));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment