Skip to content

Commit

Permalink
Gfs/gui fixes (#188)
Browse files Browse the repository at this point in the history
* Add more indices to improve GUI performance

* Fix #185 

* Fix #175
 
* Fix #183

* Fix #180

* Fix #177

* Fix #176

* Fix #174

* Fix #186

* Fix #179
  • Loading branch information
gfs authored May 2, 2019
1 parent b1074dd commit bcf698a
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 64 deletions.
52 changes: 31 additions & 21 deletions Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,14 @@ private static int RunExportCollectCommand(ExportCollectCommandOptions opts)
serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
Log.Debug("{0} RunExportCollectCommand", Strings.Get("End"));
string path = Path.Combine(opts.OutputPath, Helpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId + "_summary.json.txt"));

var output = new Dictionary<string, Object>();
output["results"] = results;
output["metadata"] = Helpers.GenerateMetadata();
using (StreamWriter sw = new StreamWriter(path)) //lgtm[cs/path-injection]
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, results);
serializer.Serialize(writer, output);
}
}
Log.Information(Strings.Get("OutputWrittenTo"), path);
Expand Down Expand Up @@ -495,7 +497,6 @@ public static void WriteScanJson(int ResultType, string BaseId, string CompareId
}
}
}
Log.Information("Halfway");
if (ChangeType == CHANGE_TYPE.DELETED || ChangeType == CHANGE_TYPE.MODIFIED)
{
inner_cmd.Parameters.Clear();
Expand Down Expand Up @@ -580,24 +581,28 @@ public static void WriteScanJson(int ResultType, string BaseId, string CompareId
//telemetry.GetMetric("ResultsExported").TrackValue(records.Count);

serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

var o = new Dictionary<string, Object>();
o["results"] = records;
o["metadata"] = Helpers.GenerateMetadata();
using (StreamWriter sw = new StreamWriter(Path.Combine(OutputPath, Helpers.MakeValidFileName(BaseId + "_vs_" + CompareId + "_" + ExportType.ToString() + ".json.txt")))) //lgtm[cs/path-injection]
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, records);
serializer.Serialize(writer, o);
}
}
}
}

serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

var output = new Dictionary<string, Object>();
output["results"] = actualExported;
output["metadata"] = Helpers.GenerateMetadata();
using (StreamWriter sw = new StreamWriter(Path.Combine(OutputPath, Helpers.MakeValidFileName(BaseId + "_vs_" + CompareId + "_summary.json.txt")))) //lgtm[cs/path-injection]
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, actualExported);
serializer.Serialize(writer, output);
}
}

Expand Down Expand Up @@ -648,17 +653,20 @@ public static void WriteMonitorJson(string RunId, int ResultType, string OutputP
string GET_SERIALIZED_RESULTS = "select change_type, Serialized from file_system_monitored where run_id = @run_id";


var cmd = new SqliteCommand(GET_SERIALIZED_RESULTS, DatabaseManager.Connection);
cmd.Parameters.AddWithValue("@run_id", RunId);
using (var reader = cmd.ExecuteReader())
using (var cmd = new SqliteCommand(GET_SERIALIZED_RESULTS, DatabaseManager.Connection, DatabaseManager.Transaction))
{
FileMonitorEvent obj;

while (reader.Read())
cmd.Parameters.AddWithValue("@run_id", RunId);
using (var reader = cmd.ExecuteReader())
{
obj = JsonConvert.DeserializeObject<FileMonitorEvent>(reader["serialized"].ToString());
obj.ChangeType = (CHANGE_TYPE)int.Parse(reader["change_type"].ToString());
records.Add(obj);

FileMonitorEvent obj;

while (reader.Read())
{
obj = JsonConvert.DeserializeObject<FileMonitorEvent>(reader["serialized"].ToString());
obj.ChangeType = (CHANGE_TYPE)int.Parse(reader["change_type"].ToString());
records.Add(obj);
}
}
}

Expand All @@ -667,16 +675,17 @@ public static void WriteMonitorJson(string RunId, int ResultType, string OutputP
settings.NullValueHandling = NullValueHandling.Ignore;
JsonSerializer serializer = JsonSerializer.Create(settings);
serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

var output = new Dictionary<string, Object>();
output["results"] = records;
output["metadata"] = Helpers.GenerateMetadata();
string path = Path.Combine(OutputPath, Helpers.MakeValidFileName(RunId + "_Monitoring_" + ((RESULT_TYPE)ResultType).ToString() + ".json.txt"));

using (StreamWriter sw = new StreamWriter(path)) //lgtm[cs/path-injection]
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, records);
serializer.Serialize(writer, output);
}
}

