Работа над разбором TLS пакетов

This commit is contained in:
serega6531
2020-04-18 16:10:34 +03:00
parent 195fbd1e06
commit 1e9d327af0
5 changed files with 60 additions and 10 deletions

View File

@@ -3,9 +3,11 @@ package ru.serega6531.packmate.service.optimization.tls;
import org.pcap4j.packet.AbstractPacket; import org.pcap4j.packet.AbstractPacket;
import org.pcap4j.packet.IllegalRawDataException; import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.packet.Packet; import org.pcap4j.packet.Packet;
import org.pcap4j.packet.factory.PacketFactories;
import org.pcap4j.packet.namednumber.TcpPort;
import org.pcap4j.util.ByteArrays; import org.pcap4j.util.ByteArrays;
import ru.serega6531.packmate.service.optimization.tls.numbers.ContentType;
import ru.serega6531.packmate.service.optimization.tls.numbers.TlsVersion;
import ru.serega6531.packmate.service.optimization.tls.records.ChangeCipherSpecRecord;
import ru.serega6531.packmate.service.optimization.tls.records.TlsRecord;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -28,9 +30,7 @@ public class TlsPacket extends AbstractPacket {
int payloadLength = length - header.length(); int payloadLength = length - header.length();
if (payloadLength > 0) { if (payloadLength > 0) {
this.payload = this.payload = TlsPacket.newPacket(rawData, offset + header.length(), payloadLength);
PacketFactories.getFactory(Packet.class, TcpPort.class)
.newInstance(rawData, offset + header.length(), payloadLength);
} else { } else {
this.payload = null; this.payload = null;
} }
@@ -60,10 +60,12 @@ public class TlsPacket extends AbstractPacket {
private static final int CONTENT_TYPE_OFFSET = 0; private static final int CONTENT_TYPE_OFFSET = 0;
private static final int VERSION_OFFSET = CONTENT_TYPE_OFFSET + BYTE_SIZE_IN_BYTES; private static final int VERSION_OFFSET = CONTENT_TYPE_OFFSET + BYTE_SIZE_IN_BYTES;
private static final int LENGTH_OFFSET = VERSION_OFFSET + SHORT_SIZE_IN_BYTES; private static final int LENGTH_OFFSET = VERSION_OFFSET + SHORT_SIZE_IN_BYTES;
private static final int RECORD_OFFSET = LENGTH_OFFSET + SHORT_SIZE_IN_BYTES;
private ContentType contentType; private ContentType contentType;
private TlsVersion version; private TlsVersion version;
private short length; private short length;
private TlsRecord record;
private TlsHeader(Builder builder) { private TlsHeader(Builder builder) {
//TODO //TODO
@@ -73,6 +75,18 @@ public class TlsPacket extends AbstractPacket {
this.contentType = ContentType.getInstance(ByteArrays.getByte(rawData, CONTENT_TYPE_OFFSET + offset)); this.contentType = ContentType.getInstance(ByteArrays.getByte(rawData, CONTENT_TYPE_OFFSET + offset));
this.version = TlsVersion.getInstance(ByteArrays.getShort(rawData, VERSION_OFFSET + offset)); this.version = TlsVersion.getInstance(ByteArrays.getShort(rawData, VERSION_OFFSET + offset));
this.length = ByteArrays.getShort(rawData, LENGTH_OFFSET + offset); this.length = ByteArrays.getShort(rawData, LENGTH_OFFSET + offset);
if (contentType == ContentType.HANDSHAKE) {
} else if (contentType == ContentType.CHANGE_CIPHER_SPEC) {
this.record = ChangeCipherSpecRecord.newInstance(rawData, offset + RECORD_OFFSET, length);
} else if (contentType == ContentType.APPLICATION_DATA) {
} else if (contentType == ContentType.ALERT) {
} else {
throw new IllegalArgumentException("Unknown content type: " + contentType);
}
} }
@Override @Override
@@ -83,13 +97,19 @@ public class TlsPacket extends AbstractPacket {
rawFields.add(ByteArrays.toByteArray(length)); rawFields.add(ByteArrays.toByteArray(length));
return rawFields; return rawFields;
} }
@Override
public int length() {
return RECORD_OFFSET + length;
}
} }
public static final class Builder extends AbstractBuilder { public static final class Builder extends AbstractBuilder {
private Packet.Builder payloadBuilder; private Packet.Builder payloadBuilder;
public Builder() {} public Builder() {
}
public Builder(TlsPacket packet) { public Builder(TlsPacket packet) {
this.payloadBuilder = packet.payload != null ? packet.payload.getBuilder() : null; this.payloadBuilder = packet.payload != null ? packet.payload.getBuilder() : null;

View File

@@ -1,4 +1,4 @@
package ru.serega6531.packmate.service.optimization.tls; package ru.serega6531.packmate.service.optimization.tls.numbers;
import org.pcap4j.packet.namednumber.NamedNumber; import org.pcap4j.packet.namednumber.NamedNumber;
@@ -7,12 +7,16 @@ import java.util.Map;
public class ContentType extends NamedNumber<Byte, ContentType> { public class ContentType extends NamedNumber<Byte, ContentType> {
public static final ContentType CHANGE_CIPHER_SPEC = new ContentType((byte) 20, "Change Cipher Spec");
public static final ContentType ALERT = new ContentType((byte) 21, "Alert");
public static final ContentType HANDSHAKE = new ContentType((byte) 22, "Handshake"); public static final ContentType HANDSHAKE = new ContentType((byte) 22, "Handshake");
public static final ContentType APPLICATION_DATA = new ContentType((byte) 23, "Application Data"); public static final ContentType APPLICATION_DATA = new ContentType((byte) 23, "Application Data");
private static final Map<Byte, ContentType> registry = new HashMap<>(); private static final Map<Byte, ContentType> registry = new HashMap<>();
static { static {
registry.put(CHANGE_CIPHER_SPEC.value(), CHANGE_CIPHER_SPEC);
registry.put(ALERT.value(), ALERT);
registry.put(HANDSHAKE.value(), HANDSHAKE); registry.put(HANDSHAKE.value(), HANDSHAKE);
registry.put(APPLICATION_DATA.value(), APPLICATION_DATA); registry.put(APPLICATION_DATA.value(), APPLICATION_DATA);
} }
@@ -25,7 +29,7 @@ public class ContentType extends NamedNumber<Byte, ContentType> {
if (registry.containsKey(value)) { if (registry.containsKey(value)) {
return registry.get(value); return registry.get(value);
} else { } else {
return new ContentType(value, "unknown"); throw new IllegalArgumentException("Unknown record type " + value);
} }
} }

View File

@@ -1,4 +1,4 @@
package ru.serega6531.packmate.service.optimization.tls; package ru.serega6531.packmate.service.optimization.tls.numbers;
import org.pcap4j.packet.namednumber.NamedNumber; import org.pcap4j.packet.namednumber.NamedNumber;

View File

@@ -0,0 +1,19 @@
package ru.serega6531.packmate.service.optimization.tls.records;
import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.util.ByteArrays;
public class ChangeCipherSpecRecord extends TlsRecord {
private byte changeCipherSpecMessage;
public static ChangeCipherSpecRecord newInstance(byte[] rawData, int offset, int length) {
ByteArrays.validateBounds(rawData, offset, length);
return new ChangeCipherSpecRecord(rawData, offset);
}
private ChangeCipherSpecRecord(byte[] rawData, int offset) {
this.changeCipherSpecMessage = ByteArrays.getByte(rawData, offset);
}
}

View File

@@ -0,0 +1,7 @@
package ru.serega6531.packmate.service.optimization.tls.records;
import java.io.Serializable;
public class TlsRecord implements Serializable {
}