C# 抓包工具
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CatchTool
{
class Program
{
private static Socket socket = null;
static void Main(string[] args)
{
string localIP = "192.168.10.6";
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
socket.Bind(new IPEndPoint(IPAddress.Parse(localIP), 0));
socket.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, new byte[4]);
Thread t = new Thread(Execute);
t.IsBackground = true;
t.Start();
Console.WriteLine("start...");
Console.ReadLine();
}
private static void Execute()
{
while (true)
{
try
{
byte[] buffer = new byte[10240];
int count = socket.Receive(buffer);
if (count > 0)
{
int headerLen = (buffer[0] & 0xf) * 4;
if (buffer[9] == (int)ProtocolType.Tcp)
{
byte[] data = new byte[count - headerLen];
Array.Copy(buffer, headerLen, data, 0, data.Length);
byte[] sourceIpData = new byte[4];
Array.Copy(buffer, 12, sourceIpData, 0, 4);
byte[] destIpData = new byte[4];
Array.Copy(buffer, 16, destIpData, 0, 4);
string sourceIp = $"{sourceIpData[0]}.{sourceIpData[1]}.{sourceIpData[2]}.{sourceIpData[3]}";
string destIp = $"{destIpData[0]}.{destIpData[1]}.{destIpData[2]}.{destIpData[3]}";
Console.WriteLine($"source IP={sourceIp} ,destination IP={destIp}");
}
}
}
catch (Exception)
{
throw;
}
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律