Log.Information(Strings.Get("OutputWrittenTo"), path);

}
Expand All @@ -690,6 +699,7 @@ private static int RunMonitorCommand(MonitorCommandOptions opts)
#endif
AdminOrQuit();
Filter.LoadFilters(opts.FilterLocation);
opts.RunId = opts.RunId.Trim();
if (opts.RunId.Equals("Timestamp"))
{
opts.RunId = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Expand Down Expand Up @@ -1160,7 +1170,7 @@ public static int RunCollectCommand(CollectCommandOptions opts)
int returnValue = (int)ERRORS.NONE;
AdminOrQuit();
DatabaseManager.VerifySchemaVersion();

opts.RunId = opts.RunId.Trim();
Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Files", opts.EnableAllCollectors ? "True" : opts.EnableFileSystemCollector.ToString());
StartEvent.Add("Ports", opts.EnableAllCollectors ? "True" : opts.EnableNetworkPortCollector.ToString());
Expand Down
18 changes: 9 additions & 9 deletions Gui/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ public ActionResult GetResults(string BaseId, string CompareId, int ResultType,
result_count = int.Parse(reader["count(*)"].ToString());
}
}

output["Results"] = results;
output["TotalCount"] = result_count;
output["Offset"] = Offset;
output["Requested"] = NumResults;
output["Actual"] = results.Count;
return Json(JsonConvert.SerializeObject(output));
}

output["Results"] = results;
output["TotalCount"] = result_count;
output["Offset"] = Offset;
output["Requested"] = NumResults;
output["Actual"] = results.Count;
return Json(JsonConvert.SerializeObject(output));

}


