移除PacketDotNet

This commit is contained in:
老九 2021-09-17 22:03:13 +08:00
parent 3cdefb8553
commit 8c752000c5
2 changed files with 31 additions and 37 deletions

View File

@ -2,8 +2,8 @@
using DNS.Protocol.ResourceRecords; using DNS.Protocol.ResourceRecords;
using FastGithub.Configuration; using FastGithub.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using PacketDotNet;
using System; using System;
using System.Buffers.Binary;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -62,8 +62,7 @@ namespace FastGithub.Dns
}, handle); }, handle);
var packetLength = 0U; var packetLength = 0U;
var packetBuffer = new byte[ushort.MaxValue]; using var winDivertBuffer = new WinDivertBuffer();
using var winDivertBuffer = new WinDivertBuffer(packetBuffer);
var winDivertAddress = new WinDivertAddress(); var winDivertAddress = new WinDivertAddress();
DnsFlushResolverCache(); DnsFlushResolverCache();
@ -73,10 +72,7 @@ namespace FastGithub.Dns
{ {
try try
{ {
if (this.ModifyDnsPacket(packetBuffer, ref winDivertAddress, ref packetLength)) this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
{
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -93,54 +89,53 @@ namespace FastGithub.Dns
/// <summary> /// <summary>
/// 修改DNS数据包 /// 修改DNS数据包
/// </summary> /// </summary>
/// <param name="packetBuffer"></param> /// <param name="winDivertBuffer"></param>
/// <param name="winDivertAddress"></param> /// <param name="winDivertAddress"></param>
/// <param name="packetLength"></param> /// <param name="packetLength"></param>
/// <returns></returns> unsafe private void ModifyDnsPacket(WinDivertBuffer winDivertBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
private bool ModifyDnsPacket(byte[] packetBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
{ {
var packetData = packetBuffer.AsSpan(0, (int)packetLength).ToArray(); var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength);
var packet = Packet.ParsePacket(LinkLayers.Raw, packetData); var requestPayload = new Span<byte>(packet.PacketPayload, (int)packet.PacketPayloadLength).ToArray();
var ipPacket = (IPPacket)packet.PayloadPacket;
var udpPacket = (UdpPacket)ipPacket.PayloadPacket;
var request = Request.FromArray(udpPacket.PayloadData); var request = Request.FromArray(requestPayload);
if (request.OperationCode != OperationCode.Query) if (request.OperationCode != OperationCode.Query)
{ {
return false; return;
} }
var question = request.Questions.FirstOrDefault(); var question = request.Questions.FirstOrDefault();
if (question == null || question.Type != RecordType.A) if (question == null || question.Type != RecordType.A)
{ {
return false; return;
} }
var domain = question.Name; var domain = question.Name;
if (this.fastGithubConfig.IsMatch(domain.ToString()) == false) if (this.fastGithubConfig.IsMatch(domain.ToString()) == false)
{ {
return false; return;
} }
// 反转ip // dns响应数据
var destAddress = ipPacket.DestinationAddress;
ipPacket.DestinationAddress = ipPacket.SourceAddress;
ipPacket.SourceAddress = destAddress;
// 反转端口
var destPort = udpPacket.DestinationPort;
udpPacket.DestinationPort = udpPacket.SourcePort;
udpPacket.SourcePort = destPort;
// 设置dns响应
var response = Response.FromRequest(request); var response = Response.FromRequest(request);
var record = new IPAddressResourceRecord(domain, IPAddress.Loopback, this.ttl); var record = new IPAddressResourceRecord(domain, IPAddress.Loopback, this.ttl);
response.AnswerRecords.Add(record); response.AnswerRecords.Add(record);
udpPacket.PayloadData = response.ToArray(); var responsePayload = response.ToArray();
// 修改数据内容和数据长度 // 修改payload和包长
packet.Bytes.CopyTo(packetBuffer, 0); responsePayload.CopyTo(new Span<byte>(packet.PacketPayload, responsePayload.Length));
packetLength = (uint)packet.Bytes.Length; packetLength += (uint)responsePayload.Length - packet.PacketPayloadLength;
// 修改ip包
var destAddress = packet.IPv4Header->DstAddr;
packet.IPv4Header->DstAddr = packet.IPv4Header->SrcAddr;
packet.IPv4Header->SrcAddr = destAddress;
packet.IPv4Header->Length = BinaryPrimitives.ReverseEndianness((ushort)packetLength);
// 修改udp包
var destPort = packet.UdpHeader->DstPort;
packet.UdpHeader->DstPort = packet.UdpHeader->SrcPort;
packet.UdpHeader->SrcPort = destPort;
packet.UdpHeader->Length = BinaryPrimitives.ReverseEndianness((ushort)(responsePayload.Length + 8));
// 反转方向 // 反转方向
if (winDivertAddress.Direction == WinDivertDirection.Inbound) if (winDivertAddress.Direction == WinDivertDirection.Inbound)
@ -152,8 +147,8 @@ namespace FastGithub.Dns
winDivertAddress.Direction = WinDivertDirection.Inbound; winDivertAddress.Direction = WinDivertDirection.Inbound;
} }
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}"); this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}");
return true;
} }
} }
} }

View File

@ -8,7 +8,6 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="DNS" Version="6.1.0" /> <PackageReference Include="DNS" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="PacketDotNet" Version="1.3.0" />
<PackageReference Include="WinDivertSharp" Version="1.4.3.3" /> <PackageReference Include="WinDivertSharp" Version="1.4.3.3" />
</ItemGroup> </ItemGroup>