-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacketInfo.cs
More file actions
156 lines (129 loc) · 4.22 KB
/
PacketInfo.cs
File metadata and controls
156 lines (129 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Linq;
using System.Net.NetworkInformation;
using IPK_Project2.Interfaces;
using PacketDotNet;
using SharpPcap;
namespace IPK_Project2;
public class PacketInfo : IPacketInfo {
private string? Timestamp { get; set; }
private string? SourceMac { get; set; }
private string? DestinationMac { get; set; }
private int FrameLength { get; set; }
private string? SourceIp { get; set; }
private string? DestinationIp { get; set; }
private int SourcePort { get; set; }
private int DestinationPort { get; set; }
private ProtocolType Protocol { get; set; }
private EthernetType EthernetType { get; set; }
private Packet? Packet { get; set; }
// Convert packet into object
public void PacketToObject(PacketCapture packetCapture) {
Packet = Packet.ParsePacket(packetCapture.GetPacket().LinkLayerType,
packetCapture.GetPacket().Data);
ParseEthernetPacket();
if (EthernetType is EthernetType.IPv4 or EthernetType.IPv6) {
ParseIpPacket();
}
Timestamp = packetCapture.Header.Timeval.Date.ToLocalTime().ToString("o");
FrameLength = packetCapture.Data.Length;
}
// Extract information from ethernet frame
private void ParseEthernetPacket() {
var ethPacket = (EthernetPacket)Packet!;
EthernetType = ethPacket.Type;
SourceMac = FormatMacAddress(ethPacket.SourceHardwareAddress);
DestinationMac = FormatMacAddress(ethPacket.DestinationHardwareAddress);
}
// Extract information from IP packet
private void ParseIpPacket() {
var ipPacket = Packet!.Extract<IPPacket>();
Protocol = ipPacket.Protocol;
switch (Protocol) {
case ProtocolType.Tcp: {
var tpcPacket = Packet.Extract<TcpPacket>();
SourcePort = tpcPacket.SourcePort;
DestinationPort = tpcPacket.DestinationPort;
break;
}
case ProtocolType.Udp: {
var udpPacket = Packet.Extract<UdpPacket>();
SourcePort = udpPacket.SourcePort;
DestinationPort = udpPacket.DestinationPort;
break;
}
}
SourceIp = ipPacket.SourceAddress.ToString();
DestinationIp = ipPacket.DestinationAddress.ToString();
}
// Print the packet to console
public void PrintPacket(int currentPacket, int max) {
// Print separator
if (currentPacket > 1) {
Console.WriteLine();
Console.WriteLine(new string('-', 74));
Console.WriteLine();
}
Console.WriteLine($"Packet {currentPacket}/{max}");
PrintGeneralPacketInformation();
PrintPacketHexDump();
}
private void PrintGeneralPacketInformation() {
Console.WriteLine($"timestamp: {Timestamp}");
Console.WriteLine($"src MAC: {SourceMac}");
Console.WriteLine($"dst MAC: {DestinationMac}");
Console.WriteLine($"frame length: {FrameLength} bytes");
if (EthernetType is EthernetType.IPv4 or EthernetType.IPv6) {
Console.WriteLine($"src IP: {SourceIp}");
Console.WriteLine($"dst IP: {DestinationIp}");
if (Protocol is ProtocolType.Tcp or ProtocolType.Udp) {
Console.WriteLine($"src port: {SourcePort}");
Console.WriteLine($"dst port: {DestinationPort}");
}
Console.WriteLine($"protocol: {Protocol.ToString()}");
}
Console.WriteLine($"ethernet type: {EthernetType.ToString()}");
Console.WriteLine();
}
private void PrintPacketHexDump() {
if (Packet == null) {
return;
}
var bytes = Packet.Bytes;
for (int i = 0; i < bytes.Length; i += 16) {
// Left side
Console.Write($"0x{i:x4}: ");
char[] chars = FormatHexLine(bytes, i);
// Right side
Console.Write(" ");
Console.WriteLine(chars);
}
}
private char[] FormatHexLine(byte[] bytes, int offset) {
char[] chars = new char[16];
// 2x 16 per line
for (int j = 0; j < 16; j++) {
chars[j] = FormatByte(bytes, offset + j);
// Every 8 byted add space
if (j == 7) {
Console.Write(" ");
}
}
return chars;
}
private char FormatByte(byte[] bytes, int index) {
if (index < bytes.Length) {
Console.Write($"{bytes[index]:x2} ");
// Non-printable characters are replaced with '.'
char c = (char)bytes[index];
return char.IsControl(c) ? '.' : c;
}
// Fill the last row if it's less than 16 bytes
Console.Write(" ");
return ' ';
}
// Converts mac address into "00:1b:3f:56:8a:00" format
private static string FormatMacAddress(PhysicalAddress address) {
return string.Join(":", address.GetAddressBytes().Select(x => x.ToString("x2")).ToArray());
}
}