728x90
반응형
using System;
using SharpPcap;
using HexaViewer;
namespace Net_Packet001
{
class Program
{
static void Main(string[] args)
{
string NIC_Name = "Wi-Fi";
ICaptureDevice PcapDevice = null;
if (1 > CaptureDeviceList.Instance.Count)
{
Console.WriteLine("패킷을 캡쳐할 수 있는 장치가 존재하지 않아 종료합니다...");
return;
}
foreach (ICaptureDevice NIC in CaptureDeviceList.Instance)
{
string FriendName = NIC.ToString().Split('\n')[1];
FriendName = FriendName.Substring(FriendName.IndexOf(' ') + 1);
if (FriendName == NIC_Name)
{
PcapDevice = NIC;
break;
}
}
if (null == PcapDevice)
{
Console.WriteLine("캡쳐에 적합한 장치 이름을 검색할 수 없어 종료합니다...");
return;
}
else
{
Console.WriteLine("===========================================================================");
Console.WriteLine("캡쳐에 사용할 적합한 NIC의 검색을 완료하였습니다...");
Console.WriteLine("===========================================================================");
Console.WriteLine(PcapDevice);
Console.WriteLine("===========================================================================");
Console.WriteLine("패킷을 캡쳐하기 위해 위 장치를 사용합니다...");
Console.WriteLine("===========================================================================");
}
PcapDevice.Open(DeviceMode.Promiscuous, 0);
RawCapture Pakcet = PcapDevice.GetNextPacket();
HexaView.HexaViewText(Pakcet.Data);
PcapDevice.Close();
}
}
}
using System;
namespace HexaViewer
{
public class HexaView
{
static void PrintLine(char Char, ushort Count)
{
for(int Temp = 0; Temp < Count; ++Temp)
{
Console.Write(Char);
}
Console.WriteLine();
}
static void PrintHead(char Line1, char Line2, ushort Count)
{
PrintLine(Line1, Count);
PrintLine(Line2, Count);
Console.Write(" ADDRESS ");
for (int iCount = 0; iCount < 16; ++iCount)
{
Console.Write("{0:X02} ", iCount);
}
for (int iCount = 0; iCount < 16; ++iCount)
{
Console.Write("{0:X01}", iCount);
}
Console.WriteLine();
PrintLine(Line1, Count);
}
static void PrintHead()
{
PrintHead('=', '-',73);
}
static void PrintEnd()
{
PrintLine('=', 73);
}
public static void HexaViewText(byte[] Data)
{
ArraySegment<byte> TempDataPos;
int iLoopTotal = Data.Length / 16;
int iLoopRemain = Data.Length % 16;
PrintHead();
for (int iLoopCount = 0; iLoopCount < iLoopTotal; iLoopCount = iLoopCount + 1)
{
Console.Write("{0:X08} ", iLoopCount * 16);
TempDataPos = new ArraySegment<byte>(Data, iLoopCount * 16, 16);
foreach (var Temp in TempDataPos)
{
Console.Write("{0:X02} ", Temp);
}
foreach (char Temp in TempDataPos)
{
if (' ' > Temp)
{
Console.Write(".");
}
else if (127 < Temp)
{
Console.Write(".");
}
else
{
Console.Write("{0}", Temp);
}
}
Console.WriteLine();
}
if (0 != iLoopRemain)
{
iLoopTotal = iLoopTotal * 16;
Console.Write("{0:X08} ", iLoopTotal);
TempDataPos = new ArraySegment<byte>(Data, iLoopTotal, iLoopRemain);
foreach (var Temp in TempDataPos)
{
Console.Write("{0:X02} ", Temp);
}
for (int Temp = 0; Temp < 16 - iLoopRemain; ++Temp)
{
Console.Write(" ");
}
foreach (char Temp in TempDataPos)
{
if (' ' > Temp)
{
Console.Write(".");
}
else if (127 < Temp)
{
Console.Write(".");
}
else
{
Console.Write("{0}", Temp);
}
}
for (int Temp = 0; Temp < 16 - iLoopRemain; ++Temp)
{
Console.Write(" ");
}
}
Console.WriteLine();
PrintEnd();
}
}
}
728x90
'기술자료 > C#' 카테고리의 다른 글
C# 소켓 억셉트 Socket Asynchronous Accept 초간단 소스 (0) | 2021.09.15 |
---|---|
C# Console 비동기 입력 ReadLine ReadKey Asynchronous input 초간단 소스 (0) | 2021.09.15 |
C# Packet Capture Programming #3 - 패킷 캡쳐 GetNextPacket (0) | 2021.09.07 |
static HexaViewer Class 구현 - ArraySegment 응용 (0) | 2021.09.07 |
C# Packet Capture Programming #2 - 캡쳐 장치 선정 ICaptureDevice (0) | 2021.09.01 |
C# Packet Capture Programming #1 (0) | 2021.09.01 |
예외처리 1 : try catch 문법 (0) | 2021.06.10 |
포인터 사용 C# Pointer unsafe (0) | 2021.04.29 |