Skip to content

Commit

Permalink
Merge pull request #76 from odedshimon/refactor-common-ui-code
Browse files Browse the repository at this point in the history
Refactor common UI code
  • Loading branch information
odedshimon authored Jan 25, 2021
2 parents d99533c + f64131a commit 2022b25
Show file tree
Hide file tree
Showing 24 changed files with 330 additions and 442 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# Ignore Visual Studio launchSettings (e.g application arguments)
**/Properties/launchSettings.json
58 changes: 4 additions & 54 deletions BruteShark/BruteSharkCli/BruteSharkCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public BruteSharkCli(string[] args)
_processor.BuildUdpSessions = true;

// Contract the events.
_processor.UdpPacketArived += (s, e) => _analyzer.Analyze(CastProcessorUdpPacketToAnalyzerUdpPacket(e.Packet));
_processor.TcpPacketArived += (s, e) => _analyzer.Analyze(CastProcessorTcpPacketToAnalyzerTcpPacket(e.Packet));
_processor.TcpSessionArrived += (s, e) => _analyzer.Analyze(CastProcessorTcpSessionToAnalyzerTcpSession(e.TcpSession));
_processor.UdpSessionArrived += (s, e) => _analyzer.Analyze(CastProcessorUdpStreamToAnalyzerUdpStream(e.UdpSession));
_processor.UdpPacketArived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorUdpPacketToAnalyzerUdpPacket(e.Packet));
_processor.TcpPacketArived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorTcpPacketToAnalyzerTcpPacket(e.Packet));
_processor.TcpSessionArrived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorTcpSessionToAnalyzerTcpSession(e.TcpSession));
_processor.UdpSessionArrived += (s, e) => _analyzer.Analyze(CommonUi.Casting.CastProcessorUdpStreamToAnalyzerUdpStream(e.UdpSession));
}

private void RunShellMode()
Expand All @@ -52,56 +52,6 @@ private void RunSingleCommand()
}
}

public static PcapAnalyzer.UdpPacket CastProcessorUdpPacketToAnalyzerUdpPacket(PcapProcessor.UdpPacket udpPacket)
{
return new PcapAnalyzer.UdpPacket()
{
SourceIp = udpPacket.SourceIp,
DestinationIp = udpPacket.DestinationIp,
SourcePort = udpPacket.SourcePort,
DestinationPort = udpPacket.DestinationPort,
Data = udpPacket.Data
};
}

private PcapAnalyzer.TcpPacket CastProcessorTcpPacketToAnalyzerTcpPacket(PcapProcessor.TcpPacket tcpPacket)
{
return new PcapAnalyzer.TcpPacket()
{
SourceIp = tcpPacket.SourceIp,
DestinationIp = tcpPacket.DestinationIp,
SourcePort = tcpPacket.SourcePort,
DestinationPort = tcpPacket.DestinationPort,
Data = tcpPacket.Data
};
}

private PcapAnalyzer.TcpSession CastProcessorTcpSessionToAnalyzerTcpSession(PcapProcessor.TcpSession tcpSession)
{
return new PcapAnalyzer.TcpSession()
{
SourceIp = tcpSession.SourceIp,
DestinationIp = tcpSession.DestinationIp,
SourcePort = tcpSession.SourcePort,
DestinationPort = tcpSession.DestinationPort,
Data = tcpSession.Data,
Packets = tcpSession.Packets.Select(p => CastProcessorTcpPacketToAnalyzerTcpPacket(p)).ToList()
};
}

private PcapAnalyzer.UdpStream CastProcessorUdpStreamToAnalyzerUdpStream(PcapProcessor.UdpSession udpStream)
{
return new PcapAnalyzer.UdpStream()
{
SourceIp = udpStream.SourceIp,
DestinationIp = udpStream.DestinationIp,
SourcePort = udpStream.SourcePort,
DestinationPort = udpStream.DestinationPort,
Data = udpStream.Data,
Packets = udpStream.Packets.Select(p => CastProcessorUdpPacketToAnalyzerUdpPacket(p)).ToList()
};
}

