Commit ff603078 authored by Brickner_cp's avatar Brickner_cp

Packet validation for TCP and ARP.

Constructor check for IPv4 Basic Security Option.
Code Coverage 94.34%.
parent d9ff3069
...@@ -71,6 +71,8 @@ namespace PcapDotNet.Packets.Test ...@@ -71,6 +71,8 @@ namespace PcapDotNet.Packets.Test
arpSenderHardwareAddress, arpSenderProtocolAddress, arpSenderHardwareAddress, arpSenderProtocolAddress,
arpTargetHardwareAddress, arpTargetProtocolAddress); arpTargetHardwareAddress, arpTargetProtocolAddress);
Assert.IsTrue(packet.IsValid, "IsValid");
// Ethernet // Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength"); Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength");
......
...@@ -168,8 +168,9 @@ namespace PcapDotNet.Packets.Test ...@@ -168,8 +168,9 @@ namespace PcapDotNet.Packets.Test
Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString()); Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString());
for (int optionIndex = 0; optionIndex != ipV4Options.Count; ++optionIndex) for (int optionIndex = 0; optionIndex != ipV4Options.Count; ++optionIndex)
{ {
Assert.AreEqual(ipV4Options[optionIndex], packet.Ethernet.IpV4.Options[optionIndex]); IpV4Option option = ipV4Options[optionIndex];
Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options[optionIndex]); Assert.AreEqual(option, packet.Ethernet.IpV4.Options[optionIndex]);
Assert.IsFalse(option.Equals(null));
} }
if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp) if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
...@@ -203,6 +204,49 @@ namespace PcapDotNet.Packets.Test ...@@ -203,6 +204,49 @@ namespace PcapDotNet.Packets.Test
Assert.Fail(); Assert.Fail();
} }
[TestMethod]
public void IpV4OptionTimestampFactoryCreateInstanceErrorTest()
{
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionTimestampOnly(0, 0)),
Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
// Bad Length
byte[] buffer = packet.Buffer;
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 2;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 4;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 5;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 4;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
// Bad Pointer
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 1;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 5;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 6;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
}
[TestMethod] [TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IpV4OptionRoutePointedAddressIndexErrorTest() public void IpV4OptionRoutePointedAddressIndexErrorTest()
...@@ -213,6 +257,49 @@ namespace PcapDotNet.Packets.Test ...@@ -213,6 +257,49 @@ namespace PcapDotNet.Packets.Test
Assert.Fail(); Assert.Fail();
} }
[TestMethod]
public void IpV4OptionRouteTryReadErrorTest()
{
// Small Length
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionLooseSourceRouting()),
Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
byte[] buffer = packet.Buffer;
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 2;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 3;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
// Bad Length
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 4;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 3;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
// Bad Pointer
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 0;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 4;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 5;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
}
[TestMethod] [TestMethod]
[ExpectedException(typeof(ArgumentException))] [ExpectedException(typeof(ArgumentException))]
public void IpV4OptionsTooLongErrorTest() public void IpV4OptionsTooLongErrorTest()
...@@ -270,7 +357,7 @@ namespace PcapDotNet.Packets.Test ...@@ -270,7 +357,7 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IpV4OptionBasicSecurtiyBadLengthTest() public void IpV4OptionBasicSecurityConstructorBadLengthTest()
{ {
IpV4OptionBasicSecurity option = new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret, IpV4OptionSecurityProtectionAuthorities.None, 1); IpV4OptionBasicSecurity option = new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret, IpV4OptionSecurityProtectionAuthorities.None, 1);
Assert.IsNotNull(option); Assert.IsNotNull(option);
...@@ -279,13 +366,73 @@ namespace PcapDotNet.Packets.Test ...@@ -279,13 +366,73 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
[ExpectedException(typeof(ArgumentException))] [ExpectedException(typeof(ArgumentException))]
public void IpV4OptionBasicSecurtiyNoProtectionAuthoritiesButWithValueTest() public void IpV4OptionBasicSecurityConstructorNoProtectionAuthoritiesButWithValueTest()
{ {
IpV4OptionBasicSecurity option = new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret, IpV4OptionSecurityProtectionAuthorities.Nsa, 3); IpV4OptionBasicSecurity option = new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret, IpV4OptionSecurityProtectionAuthorities.Nsa, 3);
Assert.IsNotNull(option); Assert.IsNotNull(option);
Assert.Fail(); Assert.Fail();
} }
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4OptionBasicSecurityConstructorBadClassificationLevelTest()
{
IpV4OptionBasicSecurity option = new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.None);
Assert.IsNotNull(option);
Assert.Fail();
}
[TestMethod]
public void IpV4OptionBasicSecurityCreateInstanceErrorTest()
{
// Invalid Length
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionBasicSecurity()),
Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
byte[] buffer = packet.Buffer;
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 1] = 2;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
// Invalid classification level
packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret)),
Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer = packet.Buffer;
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 2] = 0;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
// Invalid protection authorities bytes
packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Confidential, IpV4OptionSecurityProtectionAuthorities.Nsa, 5)),
Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer = packet.Buffer;
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 3] = 0;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 3] = 1;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer[packet.Length - packet.Ethernet.IpV4.Length + IpV4Datagram.HeaderMinimumLength + 4] = 1;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
}
[TestMethod] [TestMethod]
[ExpectedException(typeof(ArgumentException))] [ExpectedException(typeof(ArgumentException))]
public void IpV4OptionQuickStartBadFunctionTest() public void IpV4OptionQuickStartBadFunctionTest()
...@@ -338,6 +485,23 @@ namespace PcapDotNet.Packets.Test ...@@ -338,6 +485,23 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(badPacket.IsValid, "badPacket.IsValid"); Assert.IsFalse(badPacket.IsValid, "badPacket.IsValid");
} }
[TestMethod]
public void IpV4DatagramInvalidHeaderChecksumTest()
{
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, IpV4Address.Zero, IpV4Address.Zero, IpV4Options.None,
Datagram.Empty);
Assert.IsTrue(packet.IsValid);
byte[] buffer = packet.Buffer;
++buffer[packet.Length - packet.Ethernet.IpV4.Length + 1];
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.IsValid);
}
[TestMethod] [TestMethod]
public void IpV4BadChecksumTest() public void IpV4BadChecksumTest()
{ {
......
using System; using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
...@@ -115,7 +116,8 @@ namespace PcapDotNet.Packets.Test ...@@ -115,7 +116,8 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(option, option); Assert.AreEqual(option, option);
Assert.AreEqual(option.GetHashCode(), option.GetHashCode()); Assert.AreEqual(option.GetHashCode(), option.GetHashCode());
Assert.IsFalse(string.IsNullOrEmpty(option.ToString())); Assert.IsFalse(string.IsNullOrEmpty(option.ToString()));
Assert.IsFalse(option.Equals(null));
Assert.IsFalse(option.Equals(2));
} }
Assert.AreEqual(tcpOptions, packet.Ethernet.IpV4.Tcp.Options, "Options"); Assert.AreEqual(tcpOptions, packet.Ethernet.IpV4.Tcp.Options, "Options");
Assert.AreEqual((tcpControlBits & TcpControlBits.Acknowledgment) == TcpControlBits.Acknowledgment, packet.Ethernet.IpV4.Tcp.IsAcknowledgment, "IsAcknowledgment"); Assert.AreEqual((tcpControlBits & TcpControlBits.Acknowledgment) == TcpControlBits.Acknowledgment, packet.Ethernet.IpV4.Tcp.IsAcknowledgment, "IsAcknowledgment");
...@@ -132,5 +134,54 @@ namespace PcapDotNet.Packets.Test ...@@ -132,5 +134,54 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(tcpPayload, packet.Ethernet.IpV4.Tcp.Payload, "Payload"); Assert.AreEqual(tcpPayload, packet.Ethernet.IpV4.Tcp.Payload, "Payload");
} }
} }
[TestMethod]
public void TcpOptionSelectiveAcknowledgmentBlockTest()
{
TcpOptionSelectiveAcknowledgmentBlock block1 = new TcpOptionSelectiveAcknowledgmentBlock();
Assert.AreEqual<uint>(0, block1.LeftEdge);
Assert.AreEqual<uint>(0, block1.RightEdge);
block1 = new TcpOptionSelectiveAcknowledgmentBlock(1, 2);
Assert.AreEqual<uint>(1, block1.LeftEdge);
Assert.AreEqual<uint>(2, block1.RightEdge);
TcpOptionSelectiveAcknowledgmentBlock block2 = new TcpOptionSelectiveAcknowledgmentBlock();
Assert.AreNotEqual(block1, block2);
Assert.IsTrue(block1 != block2);
Assert.IsFalse(block1 == block2);
block2 = new TcpOptionSelectiveAcknowledgmentBlock(1, 2);
Assert.AreEqual(block1, block2);
Assert.IsFalse(block1 != block2);
Assert.IsTrue(block1 == block2);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TcpOptionMd5SignatureConstructorErrorDataLengthTest()
{
new TcpOptionMd5Signature(new byte[10]);
Assert.Fail();
}
[TestMethod]
public void TcpOptionMd5SignatureCreateInstanceErrorDataLengthTest()
{
Packet packet = PacketBuilder.EthernetIpV4Tcp(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, new IpV4Address(), new IpV4Address(), new IpV4Options(),
0, 0, 0, 0, new TcpControlBits(), 0, 0,
new TcpOptions(new TcpOptionMd5Signature(new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16})), Datagram.Empty);
Assert.IsTrue(packet.IsValid);
Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Options.IsValid);
byte[] buffer = packet.Buffer;
buffer[buffer.Length - packet.Ethernet.IpV4.Tcp.Length + TcpDatagram.HeaderMinimumLength + 1] = 2;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Options.IsValid);
}
} }
} }
\ No newline at end of file
...@@ -44,6 +44,11 @@ namespace PcapDotNet.Packets.Arp ...@@ -44,6 +44,11 @@ namespace PcapDotNet.Packets.Arp
public const int HeaderBaseLength = 8; public const int HeaderBaseLength = 8;
public int HeaderLength
{
get { return GetHeaderLength(HardwareLength, ProtocolLength); }
}
/// <summary> /// <summary>
/// Each data link layer protocol is assigned a number used in this field. /// Each data link layer protocol is assigned a number used in this field.
/// </summary> /// </summary>
...@@ -117,6 +122,11 @@ namespace PcapDotNet.Packets.Arp ...@@ -117,6 +122,11 @@ namespace PcapDotNet.Packets.Arp
get { return ReadBytes(OffsetTargetProtocolAddress, ProtocolLength); } get { return ReadBytes(OffsetTargetProtocolAddress, ProtocolLength); }
} }
protected override bool CalculateIsValid()
{
return Length >= HeaderBaseLength && Length == HeaderLength;
}
internal ArpDatagram(byte[] buffer, int offset, int length) internal ArpDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length) : base(buffer, offset, length)
{ {
......
...@@ -329,8 +329,6 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -329,8 +329,6 @@ namespace PcapDotNet.Packets.IpV4
switch (Protocol) switch (Protocol)
{ {
// case IpV4Protocol.Tcp:
// return Tcp.IsValid; //&& IsTransportChecksumCorrect;
case IpV4Protocol.Tcp: case IpV4Protocol.Tcp:
case IpV4Protocol.Udp: case IpV4Protocol.Udp:
return Transport.IsValid && (Transport.IsChecksumOptional && Transport.Checksum == 0 || return Transport.IsValid && (Transport.IsChecksumOptional && Transport.Checksum == 0 ||
......
...@@ -79,6 +79,14 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -79,6 +79,14 @@ namespace PcapDotNet.Packets.IpV4
"protectionAuthorities"); "protectionAuthorities");
} }
if (classificationLevel != IpV4OptionSecurityClassificationLevel.Confidential &&
classificationLevel != IpV4OptionSecurityClassificationLevel.Secret &&
classificationLevel != IpV4OptionSecurityClassificationLevel.TopSecret &&
classificationLevel != IpV4OptionSecurityClassificationLevel.Unclassified)
{
throw new ArgumentException("Invalid classification level " + classificationLevel);
}
_classificationLevel = classificationLevel; _classificationLevel = classificationLevel;
_protectionAuthorities = protectionAuthorities; _protectionAuthorities = protectionAuthorities;
_length = length; _length = length;
......
...@@ -230,6 +230,11 @@ namespace PcapDotNet.Packets.Transport ...@@ -230,6 +230,11 @@ namespace PcapDotNet.Packets.Transport
get { return (ControlBits & TcpControlBits.Fin) == TcpControlBits.Fin; } get { return (ControlBits & TcpControlBits.Fin) == TcpControlBits.Fin; }
} }
protected override bool CalculateIsValid()
{
return Length >= HeaderMinimumLength && Length >= HeaderLength && Options.IsValid;
}
internal TcpDatagram(byte[] buffer, int offset, int length) internal TcpDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length) : base(buffer, offset, length)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment