Skip to content

Commit

Permalink
Merge pull request #519 from wh1tenoise/master
Browse files Browse the repository at this point in the history
Improvements on the asynchronous code. Looks good on my end, nice work!
  • Loading branch information
mattgrandy authored Feb 3, 2021
2 parents 7102a86 + 2934133 commit 95c49d6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
48 changes: 27 additions & 21 deletions CS/EyeWitness/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,54 +147,54 @@ static void DictMaker()

private static async Task ScreenshotSender(WitnessedServer obj, int timeDelay)
{
//Cancel after 30s
var cts = new CancellationTokenSource(timeDelay);
cts.CancelAfter(timeDelay);
try
{
//Keep it syncronous for this slow version
//Allow the thread to exit somewhat cleanly before exiting the semaphore
_pool.WaitOne(40000);

_pool.WaitOne();
//Cancel after timeDelay
var cts = new CancellationTokenSource(timeDelay);
Console.WriteLine("Grabbing screenshot for: " + obj.remoteSystem);
var task = await obj.RunWithTimeoutCancellation(cts.Token);

_pool.Release();
}
catch (OperationCanceledException)
catch (OperationCanceledException e)
{
Console.WriteLine("[-] Thread aborted while grabbing screenshot for: " + obj.remoteSystem);
Console.WriteLine($"[-] Thread aborted while grabbing screenshot for: {obj.remoteSystem} - {e.Message}");
}
catch (SemaphoreFullException)
{
//return;
}
finally
{
_pool.Release();
}
}

private static async Task SourceSender(WitnessedServer obj)
{
//Cancel after 10s
//This cancellation time isn't as important as the screenshot one so we can hard code it
var cts = new CancellationTokenSource(10000);
cts.CancelAfter(10000);

try
{
await _Sourcepool.WaitAsync(10000);
await _Sourcepool.WaitAsync();
//Cancel after 10s
//This cancellation time isn't as important as the screenshot one so we can hard code it
var cts = new CancellationTokenSource(10000);
Console.WriteLine("Grabbing source of: " + obj.remoteSystem);
await obj.SourcerAsync(cts.Token);
obj.CheckCreds(categoryDict, signatureDict);

_Sourcepool.Release();
}
catch (OperationCanceledException)
catch (OperationCanceledException e)
{
Console.WriteLine("[-] Thread aborted while grabbing source for: " + obj.remoteSystem);
Console.WriteLine($"[-] Thread aborted while grabbing source for: {obj.remoteSystem} - {e.Message}");
}
catch (SemaphoreFullException)
{
//return;
}
finally
{
_Sourcepool.Release();
}
}

public static void CategoryCounter(WitnessedServer[] urlArray, Dictionary<string, string> catDict)
Expand All @@ -205,7 +205,7 @@ public static void CategoryCounter(WitnessedServer[] urlArray, Dictionary<string
if (categoryRankDict.ContainsKey(urlObject.systemCategory))
{
categoryRankDict[urlObject.systemCategory][1] = (int)categoryRankDict[urlObject.systemCategory][1] + 1;
}
}
}
}

Expand Down Expand Up @@ -469,7 +469,13 @@ static void Main(string[] args)
int arrayPosition = 0;
foreach (var url in allUrls)
{
WitnessedServer singleSite = new WitnessedServer(url);
Uri uriResult;
if(!(Uri.TryCreate(url, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)))
{
Uri.TryCreate($"http://{url}", UriKind.Absolute, out uriResult);
}

WitnessedServer singleSite = new WitnessedServer(uriResult.AbsoluteUri);
serverArray[arrayPosition] = singleSite;
arrayPosition++;

Expand Down
65 changes: 31 additions & 34 deletions CS/EyeWitness/WitnessedServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Security;
using System.ComponentModel;
using System.Threading.Tasks;

namespace EyeWitness
Expand Down Expand Up @@ -112,7 +111,6 @@ private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventAr
catch(ThreadAbortException)
{
Console.WriteLine("Error aborting thread, returning");
browser.Dispose();
return;
}
finally
Expand Down Expand Up @@ -209,49 +207,49 @@ public async Task<String> RunWithTimeoutCancellation(CancellationToken cancellat

Thread workerThread = new Thread(delegate ()
{
try
{
//Create bounds the same size as the screen
Rectangle bounds = Screen.PrimaryScreen.Bounds;
//Create bounds the same size as the screen
Rectangle bounds = Screen.PrimaryScreen.Bounds;

//Don't care about TLS issues
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback
(
delegate { return true; }
);
using (WebBrowser br = new WebBrowser())
//Don't care about TLS issues
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback
(
delegate { return true; }
);
using (WebBrowser br = new WebBrowser())
{
try
{
br.Width = bounds.Width;
br.Height = bounds.Height;
br.ScriptErrorsSuppressed = true;
br.ScrollBarsEnabled = false;


br.Navigate(remoteSystem);

br.Visible = false;
br.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
br.NewWindow += new System.ComponentModel.CancelEventHandler(WinFormBrowser_NewWindow);


br.Navigate(remoteSystem);

while (br.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
//Application.Run();
cancellationToken.ThrowIfCancellationRequested();
}
}
catch { }
finally
{
if(!br.IsDisposed)
{
br.Dispose();
}
}
}
catch
{
return;
}

});
workerThread.SetApartmentState(ApartmentState.STA);
await Task.Run(() =>
{
workerThread.Start();
bool finished = workerThread.Join(30000);
bool finished = workerThread.Join(Timeout.Infinite);
if (!finished)
try
{
Expand Down Expand Up @@ -282,14 +280,11 @@ public async Task<String> SourcerAsync(CancellationToken cancellationToken)
// Capture source code and headers
ServicePointManager.Expect100Continue = true;
// fix for allowing tls12
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
await Task.Run(async() =>
{
using (WebClient witnessClient = new WebClient())
{
// Instantiate the CancellationTokenSource.
//var taskCompletionSource = new TaskCompletionSource<bool>();
//cts.CancelAfter(20000);

try
{
Expand All @@ -298,25 +293,27 @@ await Task.Run(async() =>
delegate { return true; }
);
// Uri test = Uri.Parse(remoteSystem);
sourceCode = witnessClient.DownloadString(remoteSystem);
cancellationToken.Register(witnessClient.CancelAsync);
sourceCode = await witnessClient.DownloadStringTaskAsync(remoteSystem);
cancellationToken.ThrowIfCancellationRequested();
headers = witnessClient.ResponseHeaders.ToString();
webpageTitle = Regex.Match(sourceCode, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>",
RegexOptions.IgnoreCase).Groups["Title"].Value;
File.WriteAllText(Program.witnessDir + "\\src\\" + urlSaveName + ".txt", sourceCode);
File.WriteAllText(Program.witnessDir + "\\headers\\" + urlSaveName + ".txt", headers);
witnessClient.Dispose();
return;
}

catch (Exception e)
{
//Console.WriteLine(e);
Console.WriteLine("[*] Offline Server - " + remoteSystem);
Console.WriteLine($"[*] Offline Server - {remoteSystem} - {e.Message}");
errorState = "offline";
systemCategory = "offline";
webpageTitle = "Server Offline";
headers = "Server Offline";
return;
}
finally
{
witnessClient.Dispose();
}
}
}, cancellationToken);
Expand Down

0 comments on commit 95c49d6

Please sign in to comment.