internal void Start()
{
if (_args.Length == 0)
Expand Down
1 change: 1 addition & 0 deletions BruteShark/BruteSharkCli/BruteSharkCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BruteForce\BruteForce.csproj" />
<ProjectReference Include="..\CommonUi\CommonUi.csproj" />
<ProjectReference Include="..\PcapAnalyzer\PcapAnalyzer.csproj" />
<ProjectReference Include="..\PcapProcessor\PcapProcessor.csproj" />
</ItemGroup>
Expand Down
103 changes: 0 additions & 103 deletions BruteShark/BruteSharkCli/Casting.cs

This file was deleted.

4 changes: 2 additions & 2 deletions BruteShark/BruteSharkCli/Cli Shell/CliShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public CliShell(PcapAnalyzer.Analyzer analyzer, PcapProcessor.Processor processo
AddCommand(new CliShellCommand("show-hashes", p => PrintHashes(), "Print Hashes"));
AddCommand(new CliShellCommand("show-networkmap", p => PrintNetworkMap(), "Prints the network map as a json string. Usage: show-networkmap"));
AddCommand(new CliShellCommand("export-hashes", p => Utilities.ExportHashes(p, _hashes), "Export all Hashes to Hascat format input files. Usage: export-hashes <OUTPUT-DIRECTORY>"));
AddCommand(new CliShellCommand("export-networkmap", p => Utilities.ExportNetworkMap(p, _connections), "Export network map to a json file for neo4j. Usage: export-networkmap <OUTPUT-file>"));
AddCommand(new CliShellCommand("export-networkmap", p => CommonUi.Exporting.ExportNetworkMap(p, _connections), "Export network map to a json file for neo4j. Usage: export-networkmap <OUTPUT-file>"));

// Add the help command
this.AddCommand(new CliShellCommand(
Expand Down Expand Up @@ -165,7 +165,7 @@ private void PrintHashes()

private void PrintNetworkMap()
{
Console.WriteLine(NetwrokMapJsonExporter.GetNetworkMapAsJsonString(this._connections.ToList()));
Console.WriteLine(CommonUi.Exporting.GetNetworkMapAsJsonString(this._connections));
}

private void StartAnalyzing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class SingleCommandRunner
{
private SingleCommandFlags _cliFlags;
private List<string> _files;

private HashSet<PcapAnalyzer.NetworkFile> _extractedFiles;
private HashSet<PcapAnalyzer.NetworkPassword> _passwords;
private HashSet<PcapAnalyzer.NetworkHash> _hashes;
private HashSet<PcapAnalyzer.NetworkConnection> _connections;

private PcapProcessor.Processor _processor;
private PcapAnalyzer.Analyzer _analyzer;

Expand All @@ -29,11 +32,12 @@ public SingleCommandRunner(Analyzer analyzer, Processor processor, string[] args
{
_analyzer = analyzer;
_processor = processor;

_files = new List<string>();

_hashes = new HashSet<NetworkHash>();
_connections = new HashSet<PcapAnalyzer.NetworkConnection>();
_passwords = new HashSet<NetworkPassword>();
_files = new List<string>();
_extractedFiles = new HashSet<NetworkFile>();

_analyzer.ParsedItemDetected += OnParsedItemDetected;
_processor.ProcessingFinished += (s, e) => this.ExportResults();
Expand Down Expand Up @@ -157,15 +161,17 @@ private void ExportResults()
{
if (moduleName.Contains("NetworkMap"))
{
Utilities.ExportNetworkMap(_cliFlags.OutputDir, _connections);
var filePath = CommonUi.Exporting.ExportNetworkMap(_cliFlags.OutputDir, _connections);
Console.WriteLine($"Successfully exported network map to json file: {filePath}");
}
else if (moduleName.Contains("Credentials"))
{
Utilities.ExportHashes(_cliFlags.OutputDir, _hashes);
}
else if (moduleName.Contains("FileExtracting"))
{
// Todo - extract files to output
var dirPath = CommonUi.Exporting.ExportFiles(_cliFlags.OutputDir, _extractedFiles);
Console.WriteLine($"Successfully exported extracted files to: {dirPath}");
}
// Todo - add exporting of dns module results
}
Expand Down Expand Up @@ -195,14 +201,21 @@ private void OnParsedItemDetected(object sender, ParsedItemDetectedEventArgs e)
PrintDetectedItem(e.ParsedItem);
}
}
if (e.ParsedItem is PcapAnalyzer.NetworkHash)
else if (e.ParsedItem is PcapAnalyzer.NetworkHash)
{
if (_hashes.Add(e.ParsedItem as PcapAnalyzer.NetworkHash))
{
PrintDetectedItem(e.ParsedItem);
}
}
if (e.ParsedItem is PcapAnalyzer.NetworkConnection)
else if (e.ParsedItem is PcapAnalyzer.NetworkFile)
{
if (_extractedFiles.Add(e.ParsedItem as PcapAnalyzer.NetworkFile))
{
PrintDetectedItem(e.ParsedItem);
}
}
else if (e.ParsedItem is PcapAnalyzer.NetworkConnection)
{
var networkConnection = e.ParsedItem as NetworkConnection;
_connections.Add(networkConnection);
Expand Down
35 changes: 6 additions & 29 deletions BruteShark/BruteSharkCli/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static DataTable ToDataTable<T>(this IEnumerable<T> items, int itemLength
return dataTable;
}


public static void PrintBruteSharkAsciiArt()
{
var bruteSharkAscii =@"
Expand All @@ -67,23 +66,23 @@ public static void PrintBruteSharkAsciiArt()

Console.WriteLine(bruteSharkAscii);
}

internal static void ExportHashes(string dirPath, HashSet<PcapAnalyzer.NetworkHash> hashes)
{
// Run on each Hash Type we found.
string hashesPath = Path.Combine(dirPath, "Hasehs");
Directory.CreateDirectory(hashesPath);

foreach (string hashType in hashes.Select(h => h.HashType).Distinct())
// Run on each Hash Type we found.
foreach (string hashType in hashes.Select(hash => hash.HashType).Distinct())
{
try
{
// Convert all hashes from that type to Hashcat format.

var hashesToExport = hashes.Where(h => (h as PcapAnalyzer.NetworkHash).HashType == hashType)
.Select(h => BruteForce.Utilities.ConvertToHashcatFormat(
Casting.CastAnalyzerHashToBruteForceHash(h)));
CommonUi.Casting.CastAnalyzerHashToBruteForceHash(h)));

var outputFilePath = MakeUnique(Path.Combine(hashesPath, $"Brute Shark - {hashType} Hashcat Export.txt"));
var outputFilePath = CommonUi.Exporting.GetUniqueFilePath(Path.Combine(hashesPath, $"Brute Shark - {hashType} Hashcat Export.txt"));

using (var streamWriter = new StreamWriter(outputFilePath, true))
{
Expand All @@ -97,33 +96,11 @@ internal static void ExportHashes(string dirPath, HashSet<PcapAnalyzer.NetworkHa
}
catch (Exception ex)
{
// in case Casting.CastAnalyzerHashToBruteForceHash(h))) fails and throws exception for not supported hash type
// In case Casting.CastAnalyzerHashToBruteForceHash(h) fails and throws exception for not supported hash type
continue;
}
}
}
public static string MakeUnique(string path)
{
string dir = Path.GetDirectoryName(path);
string fileName = Path.GetFileNameWithoutExtension(path);
string fileExt = Path.GetExtension(path);

for (int i = 1; ; ++i)
{
if (!File.Exists(path))
return new FileInfo(path).FullName;

path = Path.Combine(dir, fileName + " " + i + fileExt);
}
}
internal static void ExportNetworkMap(string dirPath, HashSet<PcapAnalyzer.NetworkConnection> connections)
{
string netowrkMapPath = Path.Combine(Path.Combine(dirPath, "NetworkMap"), "networkmap.json");
Directory.CreateDirectory(netowrkMapPath);

PcapAnalyzer.NetwrokMapJsonExporter.FileExport(connections.ToList<PcapAnalyzer.NetworkConnection>(), netowrkMapPath);

Console.WriteLine($"Successfully exported network map to json file: {netowrkMapPath}");
}
}
}
1 change: 1 addition & 0 deletions BruteShark/BruteSharkDesktop/BruteSharkDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<ProjectReference Include="..\BruteForce\BruteForce.csproj" />
<ProjectReference Include="..\CommonUi\CommonUi.csproj" />
<ProjectReference Include="..\PcapAnalyzer\PcapAnalyzer.csproj" />
<ProjectReference Include="..\PcapProcessor\PcapProcessor.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 2022b25

Please sign in to comment.