private void Form1_Load(object sender, EventArgs e) { checker.Enabled = false; heartbeat.Enabled = false; // Init Platform IP PlatformConnector.initPlatformIp(); // Register scan and get an API token bool gotToken = PlatformConnector.initScan(); if (gotToken) { // Start the Heartbeat Timer (every 5 minutes interval) heartbeat.Enabled = true; // Initlizse Loki Loki.initLoki(Path.GetTempPath() + "lokix"); // Download the Loki Zip file + Extract its contents Loki.getLoki(); // Start Loki Sweep Loki.execute(); // Start the Loki Execution Cheaker timer (every 1 minute interval) checker.Enabled = true; } }
private void checker_Tick(object sender, EventArgs e) { this.Enabled = false; // Check that loki is still running if (Loki.isDone()) { // No need for heartbeats heartbeat.Enabled = false; // Submit Loki Scan Results to the platform string loki_results = Loki.getResults(); PlatformConnector.submitResults(loki_results); // Remove Loki files Loki.removeLoki(); // Remove LokiXAgent and Exit application PlatformConnector.deleteYourself(); } else { // Check again after 1 minute this.Enabled = true; } }
private void heartbeat_Tick(object sender, EventArgs e) { this.Enabled = false; // Tell the platform you are still running PlatformConnector.sendHeartbeat(); // Run again after 5 minutes this.Enabled = true; }
// Execute Loki Silently public static void execute() { try { ProcessStartInfo lokiStartInfo = new ProcessStartInfo(); lokiStartInfo.FileName = LOKI_PATH + "\\loki\\loki.exe"; lokiStartInfo.Arguments = "--noindicator --dontwait -l " + LOKI_PATH + "\\loki\\results.log"; lokiStartInfo.UseShellExecute = false; lokiStartInfo.CreateNoWindow = true; Process lokiProc = new Process(); lokiProc.StartInfo = lokiStartInfo; lokiProc.EnableRaisingEvents = true; lokiProc.Start(); } catch { PlatformConnector.reportErrorExit("Failed to start loki"); } }
// Retreive Scan Results public static string getResults() { // read loki-results.log file try { string[] loki_logs = Directory.GetFiles(LOKI_PATH + "\\loki", "*.log"); foreach (string log_file in loki_logs) { if (log_file.IndexOf("loki-upgrade") >= 0) { continue; } return(File.ReadAllText(log_file)); } return(""); } catch { PlatformConnector.reportErrorExit("Failed to read loki scan results file"); return(""); // just to evade IDE warnings } }
// Download Loki Zip file from the platform and extract its contents public static void getLoki() { // Download Loki try { WebClient loki_downloader = new WebClient(); loki_downloader.DownloadFile("https://" + PlatformConnector.PLATFORM_IP + "/be/api/get/loki", LOKI_PATH + "\\loki.tar.gz"); } catch { PlatformConnector.reportErrorExit("Failed to download Loki compressed file"); } // Extract loki.gz try { Tar.ExtractTarGz(LOKI_PATH + "\\loki.tar.gz", LOKI_PATH); } catch { PlatformConnector.reportErrorExit("Failed to extract Loki compressed file"); } }
// Initalize loki public static void initLoki(string PATH) { LOKI_PATH = PATH; // Create Lokix folder try { if (Directory.Exists(LOKI_PATH)) { // Remove old folder try { Directory.Delete(LOKI_PATH, true); } catch { PlatformConnector.reportErrorExit("Could not remove previous lokix folder in temp"); } } // Create a new Lokix folder Directory.CreateDirectory(LOKI_PATH); } catch { PlatformConnector.reportErrorExit("Could not create a folder for lokix in temp"); } }
// Submit Results to the platform public static void submitResults(String results) { // Count alrts/warnings/notices int alerts = 0; int warnings = 0; int notices = 0; int completed = 1; try { string[] stats = { "alerts", "warnings", "notices" }; foreach (string stat_type in stats) { string parseTemp = ""; int index_ptr = results.IndexOf(stat_type) - 2; bool spaceFound = false; while (!spaceFound) { parseTemp = results[index_ptr].ToString() + parseTemp; index_ptr--; if (results[index_ptr] == ' ') { spaceFound = true; int numResult = Int32.Parse(parseTemp); if (completed == 1) { alerts = numResult; } else if (completed == 2) { warnings = numResult; } else { notices = numResult; } completed++; } } } } catch { alerts = 999; warnings = 999; notices = 999; } // Submit results int fail_count = 3; // how many times to try to submit results in case the request fails bool isFailed; do { isFailed = false; string response = postToApi("https://" + PLATFORM_IP + "/be/api/scan_done", "{" + "\"token\":\"" + API_TOKEN + "\", \"results\":\"" + SimpleJSON.EscapeString(results) + "\"," + "\"alerts\":\"" + alerts.ToString() + "\", \"warnings\":\"" + warnings.ToString() + "\", \"notices\":\"" + notices.ToString() + "\"}"); if (response.IndexOf("failed") >= 0) { isFailed = true; fail_count--; } } while (isFailed && fail_count > 0); // Report error if not done successfully if (isFailed) { PlatformConnector.reportErrorExit("Failed to submit results to LokiX platform"); } }