Skip to content

Commit d88cc8c

Browse files
committed
Preparations for v2.0
- Fixed a bug in the spoof protection mechanism. - Fixed a bug related to the tray icon's context menu. - Fixed a bug related to blocking newly discovered devices from toast notifications. - Code refactorings.
1 parent 2f491aa commit d88cc8c

File tree

8 files changed

+109
-118
lines changed

8 files changed

+109
-118
lines changed

NetStalker/NetStalker/App.config

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<configSections>
44
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
5-
<section name="NetStalker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
5+
<section name="NetStalker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
66
</sectionGroup>
77
</configSections>
88
<startup>
9-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
9+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
1010
</startup>
1111
<userSettings>
1212
<NetStalker.Properties.Settings>
1313
<setting name="FriendlyName" serializeAs="String">
14-
<value />
14+
<value/>
1515
</setting>
1616
<setting name="Gateway" serializeAs="String">
17-
<value />
17+
<value/>
1818
</setting>
1919
<setting name="LocalIp" serializeAs="String">
20-
<value />
20+
<value/>
2121
</setting>
2222
<setting name="Color" serializeAs="String">
23-
<value />
23+
<value/>
2424
</setting>
2525
<setting name="Accent" serializeAs="String">
26-
<value />
26+
<value/>
2727
</setting>
2828
<setting name="Tile" serializeAs="String">
29-
<value />
29+
<value/>
3030
</setting>
3131
<setting name="Minimize" serializeAs="String">
32-
<value />
32+
<value/>
3333
</setting>
3434
<setting name="GatewayMac" serializeAs="String">
35-
<value />
35+
<value/>
3636
</setting>
3737
<setting name="Protocol" serializeAs="String">
38-
<value />
38+
<value/>
3939
</setting>
4040
<setting name="PacketDirection" serializeAs="String">
41-
<value />
41+
<value/>
4242
</setting>
4343
<setting name="AdapterName" serializeAs="String">
44-
<value />
44+
<value/>
4545
</setting>
4646
<setting name="SpoofProtection" serializeAs="String">
4747
<value>False</value>
4848
</setting>
4949
<setting name="NetMask" serializeAs="String">
50-
<value />
50+
<value/>
5151
</setting>
5252
<setting name="NetSize" serializeAs="String">
5353
<value>0</value>
5454
</setting>
5555
<setting name="SuppressN" serializeAs="String">
56-
<value />
56+
<value/>
5757
</setting>
5858
<setting name="LocalMac" serializeAs="String">
59-
<value />
59+
<value/>
6060
</setting>
6161
<setting name="BroadcastAddress" serializeAs="String">
62-
<value />
62+
<value/>
6363
</setting>
6464
</NetStalker.Properties.Settings>
6565
</userSettings>
6666
<runtime>
6767
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
6868
<dependentAssembly>
69-
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
70-
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
69+
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
70+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
7171
</dependentAssembly>
7272
</assemblyBinding>
73-
<legacyCorruptedStateExceptionsPolicy enabled="true" />
73+
<legacyCorruptedStateExceptionsPolicy enabled="true"/>
7474
</runtime>
7575
</configuration>

NetStalker/NetStalker/Forms/Main/Main.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public partial class Main : MaterialForm, IView
5252
/// The list of targets of type <see cref="Device"/>
5353
/// </summary>
5454
public static ConcurrentDictionary<IPAddress, Device> Devices;
55+
/// <summary>
56+
/// An indication that the app is being closed from the tray icon's context menu.
57+
/// </summary>
58+
public bool TrayExitFlag = false;
5559
#endregion
5660

5761
public Main(string[] args = null)
@@ -344,9 +348,14 @@ private void Main_FormClosing(object sender, FormClosingEventArgs e)
344348
ValuesTimer.Dispose();
345349
}
346350

