From c9c106d26eae359e98d162488a6d667d741a551c Mon Sep 17 00:00:00 2001 From: rtrimana Date: Thu, 26 Apr 2018 10:52:01 -0700 Subject: [PATCH] Managed to pick and parse DNS packets; but, still need to get the detailed information from inside the packet. --- .../java/edu/uci/iotproject/DnsPacket.java | 522 ++++++++++++++++++ .../main/java/edu/uci/iotproject/Main.java | 121 +++- 2 files changed, 620 insertions(+), 23 deletions(-) create mode 100644 Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/DnsPacket.java diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/DnsPacket.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/DnsPacket.java new file mode 100644 index 0000000..f15d0d7 --- /dev/null +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/DnsPacket.java @@ -0,0 +1,522 @@ +// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild +package edu.uci.iotproject; + +import io.kaitai.struct.ByteBufferKaitaiStream; +import io.kaitai.struct.KaitaiStruct; +import io.kaitai.struct.KaitaiStream; +import java.io.IOException; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import java.nio.charset.Charset; + +import java.nio.BufferUnderflowException; + + +/** + * (No support for Auth-Name + Add-Name for simplicity) + */ +public class DnsPacket extends KaitaiStruct { + public static DnsPacket fromFile(String fileName) throws IOException { + return new DnsPacket(new ByteBufferKaitaiStream(fileName)); + } + + public enum ClassType { + IN_CLASS(1), + CS(2), + CH(3), + HS(4); + + private final long id; + ClassType(long id) { this.id = id; } + public long id() { return id; } + private static final Map byId = new HashMap(4); + static { + for (ClassType e : ClassType.values()) + byId.put(e.id(), e); + } + public static ClassType byId(long id) { return byId.get(id); } + } + + public enum TypeType { + A(1), + NS(2), + MD(3), + MF(4), + CNAME(5), + SOE(6), + MB(7), + MG(8), + MR(9), + NULL(10), + WKS(11), + PTR(12), + HINFO(13), + MINFO(14), + MX(15), + TXT(16); + + private final long id; + TypeType(long id) { this.id = id; } + public long id() { return id; } + private static final Map byId = new HashMap(16); + static { + for (TypeType e : TypeType.values()) + byId.put(e.id(), e); + } + public static TypeType byId(long id) { return byId.get(id); } + } + + public DnsPacket(KaitaiStream _io) { + this(_io, null, null); + } + + public DnsPacket(KaitaiStream _io, KaitaiStruct _parent) { + this(_io, _parent, null); + } + + public DnsPacket(KaitaiStream _io, KaitaiStruct _parent, DnsPacket _root) { + super(_io); + this._parent = _parent; + this._root = _root == null ? this : _root; + _read(); + } + private void _read() { + this.transactionId = this._io.readU2be(); + this.flags = new PacketFlags(this._io, this, _root); + this.qdcount = this._io.readU2be(); + this.ancount = this._io.readU2be(); + this.nscount = this._io.readU2be(); + this.arcount = this._io.readU2be(); + queries = new ArrayList((int) (qdcount())); + for (int i = 0; i < qdcount(); i++) { + this.queries.add(new Query(this._io, this, _root)); + } + answers = new ArrayList((int) (ancount())); + for (int i = 0; i < ancount(); i++) { + this.answers.add(new Answer(this._io, this, _root)); + } + } + public static class PointerStruct extends KaitaiStruct { + public static PointerStruct fromFile(String fileName) throws IOException { + return new PointerStruct(new ByteBufferKaitaiStream(fileName)); + } + + public PointerStruct(KaitaiStream _io) { + this(_io, null, null); + } + + public PointerStruct(KaitaiStream _io, DnsPacket.Label _parent) { + this(_io, _parent, null); + } + + public PointerStruct(KaitaiStream _io, DnsPacket.Label _parent, DnsPacket _root) { + super(_io); + this._parent = _parent; + this._root = _root; + _read(); + } + private void _read() { + this.value = this._io.readU1(); + } + private DomainName contents; + public DomainName contents() { + if (this.contents != null) + return this.contents; + KaitaiStream io = _root._io(); + long _pos = io.pos(); + io.seek(value()); + this.contents = new DomainName(io, this, _root); + io.seek(_pos); + return this.contents; + } + private int value; + private DnsPacket _root; + private DnsPacket.Label _parent; + + /** + * Read one byte, then offset to that position, read one domain-name and return + */ + public int value() { return value; } + public DnsPacket _root() { return _root; } + public DnsPacket.Label _parent() { return _parent; } + } + public static class Label extends KaitaiStruct { + public static Label fromFile(String fileName) throws IOException { + return new Label(new ByteBufferKaitaiStream(fileName)); + } + + public Label(KaitaiStream _io) { + this(_io, null, null); + } + + public Label(KaitaiStream _io, DnsPacket.DomainName _parent) { + this(_io, _parent, null); + } + + public Label(KaitaiStream _io, DnsPacket.DomainName _parent, DnsPacket _root) { + super(_io); + this._parent = _parent; + this._root = _root; + _read(); + } + private void _read() { + this.length = this._io.readU1(); + if (isPointer()) { + this.pointer = new PointerStruct(this._io, this, _root); + } + // TODO: This part causes BufferUnderflowException + if (!(isPointer())) { + try { + this.name = new String(this._io.readBytes(length()), Charset.forName("ASCII")); + } catch (BufferUnderflowException ex) { + //ex.printStackTrace(); + this.name = null; + } + } + } + private Boolean isPointer; + public Boolean isPointer() { + if (this.isPointer != null) + return this.isPointer; + boolean _tmp = (boolean) (length() == 192); + this.isPointer = _tmp; + return this.isPointer; + } + private int length; + private PointerStruct pointer; + private String name; + private DnsPacket _root; + private DnsPacket.DomainName _parent; + + /** + * RFC1035 4.1.4: If the first two bits are raised it's a pointer-offset to a previously defined name + */ + public int length() { return length; } + public PointerStruct pointer() { return pointer; } + + /** + * Otherwise its a string the length of the length value + */ + public String name() { return name; } + public DnsPacket _root() { return _root; } + public DnsPacket.DomainName _parent() { return _parent; } + } + public static class Query extends KaitaiStruct { + public static Query fromFile(String fileName) throws IOException { + return new Query(new ByteBufferKaitaiStream(fileName)); + } + + public Query(KaitaiStream _io) { + this(_io, null, null); + } + + public Query(KaitaiStream _io, DnsPacket _parent) { + this(_io, _parent, null); + } + + public Query(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) { + super(_io); + this._parent = _parent; + this._root = _root; + _read(); + } + private void _read() { + this.name = new DomainName(this._io, this, _root); + this.type = DnsPacket.TypeType.byId(this._io.readU2be()); + this.queryClass = DnsPacket.ClassType.byId(this._io.readU2be()); + } + private DomainName name; + private TypeType type; + private ClassType queryClass; + private DnsPacket _root; + private DnsPacket _parent; + public DomainName name() { return name; } + public TypeType type() { return type; } + public ClassType queryClass() { return queryClass; } + public DnsPacket _root() { return _root; } + public DnsPacket _parent() { return _parent; } + } + public static class DomainName extends KaitaiStruct { + public static DomainName fromFile(String fileName) throws IOException { + return new DomainName(new ByteBufferKaitaiStream(fileName)); + } + + public DomainName(KaitaiStream _io) { + this(_io, null, null); + } + + public DomainName(KaitaiStream _io, KaitaiStruct _parent) { + this(_io, _parent, null); + } + + public DomainName(KaitaiStream _io, KaitaiStruct _parent, DnsPacket _root) { + super(_io); + this._parent = _parent; + this._root = _root; + _read(); + } + private void _read() { + this.name = new ArrayList