Skip to content

Commit 172686c

Browse files
authored
Merge pull request #112 from odedshimon/refactor-data-context
Refactor data context
2 parents 8adb5f8 + 0252097 commit 172686c

File tree

32 files changed

+512
-207
lines changed

32 files changed

+512
-207
lines changed

BruteShark/BruteForce/BruteForce.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
56
</PropertyGroup>
67

78
</Project>

BruteShark/BruteSharkCli/Cli Shell/CliShell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private void PrintHashes()
229229

230230
private void PrintNetworkMap()
231231
{
232-
Console.WriteLine(CommonUi.Exporting.GetNetworkMapAsJsonString(this._connections));
232+
Console.WriteLine(CommonUi.Exporting.GetIndentdJson(this._connections));
233233
}
234234

235235
private void StartAnalyzing()

BruteShark/BruteSharkCli/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"BruteSharkCli": {
44
"commandName": "Project",
5-
"commandLineArgs": "-d C:\\\\Users\\King\\\\github\\BS_SEP\\BruteShark\\Pcap_Examples -m Credentials,NetworkMap,FileExtracting,DNS -o C:\\\\Users\\King\\Desktop\\Test"
5+
"commandLineArgs": "-d C:\\\\Users\\King\\\\github\\BS_NETWORK\\BruteShark\\Pcap_Examples -m Credentials,NetworkMap,FileExtracting,DNS -o C:\\\\Users\\King\\Desktop\\Test"
66
}
77
}
88
}