347-
if (MetroMessageBox.Show(this, "Quit the application ?", "Quit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
351+
Devices = null;
352+
353+
if (e.CloseReason == CloseReason.UserClosing && !TrayExitFlag)
348354
{
349-
e.Cancel = true;
355+
if (MetroMessageBox.Show(this, "Quit the application ?", "Quit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
356+
{
357+
e.Cancel = true;
358+
}
350359
}
351360
}
352361
}
@@ -591,7 +600,7 @@ private void FastObjectListView1_ItemsAdding(object sender, ItemsAddingEventArgs
591600
if (Ad.Count > 0)
592601
{
593602
Device Device = Ad[0];
594-
NotificationAPI Napi = new NotificationAPI(Device);
603+
NotificationAPI Napi = new NotificationAPI(Device, this);
595604
Napi.CreateNotification();
596605
Napi.AttachHandlers();
597606
Napi.ShowToast();
@@ -601,6 +610,11 @@ private void FastObjectListView1_ItemsAdding(object sender, ItemsAddingEventArgs
601610
}
602611

603612
private void FastObjectListView1_SubItemChecking(object sender, SubItemCheckingEventArgs e)
613+
{
614+
SubItemCheckingHandler(e);
615+
}
616+
617+
public void SubItemCheckingHandler(SubItemCheckingEventArgs e)
604618
{
605619
try
606620
{
@@ -633,6 +647,9 @@ private void FastObjectListView1_SubItemChecking(object sender, SubItemCheckingE
633647
//Activate the BR if it's not already active
634648
if (!Blocker_Redirector.BRMainSwitch)
635649
{
650+
if (Blocker_Redirector.BRTask != null && Blocker_Redirector.BRTask.Status == TaskStatus.Running)
651+
Blocker_Redirector.BRTask.Wait();
652+
636653
Blocker_Redirector.BRMainSwitch = true;
637654
Blocker_Redirector.BlockAndRedirect();
638655
}
@@ -658,6 +675,9 @@ private void FastObjectListView1_SubItemChecking(object sender, SubItemCheckingE
658675
//Activate the BR if it's not already active
659676
if (!Blocker_Redirector.BRMainSwitch)
660677
{
678+
if (Blocker_Redirector.BRTask != null && Blocker_Redirector.BRTask.Status == TaskStatus.Running)
679+
Blocker_Redirector.BRTask.Wait();
680+
661681
Blocker_Redirector.BRMainSwitch = true;
662682
Blocker_Redirector.BlockAndRedirect();
663683
}

NetStalker/NetStalker/MainLogic/Blocker-Redirector.cs

+47-47
Original file line numberDiff line numberDiff line change
@@ -46,50 +46,50 @@ public static void BlockAndRedirect()
4646
MainDevice.Filter = "ip";
4747

4848
BRTask = Task.Run(() =>
49-
{
50-
RawCapture rawCapture;
51-
EthernetPacket packet;
52-
53-
while (BRMainSwitch)
54-
{
55-
if ((rawCapture = MainDevice.GetNextPacket()) != null)
56-
{
57-
packet = Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data) as EthernetPacket;
58-
if (packet == null)
59-
continue;
60-
61-
KeyValuePair<IPAddress, Device> device;
62-
63-
if (!(device = Main.Devices.FirstOrDefault(D => D.Value.MAC.Equals(packet.SourceHardwareAddress))).Equals(default(KeyValuePair<IPAddress, Device>)) && device.Value.Redirected)
64-
{
65-
if (device.Value.UploadCap == 0 || device.Value.UploadCap > device.Value.PacketsSentSinceLastReset)
66-
{
67-
packet.SourceHardwareAddress = MainDevice.MacAddress;
68-
packet.DestinationHardwareAddress = AppConfiguration.GatewayMac;
69-
MainDevice.SendPacket(packet);
70-
device.Value.PacketsSentSinceLastReset += packet.Bytes.Length;
71-
}
72-
}
73-
else if (packet.SourceHardwareAddress.Equals(AppConfiguration.GatewayMac))
74-
{
75-
IPv4Packet IPV4 = packet.Extract<IPv4Packet>();
76-
77-
if (!(device = Main.Devices.FirstOrDefault(D => D.Key.Equals(IPV4.DestinationAddress))).Equals(default(KeyValuePair<IPAddress, Device>)) && device.Value.Redirected)
78-
{
79-
if (device.Value.DownloadCap == 0 || device.Value.DownloadCap > device.Value.PacketsReceivedSinceLastReset)
80-
{
81-
packet.SourceHardwareAddress = MainDevice.MacAddress;
82-
packet.DestinationHardwareAddress = device.Value.MAC;
83-
MainDevice.SendPacket(packet);
84-
device.Value.PacketsReceivedSinceLastReset += packet.Bytes.Length;
85-
}
86-
}
87-
}
88-
}
89-
90-
SpoofClients();
91-
}
92-
});
49+
{
50+
RawCapture rawCapture;
51+
EthernetPacket packet;
52+
53+
while (BRMainSwitch)
54+
{
55+
if ((rawCapture = MainDevice.GetNextPacket()) != null)
56+
{
57+
packet = Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data) as EthernetPacket;
58+
if (packet == null)
59+
continue;
60+
61+
KeyValuePair<IPAddress, Device> device;
62+
63+
if (!(device = Main.Devices.FirstOrDefault(D => D.Value.MAC.Equals(packet.SourceHardwareAddress))).Equals(default(KeyValuePair<IPAddress, Device>)) && device.Value.Redirected)
64+
{
65+
if (device.Value.UploadCap == 0 || device.Value.UploadCap > device.Value.PacketsSentSinceLastReset)
66+
{
67+
packet.SourceHardwareAddress = MainDevice.MacAddress;
68+
packet.DestinationHardwareAddress = AppConfiguration.GatewayMac;
69+
MainDevice.SendPacket(packet);
70+
device.Value.PacketsSentSinceLastReset += packet.Bytes.Length;
71+
}
72+
}
73+
else if (packet.SourceHardwareAddress.Equals(AppConfiguration.GatewayMac))
74+
{
75+
IPv4Packet IPV4 = packet.Extract<IPv4Packet>();
76+
77+
if (!(device = Main.Devices.FirstOrDefault(D => D.Key.Equals(IPV4.DestinationAddress))).Equals(default(KeyValuePair<IPAddress, Device>)) && device.Value.Redirected)
78+
{
79+
if (device.Value.DownloadCap == 0 || device.Value.DownloadCap > device.Value.PacketsReceivedSinceLastReset)
80+
{
81+
packet.SourceHardwareAddress = MainDevice.MacAddress;
82+
packet.DestinationHardwareAddress = device.Value.MAC;
83+
MainDevice.SendPacket(packet);
84+
device.Value.PacketsReceivedSinceLastReset += packet.Bytes.Length;
85+
}
86+
}
87+
}
88+
}
89+
90+
SpoofClients();
91+
}
92+
});
9393
}
9494

9595
/// <summary>
@@ -135,12 +135,12 @@ public static void ConstructAndSendArp(Device device, BROperation Operation)
135135
}
136136
else
137137
{
138-
ArpPacket ArpPacketForVicProtection = new ArpPacket(ArpOperation.Request, MainDevice.MacAddress, AppConfiguration.LocalIp, device.MAC, device.IP);
139-
ArpPacket ArpPacketForGatewayProtection = new ArpPacket(ArpOperation.Request, MainDevice.MacAddress, AppConfiguration.LocalIp, AppConfiguration.GatewayMac, AppConfiguration.GatewayIp)
138+
ArpPacket ArpPacketForVicProtection = new ArpPacket(ArpOperation.Response, MainDevice.MacAddress, AppConfiguration.LocalIp, device.MAC, device.IP);
139+
ArpPacket ArpPacketForGatewayProtection = new ArpPacket(ArpOperation.Response, MainDevice.MacAddress, AppConfiguration.LocalIp, AppConfiguration.GatewayMac, AppConfiguration.GatewayIp);
140+
EthernetPacket EtherPacketForVicProtection = new EthernetPacket(device.MAC, MainDevice.MacAddress, EthernetType.Arp)
140141
{
141142
PayloadPacket = ArpPacketForVicProtection
142143
};
143-
EthernetPacket EtherPacketForVicProtection = new EthernetPacket(device.MAC, MainDevice.MacAddress, EthernetType.Arp);
144144
EthernetPacket EtherPacketForGatewayProtection = new EthernetPacket(AppConfiguration.GatewayMac, MainDevice.MacAddress, EthernetType.Arp)
145145
{
146146
PayloadPacket = ArpPacketForGatewayProtection

NetStalker/NetStalker/MainLogic/Controller.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NetStalker.MainLogic;
2+
using NetStalker.ToastNotifications;
23
using System;
34
using System.Windows.Forms;
45

@@ -53,7 +54,7 @@ public static void AttachOnExitEventHandler(IView view)
5354
Scanner.CloseAllCaptures(view);
5455

5556
//Clean all notifications
56-
//NotificationAPI.ClearNotifications();
57+
NotificationAPI.ClearNotifications();
5758
};
5859
}
5960
}

NetStalker/NetStalker/MainLogic/DeviceList.cs

-30
This file was deleted.

NetStalker/NetStalker/MainLogic/Tools.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
1+
using BrightIdeasSoftware;
2+
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
23
using ShellHelpers;
34
using System;
45
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
78
using System.Net;
89
using System.Net.NetworkInformation;
10+
using System.Reflection;
911

1012
namespace NetStalker.MainLogic
1113
{

NetStalker/NetStalker/NetStalker.csproj

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<OutputType>WinExe</OutputType>
99
<RootNamespace>NetStalker</RootNamespace>
1010
<AssemblyName>NetStalker</AssemblyName>
11-
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1414
<Deterministic>true</Deterministic>
@@ -35,7 +35,7 @@
3535
<PlatformTarget>x86</PlatformTarget>
3636
<DebugSymbols>true</DebugSymbols>
3737
<DebugType>full</DebugType>
38-
<Optimize>true</Optimize>
38+
<Optimize>false</Optimize>
3939
<OutputPath>bin\Debug\</OutputPath>
4040
<DefineConstants>
4141
</DefineConstants>
@@ -45,7 +45,7 @@
4545
<RunCodeAnalysis>false</RunCodeAnalysis>
4646
</PropertyGroup>
4747
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
48-
<PlatformTarget>x64</PlatformTarget>
48+
<PlatformTarget>x86</PlatformTarget>
4949
<DebugType>pdbonly</DebugType>
5050
<Optimize>true</Optimize>
5151
<OutputPath>bin\Release\</OutputPath>
@@ -104,7 +104,6 @@
104104
<Compile Include="Forms\Information\ErrorForm.Designer.cs">
105105
<DependentUpon>ErrorForm.cs</DependentUpon>
106106
</Compile>
107-
<Compile Include="MainLogic\DeviceList.cs" />
108107
<Compile Include="MainLogic\NativeMethods.cs" />
109108
<Compile Include="MainLogic\Scanner.cs" />
110109
<Compile Include="MainLogic\IView.cs" />

0 commit comments

Comments
 (0)