Expand Down Expand Up @@ -326,7 +326,7 @@ public ActionResult GetComparators()
public ActionResult StartCollection(string Id, bool File, bool Port, bool Service, bool User, bool Registry, bool Certificates)
{
CollectCommandOptions opts = new CollectCommandOptions();
opts.RunId = Id;
opts.RunId = Id.Trim();
opts.EnableFileSystemCollector = File;
opts.EnableNetworkPortCollector = Port;
opts.EnableServiceCollector = Service;
Expand Down Expand Up @@ -381,7 +381,7 @@ public ActionResult StartMonitoring(string RunId, string Directory, string Exten

using (var cmd = new SqliteCommand(INSERT_RUN, DatabaseManager.Connection, DatabaseManager.Transaction))
{
cmd.Parameters.AddWithValue("@run_id", RunId);
cmd.Parameters.AddWithValue("@run_id", RunId.Trim());
cmd.Parameters.AddWithValue("@file_system", true);
cmd.Parameters.AddWithValue("@ports", false);
cmd.Parameters.AddWithValue("@users", false);
Expand Down
12 changes: 8 additions & 4 deletions Gui/Views/Home/Analyze.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="ExportQuantity" id="ExportAll" value="1" disabled>
<input class="form-check-input" type="radio" name="ExportQuantity" id="ExportAll" value="1" disabled selected>
<label class="form-check-label" for="ExportAll">
@Strings.Get("ExportAll")
</label>
Expand All @@ -172,6 +172,7 @@
<table class="table table-striped table-sm files results" id="FileResultsTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">@Strings.Get("ChangeType")</th>
<th scope="col">@Strings.Get("Path")")</th>
<th scope="col">@Strings.Get("Permissions")</th>
Expand All @@ -183,6 +184,7 @@
<table class="table table-hover table-sm ports results" id="PortResultsTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">@Strings.Get("ChangeType")</th>
<th scope="col">@Strings.Get("Port")</th>
<th scope="col">@Strings.Get("Type")</th>
Expand All @@ -194,17 +196,19 @@
<table class="table table-hover table-sm users results" id="UserResultsTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">@Strings.Get("ChangeType")</th>
<th scope="col">@Strings.Get("Account Type")</th>
<th scope="col">@Strings.Get("Name")</th>
<th scope="col"> @Strings.Get("Description")</th>
<th scope="col">@Strings.Get("Description")</th>
</tr>
</thead>
<tbody id="UserResultsTableBody"></tbody>
</table>
<table class="table table-striped table-sm certificates results" id="CertificateResultsTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">@Strings.Get("ChangeType")</th>
<th scope="col">@Strings.Get("Store Location")</th>
<th scope="col">@Strings.Get("Store Name")</th>
Expand All @@ -217,6 +221,7 @@
<table class="table table-striped table-sm services results" id="ServiceResultsTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">@Strings.Get("ChangeType")</th>
<th scope="col">@Strings.Get("Service Name")</th>
<th scope="col">@Strings.Get("Start Type")</th>
Expand All @@ -229,10 +234,9 @@
<table class="table table-striped table-sm registry results" id="RegistryResultsTable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">@Strings.Get("ChangeType")</th>
<th scope="col">@Strings.Get("Path")</th>
<th scope="col">@Strings.Get("Value")</th>
<th scope="col"> @Strings.Get("Contents")</th>
</tr>
</thead>
<tbody id="RegistryResultsTableBody"></tbody>
Expand Down
2 changes: 1 addition & 1 deletion Gui/Views/Home/Collect.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@


<div class="col-6" id="RunStatus">
<div class="row font-weight-bold">
<div class="row font-weight-bold top-margin">
<div class="col">
@Strings.Get("RunStatus")
</div>
Expand Down
2 changes: 1 addition & 1 deletion Gui/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<div class="row">
<div class="col-2 left-col">2.0-preview</div>
<div class="col-8">Rewrite of ASA with new UI and support for Mac OS and Linux</div>
<div class="col-2 text-right">2/28/19</div>
<div class="col-2 text-right">5/6/19</div>
</div>
<div class="spacer"></div>
<div id="moreInfo">
Expand Down
2 changes: 1 addition & 1 deletion Gui/wwwroot/American-English.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"en-US": {
"%NoDifference": "No difference detected.",
"%Error.NoDifference": "No difference detected.",
"%NoDirSpecified": "ERROR: Must specify a directory to watch.",
"%PathInvalid": "Provided path is invalid.",
"%UniqueId": "Must supply unique Run Id",
Expand Down
26 changes: 7 additions & 19 deletions Gui/wwwroot/js/Results.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var ResultTypeGroup = $('input[type=radio][name=ResultType]');
ResultTypeGroup.change(function () {
$('.results').hide();
resultOffset = 0;
$("#ExportResultsButton").attr('disabled', false);
$("#ExportSelection").attr('disabled', false);
GetResults($('input[name=ResultType]:checked').val(), resultOffset, 100);
switch (parseInt(ResultTypeGroup.filter(':checked').val())){
case RESULT_TYPE.PORT:
Expand All @@ -58,11 +58,6 @@ ResultTypeGroup.change(function () {
}
});

var ExportQuantityGroup = $('input[type=radio][name=ExportQuantity]');
ExportQuantityGroup.change(function () {
$("#ExportResultsButton").attr('disabled', false);
})



$('#SelectedBaseRunId').change(function () { ResetResults(); } );
Expand Down Expand Up @@ -187,7 +182,7 @@ function GetResultTypes() {
if ((result.File || result.Port || result.Certificate || result.Service || result.Registry || result.User) == false) {
SetStatus("The two runs selected have no common collectors.");
} else {
$('input[name=ExportQuantity]').prop('disabled', false);
$("#ExportResultsButton").attr('disabled', false);
}
$('#FileRadio').attr('disabled', (result.File) ? false : true);
$('#PortRadio').attr('disabled', (result.Port) ? false : true);
Expand Down Expand Up @@ -360,6 +355,7 @@ function InsertIntoTable(result) {
}

function InsertIntoRegistryTable(result) {
var appendObj;
if (result.ChangeType == CHANGE_TYPE.CREATED) {
appendObj = result.SerializedCompare;
}
Expand All @@ -381,22 +377,14 @@ function InsertIntoRegistryTable(result) {
});
caretContainer.append(caret);
arrowTD.append(caretContainer);
tmp.append(arrowTD);
tmp.append(arrowTD);
tmp.append($('<td/>', {
scope: "col",
html: ChangeTypeToString(result.ChangeType)
}));
tmp.append($('<td/>', {
scope: "col",
html: appendObj.Path
}));
tmp.append($('<td/>', {
scope: "col",
html: JSON.stringify(appendObj.Subkeys)
}));
tmp.append($('<td/>', {
scope: "col",
html: JSON.stringify(appendObj.Values)
html: appendObj.Key
}));
$('#RegistryResultsTableBody').append(tmp);
tmp = $('<tr/>');
Expand All @@ -413,8 +401,8 @@ function InsertIntoRegistryTable(result) {
var tmp3 = $('<tr/>');
var tmp4 = $('<td/>', { html: prop });
var tmp5 = $('<td/>', { html: appendObj[prop] });
if (result.ChangeType == CHANGE_TYPE.MODIFIED){
var tmp6 = $('<td/>', { html: result.SerializedCompare.prop });
if (result.ChangeType == CHANGE_TYPE.MODIFIED) {
var tmp6 = $('<td/>', { html: result.SerializedCompare[prop] });
}
tmp3.append(tmp4);
tmp3.append(tmp5);
Expand Down
8 changes: 0 additions & 8 deletions Lib/Objects/RegistryObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ public class RegistryObject
public List<string> Subkeys = new List<string>();
public string Permissions = "";

public string RowKey
{
get
{
return CryptoHelpers.CreateHash(this.ToString());
}
}

private static List<string> GetSubkeys(RegistryKey key)
{
return new List<string>(key.GetSubKeyNames());
Expand Down
8 changes: 8 additions & 0 deletions Lib/Utils/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static class DatabaseManager
private static readonly string SQL_CREATE_CERTIFICATES_RUN_ID_INDEX = "create index if not exists i_certificates_run_id on certificates(run_id)";

private static readonly string SQL_CREATE_FILE_COMBINED_INDEX = "create index if not exists i_filesystem_row_run_combined on file_system(run_id, row_key)";
private static readonly string SQL_CREATE_REGISTRY_COMBINED_INDEX = "create index if not exists i_registry_row_run_combined on registry(run_id, row_key)";
private static readonly string SQL_CREATE_COMPARED_COMBINED_INDEX = "create index if not exists i_compared_base_compare_data_combined on compared(base_run_id, compare_run_id, data_type)";

private static readonly string SQL_CREATE_ANALYZED_TABLE = "create table if not exists results (base_run_id text, compare_run_id text, status int)";

Expand Down Expand Up @@ -183,6 +185,12 @@ public static bool Setup()

cmd.CommandText = SQL_CREATE_FILE_COMBINED_INDEX;
cmd.ExecuteNonQuery();

cmd.CommandText = SQL_CREATE_REGISTRY_COMBINED_INDEX;
cmd.ExecuteNonQuery();

cmd.CommandText = SQL_CREATE_COMPARED_COMBINED_INDEX;
cmd.ExecuteNonQuery();
}

DatabaseManager.Transaction.Commit();
Expand Down
12 changes: 12 additions & 0 deletions Lib/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.InteropServices;
using System.Reflection;
using AttackSurfaceAnalyzer.ObjectTypes;
using System.Collections.Generic;

namespace AttackSurfaceAnalyzer.Utils
{
Expand Down Expand Up @@ -105,5 +106,16 @@ public static string GetOsName()
}
return "";
}

public static Dictionary<string,string> GenerateMetadata()
{
var dict = new Dictionary<string, string>();

dict["version"] = GetVersionString();
dict["os"] = GetOsName();
dict["osversion"] = GetOsVersion();

return dict;
}
}
}

0 comments on commit bcf698a

Please sign in to comment.