BruteShark/BruteSharkCli/Single Command Runner/SingleCommandRunner.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ class SingleCommandRunner
1515
{
1616
private SingleCommandFlags _cliFlags;
1717
private List<string> _files;
18+
private CommonUi.NetworkContext _networkContext;
1819
private HashSet<PcapAnalyzer.NetworkFile> _extractedFiles;
1920
private HashSet<PcapAnalyzer.NetworkPassword> _passwords;
2021
private HashSet<PcapAnalyzer.NetworkHash> _hashes;
21-
private HashSet<PcapAnalyzer.NetworkConnection> _connections;
22+
//private HashSet<PcapAnalyzer.NetworkConnection> _connections;
2223
private HashSet<CommonUi.VoipCall> _voipCalls;
2324
private HashSet<PcapAnalyzer.DnsNameMapping> _dnsMappings;
2425

@@ -41,8 +42,8 @@ public SingleCommandRunner(Analyzer analyzer, Processor processor, Sniffer sniff
4142
_processor = processor;
4243
_files = new List<string>();
4344

45+
_networkContext = new NetworkContext();
4446
_hashes = new HashSet<PcapAnalyzer.NetworkHash>();
45-
_connections = new HashSet<PcapAnalyzer.NetworkConnection>();
4647
_passwords = new HashSet<NetworkPassword>();
4748
_extractedFiles = new HashSet<NetworkFile>();
4849
_voipCalls = new HashSet<CommonUi.VoipCall>();
@@ -214,10 +215,12 @@ private void ExportResults()
214215
{
215216
if (_cliFlags.OutputDir != null)
216217
{
217-
if (_connections.Any())
218+
if (_networkContext.Connections.Any())
218219
{
219-
var filePath = CommonUi.Exporting.ExportNetworkMap(_cliFlags.OutputDir, _connections);
220-
CliPrinter.Info($"Successfully exported network map to json file: {filePath}");
220+
var networkMapFilePath = CommonUi.Exporting.ExportNetworkMap(_cliFlags.OutputDir, _networkContext.Connections);
221+
CliPrinter.Info($"Successfully exported network map to json file: {networkMapFilePath}");
222+
var nodesDataFilePath = CommonUi.Exporting.ExportNetworkNodesData(_cliFlags.OutputDir, _networkContext.GetAllNodes());
223+
CliPrinter.Info($"Successfully exported network nodes data to json file: {nodesDataFilePath}");
221224
}
222225
if (_hashes.Any())
223226
{
@@ -237,7 +240,7 @@ private void ExportResults()
237240
if(_voipCalls.Any())
238241
{
239242
var dirPath = CommonUi.Exporting.ExportVoipCalls(_cliFlags.OutputDir, _voipCalls);
240-
CliPrinter.Info($"Successfully exported voip calss extracted to: {dirPath}");
243+
CliPrinter.Info($"Successfully exported voip calls extracted to: {dirPath}");
241244
}
242245
}
243246

@@ -304,7 +307,7 @@ private void OnParsedItemDetected(object sender, ParsedItemDetectedEventArgs e)
304307
else if (e.ParsedItem is PcapAnalyzer.NetworkConnection)
305308
{
306309
var networkConnection = e.ParsedItem as NetworkConnection;
307-
_connections.Add(networkConnection);
310+
_networkContext.Connections.Add(networkConnection);
308311
}
309312
else if (e.ParsedItem is PcapAnalyzer.VoipCall)
310313
{

BruteShark/BruteSharkDesktop/BruteSharkDesktop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="AutomaticGraphLayout" Version="1.1.9" />
1313
<PackageReference Include="AutomaticGraphLayout.Drawing" Version="1.1.9" />
1414
<PackageReference Include="AutomaticGraphLayout.GraphViewerGDI" Version="1.1.9" />
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1516
</ItemGroup>
1617

1718
<ItemGroup>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Newtonsoft.Json.Linq;
2+
using System.Windows.Forms;
3+
4+
namespace BruteSharkDesktop
5+
{
6+
// Originally taken from https://stackoverflow.com/questions/39673815/how-to-recursively-populate-a-treeview-with-json-data
7+
// Did some customizations for BruteShark needs.
8+
public static class JsonTreeViewLoader
9+
{
10+
public static void LoadJsonToTreeView(this TreeView treeView, string json, string rootNodeText)
11+
{
12+
var root = JToken.Parse(json);
13+
DisplayTreeView(treeView, root, rootNodeText);
14+
}
15+
16+
private static void DisplayTreeView(TreeView treeView, JToken root, string rootName)
17+
{
18+
treeView.BeginUpdate();
19+
try
20+
{
21+
treeView.Nodes.Clear();
22+
var tNode = treeView.Nodes[treeView.Nodes.Add(new TreeNode(rootName))];
23+
tNode.Tag = root;
24+
25+
AddNode(root, tNode);
26+
27+
treeView.ExpandAll();
28+
}
29+
finally
30+
{
31+
treeView.EndUpdate();
32+
}
33+
}
34+
35+
private static void AddNode(JToken token, TreeNode inTreeNode)
36+
{
37+
if (token == null)
38+
return;
39+
if (token is JValue)
40+
{
41+
var childNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(token.ToString()))];
42+
childNode.Tag = token;
43+
}
44+
else if (token is JObject jObject)
45+
{
46+
foreach (var property in jObject.Properties())
47+
{
48+
var childNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(property.Name))];
49+
childNode.Tag = property;
50+
AddNode(property.Value, childNode);
51+
}
52+
}
53+
else if (token is JArray jArray)
54+
{
55+
foreach (JValue jv in jArray)
56+
{
57+
var childNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(jv.ToString()))];
58+
}
59+
}
60+
else
61+
{
62+
// TODO: log
63+
// Debug.WriteLine(string.Format("{0} not implemented", token.Type)); // JConstructor, JRaw
64+
}
65+
}
66+
67+
68+
}
69+
}

