Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- namespace SocketSniffConsole {
- public enum IPFlags {
- Reserved = 0x80,
- DoNotFragment = 0x40,
- MoreFragments = 0x20
- }
- public enum PacketProtocol {
- Tcp = 6,
- Udp = 57
- }
- public class IPPacket {
- public int Version { get; set; }
- public int HeaderLength { get; set; }
- public int TypeOfService { get; set; }
- public int TotalLength { get; set; }
- public int Identification { get; set; }
- public IPFlags Flags { get; set; }
- public int FragmentOffset { get; set; }
- public int TimeToLive { get; set; }
- public PacketProtocol Protocol { get; set; }
- public int Checksum { get; set; }
- public IPAddress Source { get; set; }
- public IPAddress Destination { get; set; }
- public byte[] Options { get; set; }
- public byte[] Payload { get; set; }
- public IPPacket() {
- }
- public static IPPacket Parse(byte[] raw) {
- IPPacket packet;
- packet = new IPPacket();
- packet.Version = (raw[0] & 0xF0) >> 4;
- packet.HeaderLength = (raw[0] & 0x0F);
- packet.TypeOfService = raw[1];
- packet.TotalLength = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(raw, 2)); // LOL
- packet.Identification = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(raw, 4));
- packet.Flags = (IPFlags)(raw[6] & 0x70);
- packet.FragmentOffset = (BitConverter.ToInt32(raw, 6) & 0x1FFF);
- packet.TimeToLive = raw[8];
- packet.Protocol = (PacketProtocol)raw[9];
- packet.Checksum = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(raw, 10));
- packet.Source = new IPAddress(new byte[] { raw[12], raw[13], raw[14], raw[15] });
- packet.Destination = new IPAddress(new byte[] { raw[16], raw[17], raw[18], raw[19] });
- int optionsLength = packet.HeaderLength * 5 - 20;
- packet.Options = new byte[optionsLength];
- if (optionsLength > 0)
- Array.Copy(raw, 20, packet.Options, 0, optionsLength);
- int payloadLength = packet.TotalLength - 20 - optionsLength;
- packet.Payload = new byte[payloadLength];
- Array.Copy(raw, 20 + optionsLength, packet.Payload, 0, payloadLength);
- return packet;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement