Skip to content

Commit

Permalink
[third_party/RemoteNET] ScubaDiver.API: Avoid HTTPClient race conditi…
Browse files Browse the repository at this point in the history
…on in multi-threaded contexts.
  • Loading branch information
Qonfused committed Dec 18, 2023
1 parent 2ae6c83 commit 0866f81
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions third_party/RemoteNET/src/ScubaDiver.API/DiverCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class DiverCommunicator

private int? _process_id = null;
private CallbacksListener _listener;
private static readonly object _listenerLock = new();
private readonly HttpClient httpClient = new();

public DiverCommunicator(string hostname, int diverPort)
{
Expand All @@ -47,7 +49,6 @@ private string SendRequest(
{
queryParams ??= new();

HttpClient httpClient = new();
NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty);
foreach (KeyValuePair<string, string> kvp in queryParams)
{
Expand All @@ -66,10 +67,13 @@ private string SendRequest(
msg.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
}

HttpResponseMessage res = httpClient.SendAsync(msg).Result;
string body = res.Content.ReadAsStringAsync().Result;
httpClient.Dispose();
HttpResponseMessage res = null;
lock (_listenerLock)
{
res = httpClient.SendAsync(msg).Result;
}

string body = res.Content.ReadAsStringAsync().Result;
if (body.StartsWith("{\"error\":", StringComparison.InvariantCultureIgnoreCase))
{
// Diver sent back an error. We parse it here and throwing a 'proxied' exception
Expand All @@ -91,16 +95,6 @@ public bool KillDiver()
return body?.Contains("Goodbye") ?? false;
}

public bool InjectDll(string path)
{
var res = SendRequest("inject", new Dictionary<string, string>()
{
{ "dll_path", path }
});

return res.Contains("dll loaded");
}

/// <summary>
/// Dumps the heap of the remote process
/// </summary>
Expand Down

0 comments on commit 0866f81

Please sign in to comment.