BruteShark/BruteSharkDesktop/MainForm.cs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class MainForm : Form
1818
{
1919
private CancellationTokenSource _cts;
2020
private HashSet<string> _files;
21-
private HashSet<PcapAnalyzer.NetworkConnection> _connections;
21+
private CommonUi.NetworkContext _networkContext;
2222
private PcapProcessor.Processor _processor;
2323
private PcapProcessor.Sniffer _sniffer;
2424
private PcapAnalyzer.Analyzer _analyzer;
@@ -38,7 +38,7 @@ public MainForm()
3838

3939
_files = new HashSet<string>();
4040
_cts = new CancellationTokenSource();
41-
_connections = new HashSet<PcapAnalyzer.NetworkConnection>();
41+
_networkContext = new CommonUi.NetworkContext();
4242

4343
// Create the DAL and BLL objects.
4444
_processor = new PcapProcessor.Processor();
@@ -51,13 +51,13 @@ public MainForm()
5151
_sniffer.UdpPacketArived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorUdpPacketToAnalyzerUdpPacket(e.Packet));
5252
_sniffer.TcpPacketArived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorTcpPacketToAnalyzerTcpPacket(e.Packet));
5353
_sniffer.TcpSessionArrived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorTcpSessionToAnalyzerTcpSession(e.TcpSession));
54-
_sniffer.TcpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(Casting.CastProcessorTcpSessionToBruteSharkDesktopTcpSession(e.TcpSession)));
55-
_sniffer.UdpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(Casting.CastProcessorUdpSessionToBruteSharkDesktopUdpSession(e.UdpSession)));
54+
_sniffer.TcpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(e.TcpSession));
55+
_sniffer.UdpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(e.UdpSession));
5656
_processor.UdpPacketArived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorUdpPacketToAnalyzerUdpPacket(e.Packet));
5757
_processor.TcpPacketArived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorTcpPacketToAnalyzerTcpPacket(e.Packet));
5858
_processor.TcpSessionArrived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorTcpSessionToAnalyzerTcpSession(e.TcpSession));
59-
_processor.TcpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(Casting.CastProcessorTcpSessionToBruteSharkDesktopTcpSession(e.TcpSession)));
60-
_processor.UdpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(Casting.CastProcessorUdpSessionToBruteSharkDesktopUdpSession(e.UdpSession)));
59+
_processor.TcpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(e.TcpSession));
60+
_processor.UdpSessionArrived += (s, e) => SwitchToMainThreadContext(() => OnSessionArived(e.UdpSession));
6161
_processor.FileProcessingStatusChanged += (s, e) => SwitchToMainThreadContext(() => OnFileProcessingStatusChanged(s, e));
6262
_processor.ProcessingPrecentsChanged += (s, e) => SwitchToMainThreadContext(() => OnProcessingPrecentsChanged(s, e));
6363
_processor.ProcessingFinished += (s, e) => SwitchToMainThreadContext(() => OnProcessingFinished(s, e));
@@ -73,9 +73,9 @@ public MainForm()
7373

7474
private void InitilizeModulesUserControls()
7575
{
76-
_networkMapUserControl = new NetworkMapUserControl();
76+
_networkMapUserControl = new NetworkMapUserControl(_networkContext);
7777
_networkMapUserControl.Dock = DockStyle.Fill;
78-
_sessionsExplorerUserControl = new SessionsExplorerUserControl();
78+
_sessionsExplorerUserControl = new SessionsExplorerUserControl(_networkContext);
7979
_sessionsExplorerUserControl.Dock = DockStyle.Fill;
8080
_hashesUserControl = new HashesUserControl();
8181
_hashesUserControl.Dock = DockStyle.Fill;
@@ -134,7 +134,13 @@ private void HandleFailedFiles()
134134
}
135135
}
136136

137-
private void OnSessionArived(TransportLayerSession session)
137+
private void OnSessionArived(PcapProcessor.TcpSession session)
138+
{
139+
_sessionsExplorerUserControl.AddSession(session);
140+
this.modulesTreeView.Nodes["NetworkNode"].Nodes["SessionsNode"].Text = $"Sessions ({_sessionsExplorerUserControl.SessionsCount})";
141+
}
142+
143+
private void OnSessionArived(PcapProcessor.UdpSession session)
138144
{
139145
_sessionsExplorerUserControl.AddSession(session);
140146
this.modulesTreeView.Nodes["NetworkNode"].Nodes["SessionsNode"].Text = $"Sessions ({_sessionsExplorerUserControl.SessionsCount})";
@@ -219,7 +225,7 @@ private void OnParsedItemDetected(object sender, PcapAnalyzer.ParsedItemDetected
219225
else if (e.ParsedItem is PcapAnalyzer.NetworkConnection)
220226
{
221227
var connection = e.ParsedItem as PcapAnalyzer.NetworkConnection;
222-
_connections.Add(connection);
228+
_networkContext.HandleNetworkConection(connection);
223229
_networkMapUserControl.AddEdge(connection.Source, connection.Destination);
224230
this.modulesTreeView.Nodes["NetworkNode"].Nodes["NetworkMapNode"].Text = $"Network Map ({_networkMapUserControl.NodesCount})";
225231
}
@@ -386,7 +392,7 @@ private void BuildUdpSessionsCheckBox_CheckedChanged(object sender, EventArgs e)
386392

387393
private void MessageOnBuildSessionsConfigurationChanged()
388394
{
389-
ShowInfoMessageBox(@"NOTE, Disabling sessions reconstruction means that BruteShark will not analyze full sessions,
395+
Utilities.ShowInfoMessageBox(@"NOTE, Disabling sessions reconstruction means that BruteShark will not analyze full sessions,
390396
This means a faster processing but also that some obects may not be extracted.");
391397
}
392398

@@ -427,27 +433,14 @@ private async void StartLiveCaptureAsync()
427433
// We wait here until the sniffing will be stoped (by the stop button).
428434
this.progressBar.CustomText = string.Empty;
429435
this.progressBar.Refresh();
430-
ShowInfoMessageBox("Capture Stoped");
436+
Utilities.ShowInfoMessageBox("Capture Stoped");
431437
}
432438

433439
private void StopCaptureButton_Click(object sender, EventArgs e)
434440
{
435441
_cts.Cancel();
436442
}
437443

438-
private void ShowInfoMessageBox(string text)
439-
{
440-
// NOTE: Info message box is also set up at front of the form, it solves the
441-
// problem of message box that is hidden under the form.
442-
MessageBox.Show(
443-
text: text,
444-
caption: "Info",
445-
buttons: MessageBoxButtons.OK,
446-
icon: MessageBoxIcon.Information,
447-
defaultButton: MessageBoxDefaultButton.Button1,
448-
options: MessageBoxOptions.DefaultDesktopOnly);
449-
}
450-
451444
private void promiscuousCheckBox_CheckStateChanged(object sender, EventArgs e)
452445
{
453446
if (promiscuousCheckBox.CheckState == CheckState.Checked)
@@ -485,8 +478,9 @@ private void exportResutlsButton_Click(object sender, EventArgs e)
485478
this.progressBar.CustomText = $"Exporting results to output folder: {outputDirectoryPath}...";
486479
this.progressBar.Refresh();
487480
CommonUi.Exporting.ExportFiles(outputDirectoryPath, _filesUserControl.Files);
488-
CommonUi.Exporting.ExportNetworkMap(outputDirectoryPath, _connections);
481+
CommonUi.Exporting.ExportNetworkMap(outputDirectoryPath, _networkContext.Connections);
489482
CommonUi.Exporting.ExportVoipCalls(outputDirectoryPath, _voipCallsUserControl.VoipCalls);
483+
CommonUi.Exporting.ExportNetworkNodesData(outputDirectoryPath, _networkContext.GetAllNodes());
490484
this.progressBar.CustomText = string.Empty;
491485

492486
MessageBox.Show($"Successfully exported results");
@@ -501,14 +495,14 @@ private void exportResutlsButton_Click(object sender, EventArgs e)
501495

502496
private void clearResutlsButton_Click(object sender, EventArgs e)
503497
{
504-
_connections = new HashSet<PcapAnalyzer.NetworkConnection>();
498+
_networkContext = new CommonUi.NetworkContext();
505499
_analyzer.Clear();
506500

507501
// Clear all modules user controls by recreating them.
508502
InitilizeModulesUserControls();
509503

510504
// Remove the items count of each module from the tree view (e.g "DNS (13)" -> "DNS").
511-
foreach (var node in IterateAllNodes(modulesTreeView.Nodes))
505+
foreach (var node in Utilities.IterateAllNodes(modulesTreeView.Nodes))
512506
{
513507
var index = node.Text.LastIndexOf('(');
514508

@@ -517,18 +511,9 @@ private void clearResutlsButton_Click(object sender, EventArgs e)
517511
node.Text = node.Text.Substring(0, index);
518512
}
519513
}
520-
}
521-
522-
IEnumerable<TreeNode> IterateAllNodes(TreeNodeCollection nodes)
523-
{
524-
// Recursively iterate over all nodes and sub nodes.
525-
foreach (TreeNode node in nodes)
526-
{
527-
yield return node;
528514

529-
foreach (var child in IterateAllNodes(node.Nodes))
530-
yield return child;
531-
}
515+
// Select the head of the modules tree view to force refreshing of the current user control.
516+
modulesTreeView.SelectedNode = modulesTreeView.Nodes[0];
532517
}
533518

534519
}

BruteShark/BruteSharkDesktop/UserControls/NetworkMapControl/NetworkMapUserControl.Designer.cs

Lines changed: 38 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)