private void SearchBtn_Click(object sender, EventArgs e) { if (SearchBtn.Text.Equals("Search with this Filter")) { LogSearchQuery Query = GetQueryFromInput(); if (Query != null) { LastSearchResultsIds = new List <int>(CurrentSearchResultsIds); CurrentSearchResultsIds.Clear(); LogGrid.Rows.Clear(); BaseTabs.SelectTab("SearchResultsTab"); if (LogSearchThread != null) { try { LogSearchThread.Abort(); } catch { } } LogSearchThread = new Thread(DoSearch); LogSearchThread.Start(Query); SearchBtn.Text = "Stop Current Search"; } } else { StopSearch(); } }
LogSearchQuery GetQueryFromInput() { ShowStep0Status(""); LogSearchQuery Query = new LogSearchQuery(); if (SearchTypeNewRB.Checked) { foreach (object Item in SelectLogSourceCombo.Items) { if (Item.ToString().Equals(SelectLogSourceCombo.Text)) { Query.LogSource = SelectLogSourceCombo.Text; this.CurrentSearchSource = Query.LogSource; break; } } if (Query.LogSource.Length == 0) { ShowStep0Error("Invaid Log source selected"); SelectLogSourceCombo.BackColor = Color.Red; return(null); } if (LogIdsRangeAnyRB.Checked) { Query.MinLogId = -1; Query.MaxLogId = -1; } else { if (LogIdsRangeAboveCB.Visible) { if (LogIdsRangeAboveCB.Checked) { try { Query.MinLogId = Int32.Parse(LogIdsRangeAboveTB.Text.Trim()); } catch { ShowStep0Error("Log ID range lower limit is not a valid number"); LogIdsRangeAboveTB.BackColor = Color.Red; return(null); } } if (LogIdsRangeBelowCB.Checked) { try { Query.MaxLogId = Int32.Parse(LogIdsRangeBelowTB.Text.Trim()); } catch { ShowStep0Error("Log ID range upper limit is not a valid number"); LogIdsRangeBelowTB.BackColor = Color.Red; return(null); } } else if (LogIdsRangeAboveCB.Checked) { Query.MaxLogId = Config.GetLastLogId(Query.LogSource); } } else { try { Query.ScanLogId = Int32.Parse(LogIdsRangeAboveTB.Text.Trim()); } catch { ShowStep0Error("Scan ID is not a valid number"); LogIdsRangeAboveTB.BackColor = Color.Red; return(null); } } } } else { Query.LogSource = this.CurrentSearchSource; Query.LogIds = new List <int>(this.CurrentSearchResultsIds); } if (MatchKeywordCB.Checked) { if (MatchKeywordInRequestHeadersSectionCB.Checked || MatchKeywordInRequestBodySectionCB.Checked || MatchKeywordInResponseHeadersSectionCB.Checked || MatchKeywordInResponseBodySectionCB.Checked) { if (MatchKeywordTB.Text.Length > 0) { Query.Keyword = MatchKeywordTB.Text; Query.SearchRequestHeaders = MatchKeywordInRequestHeadersSectionCB.Checked; Query.SearchRequestBody = MatchKeywordInRequestBodySectionCB.Checked; Query.SearchResponseHeaders = MatchKeywordInResponseHeadersSectionCB.Checked; Query.SearchResponseBody = MatchKeywordInResponseBodySectionCB.Checked; } else { ShowStep0Error("Keyword search is enabled but the keyword is empty"); MatchKeywordTB.BackColor = Color.Red; return(null); } } else { ShowStep0Error("Keyword search is enabled but no sections are selected"); return(null); } } if (MatchRequestUrlCB.Checked) { if (MatchRequestUrlTypeCombo.SelectedIndex > -1) { if (MatchRequestUrlKeywordTB.Text.Length > 0) { Query.UrlMatchMode = MatchRequestUrlTypeCombo.SelectedIndex; Query.UrlMatchString = MatchRequestUrlKeywordTB.Text; } else { ShowStep0Error("Url match is enabled but the match keyword is empty"); MatchRequestUrlKeywordTB.BackColor = Color.Red; return(null); } } else { ShowStep0Error("Invalid Url match type is selected"); MatchRequestUrlTypeCombo.BackColor = Color.Red; return(null); } } if (MatchRequestMethodsCB.Checked) { string[] Methods = MatchRequestMethodsTB.Text.Split(new char[] { ',' }); for (int i = 0; i < Methods.Length; i++) { Methods[i] = Methods[i].Trim(); } if (Methods.Length > 0) { if (MatchRequestMethodsPlusRB.Checked) { Query.MethodsToCheck = new List <string>(Methods); } else { Query.MethodsToIgnore = new List <string>(Methods); } } else { ShowStep0Error("Request Methods match is selected but no methods specified Invalid Url match type is selected"); MatchRequestMethodsTB.BackColor = Color.Red; return(null); } } if (MatchResponseCodesCB.Checked) { string[] ResponseCodesStrings = MatchResponseCodesTB.Text.Split(new char[] { ',' }); List <int> ResponseCodes = new List <int>(); foreach (string CodeString in ResponseCodesStrings) { try { ResponseCodes.Add(Int32.Parse(CodeString)); } catch { } } if (ResponseCodes.Count > 0) { if (MatchResponseCodesPlusRB.Checked) { Query.CodesToCheck = new List <int>(ResponseCodes); } else { Query.CodesToIgnore = new List <int>(ResponseCodes); } } else { ShowStep0Error("Response Codes match is selected but no methods specified Invalid Url match type is selected"); MatchResponseCodesTB.BackColor = Color.Red; return(null); } } if (MatchHostNamesCB.Checked) { string[] HostNames = MatchHostNamesTB.Text.Split(new char[] { ',' }); for (int i = 0; i < HostNames.Length; i++) { HostNames[i] = HostNames[i].Trim(); } if (HostNames.Length > 0) { if (MatchHostNamesPlusRB.Checked) { Query.HostNamesToCheck = new List <string>(HostNames); } else { Query.HostNamesToIgnore = new List <string>(HostNames); } } else { ShowStep0Error("Hostnames match is selected but no methods specified Invalid Url match type is selected"); MatchHostNamesTB.BackColor = Color.Red; return(null); } } if (MatchFileExtensionsCB.Checked) { string[] FileExtensions = MatchFileExtensionsTB.Text.Split(new char[] { ',' }); for (int i = 0; i < FileExtensions.Length; i++) { FileExtensions[i] = FileExtensions[i].Trim(); } if (FileExtensions.Length > 0) { if (MatchFileExtensionsPlusRB.Checked) { Query.FileExtensionsToCheck = new List <string>(FileExtensions); } else { Query.FileExtensionsToIgnore = new List <string>(FileExtensions); } } else { ShowStep0Error("Request File Extensions match is selected but no methods specified Invalid Url match type is selected"); MatchFileExtensionsTB.BackColor = Color.Red; return(null); } } if (Query.Keyword.Length > 0 || Query.UrlMatchString.Length > 0 || Query.FileExtensionsToCheck.Count > 0 || Query.FileExtensionsToIgnore.Count > 0 || Query.MethodsToCheck.Count > 0 || Query.MethodsToIgnore.Count > 0 || Query.CodesToCheck.Count > 0 || Query.CodesToIgnore.Count > 0 || Query.HostNamesToCheck.Count > 0 || Query.HostNamesToIgnore.Count > 0) { return(Query); } else { ShowStep0Error("No options selected in the filter settings"); return(null); } }
void DoSearch(object QueryObject) { LogSearchQuery Query = (LogSearchQuery)QueryObject; List <LogRow> LogRows = new List <LogRow>(); ShowSearchProgressBar(true); try { if (Query.LogIds.Count > 0) { int CurrentStartIndex = 0; while (CurrentStartIndex < Query.LogIds.Count) { LogRows = IronDB.SearchLogs(Query, Query.LogIds, CurrentStartIndex, 100); ConvertAndShowRows(LogRows); CurrentStartIndex = CurrentStartIndex + 100; } } else if (Query.ScanLogId > 0) { LogRows = IronDB.SearchLogs(Query, Query.ScanLogId); } else if (Query.MinLogId > -1 | Query.MaxLogId > -1) { int CurrentStartIndex = Query.MinLogId - 1; while (CurrentStartIndex < Query.MaxLogId + 1) { if (CurrentStartIndex + 1000 > Query.MaxLogId) { LogRows = IronDB.SearchLogs(Query, CurrentStartIndex, Query.MaxLogId + 1); } else { LogRows = IronDB.SearchLogs(Query, CurrentStartIndex, CurrentStartIndex + 1000); } ConvertAndShowRows(LogRows); CurrentStartIndex = CurrentStartIndex + 1000; } } else { int CurrentStartIndex = 0; while (CurrentStartIndex <= Config.GetLastLogId(Query.LogSource)) { LogRows = IronDB.SearchLogs(Query, CurrentStartIndex, CurrentStartIndex + 1000); ConvertAndShowRows(LogRows); CurrentStartIndex = CurrentStartIndex + 1000; } } if (this.CurrentSearchResultsIds.Count == 0) { this.CurrentSearchResultsIds = new List <int>(this.LastSearchResultsIds); } } catch (ThreadAbortException) { } catch (Exception Exp) { IronException.Report("Error searching Logs", Exp); ShowStep0Error("Error searching logs"); } finally { EndLogSearch(); } }
internal static string GetQueryWithoutIdsParts(LogSearchQuery Query) { string QueryFirstPart = string.Format("SELECT ID, SSL, HostName, Method, URL, File, Parameters, BinaryRequest, Code, Length, MIME, SetCookie, BinaryResponse FROM {0} WHERE ", GetLogTableName(Query.LogSource)); StringBuilder SB = new StringBuilder(); bool AndRequired = true; if (Query.HostNamesToCheck.Count > 0 || Query.HostNamesToIgnore.Count > 0) { if (AndRequired) SB.Append(" AND "); SB.Append(GetHostNamesSearchQueryPart(Query)); AndRequired = true; } if (Query.CodesToCheck.Count > 0 || Query.CodesToIgnore.Count > 0) { if (AndRequired) SB.Append(" AND "); SB.Append(GetCodesSearchQueryPart(Query)); AndRequired = true; } if (Query.FileExtensionsToCheck.Count > 0 || Query.FileExtensionsToIgnore.Count > 0) { if (AndRequired) SB.Append(" AND "); SB.Append(GetFilesSearchQueryPart(Query)); AndRequired = true; } if (Query.MethodsToCheck.Count > 0 || Query.MethodsToIgnore.Count > 0) { if (AndRequired) SB.Append(" AND "); SB.Append(GetMethodsSearchQueryPart(Query)); AndRequired = true; } if (Query.UrlMatchString.Length > 0) { if (AndRequired) SB.Append(" AND "); SB.Append(GetUrlSearchQueryPart(Query.UrlMatchMode)); AndRequired = true; } if (Query.Keyword.Length > 0) { if (AndRequired) SB.Append(" AND "); SB.Append(GetKeywordSearchQueryPart(Query.Keyword, Query.SearchRequestHeaders, Query.SearchRequestBody, Query.SearchResponseHeaders, Query.SearchResponseBody)); AndRequired = true; } StringBuilder FinalSB = new StringBuilder(); FinalSB.Append(QueryFirstPart); FinalSB.Append(" {0} "); FinalSB.Append(SB.ToString()); return FinalSB.ToString(); }
static List<LogRow> SearchDB(string DataSource, string CmdString, LogSearchQuery Query) { List<LogRow> IronLogRecords = new List<LogRow>(); using(SQLiteConnection DB = new SQLiteConnection(DataSource)) { DB.Open(); using (SQLiteCommand cmd = DB.CreateCommand()) { cmd.CommandText = CmdString; if (Query.UrlMatchString.Length > 0) { cmd.Parameters.AddWithValue("@UrlMatchKeyword", GetUrlSearchQueryValuePart(Query.UrlMatchMode, Query.UrlMatchString)); } if (Query.Keyword.Length > 0) { cmd.Parameters.AddWithValue("@Keyword", string.Format("%{0}%", Query.Keyword)); } if (Query.MethodsToCheck.Count > 0) { for (int i = 0; i < Query.MethodsToCheck.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@Method{0}", i), Query.MethodsToCheck[i]); } } else if (Query.MethodsToIgnore.Count > 0) { for (int i = 0; i < Query.MethodsToIgnore.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@Method{0}", i), Query.MethodsToIgnore[i]); } } if (Query.HostNamesToCheck.Count > 0) { for (int i = 0; i < Query.HostNamesToCheck.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@HostName{0}", i), Query.HostNamesToCheck[i]); } } else if (Query.HostNamesToIgnore.Count > 0) { for (int i = 0; i < Query.HostNamesToIgnore.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@HostName{0}", i), Query.HostNamesToIgnore[i]); } } if (Query.CodesToCheck.Count > 0) { for (int i = 0; i < Query.CodesToCheck.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@Code{0}", i), Query.CodesToCheck[i]); } } else if (Query.CodesToIgnore.Count > 0) { for (int i = 0; i < Query.CodesToIgnore.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@Code{0}", i), Query.CodesToIgnore[i]); } } if (Query.FileExtensionsToCheck.Count > 0) { for (int i = 0; i < Query.FileExtensionsToCheck.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@File{0}", i), Query.FileExtensionsToCheck[i]); } } else if (Query.FileExtensionsToIgnore.Count > 0) { for (int i = 0; i < Query.FileExtensionsToIgnore.Count; i++) { cmd.Parameters.AddWithValue(string.Format("@File{0}", i), Query.FileExtensionsToIgnore[i]); } } using (SQLiteDataReader result = cmd.ExecuteReader()) { while (result.Read()) { try { LogRow LR = new LogRow(); try { LR.ID = Int32.Parse(result["ID"].ToString()); } catch { continue; } LR.Host = result["HostName"].ToString(); LR.Method = result["Method"].ToString(); LR.Url = result["URL"].ToString(); LR.File = result["File"].ToString(); LR.SSL = result["SSL"].ToString().Equals("1"); LR.Parameters = result["Parameters"].ToString(); try { LR.Code = Int32.Parse(result["Code"].ToString()); } catch { LR.Code = -1; } try { LR.Length = Int32.Parse(result["Length"].ToString()); } catch { LR.Length = 0; } LR.Mime = result["MIME"].ToString(); LR.SetCookie = result["SetCookie"].ToString().Equals("1"); IronLogRecords.Add(LR); } catch { } } } } } return IronLogRecords; }
static string GetMethodsSearchQueryPart(LogSearchQuery Query) { List<string> Methods = new List<string>(); bool Negate = false; if (Query.MethodsToCheck.Count > 0) { Methods = new List<string>(Query.MethodsToCheck); Negate = false; } else if (Query.MethodsToIgnore.Count > 0) { Methods = new List<string>(Query.MethodsToIgnore); Negate = true; } StringBuilder SB = new StringBuilder(); for (int i=0; i< Methods.Count ; i++) { if (SB.Length > 0) { if (Negate) SB.Append(" AND "); else SB.Append(" OR "); } SB.Append("Method"); if (Negate) SB.Append("!="); else SB.Append("="); SB.Append(string.Format("@Method{0}", i)); } return string.Format("({0})", SB.ToString()); }
internal static List<LogRow> SearchLogs(LogSearchQuery Query, int ScanID) { List<LogRow> Results = new List<LogRow>(); string DataSource = string.Format("data source={0}", GetLogSourceFileName(Query.LogSource)); string QueryWithoutIds = GetQueryWithoutIdsParts(Query); string FullQuery = string.Format(QueryWithoutIds, GetLogIdsRangeSearchQueryPart(ScanID)); return SearchDB(DataSource, FullQuery, Query); }
LogSearchQuery GetQueryFromInput() { ShowStep0Status(""); LogSearchQuery Query = new LogSearchQuery(); if (SearchTypeNewRB.Checked) { foreach (object Item in SelectLogSourceCombo.Items) { if (Item.ToString().Equals(SelectLogSourceCombo.Text)) { Query.LogSource = SelectLogSourceCombo.Text; this.CurrentSearchSource = Query.LogSource; break; } } if (Query.LogSource.Length == 0) { ShowStep0Error("Invaid Log source selected"); SelectLogSourceCombo.BackColor = Color.Red; return null; } if (LogIdsRangeAnyRB.Checked) { Query.MinLogId = -1; Query.MaxLogId = -1; } else { if (LogIdsRangeAboveCB.Visible) { if (LogIdsRangeAboveCB.Checked) { try { Query.MinLogId = Int32.Parse(LogIdsRangeAboveTB.Text.Trim()); } catch { ShowStep0Error("Log ID range lower limit is not a valid number"); LogIdsRangeAboveTB.BackColor = Color.Red; return null; } } if (LogIdsRangeBelowCB.Checked) { try { Query.MaxLogId = Int32.Parse(LogIdsRangeBelowTB.Text.Trim()); } catch { ShowStep0Error("Log ID range upper limit is not a valid number"); LogIdsRangeBelowTB.BackColor = Color.Red; return null; } } else if (LogIdsRangeAboveCB.Checked) { Query.MaxLogId = Config.GetLastLogId(Query.LogSource); } } else { try { Query.ScanLogId = Int32.Parse(LogIdsRangeAboveTB.Text.Trim()); } catch { ShowStep0Error("Scan ID is not a valid number"); LogIdsRangeAboveTB.BackColor = Color.Red; return null; } } } } else { Query.LogSource = this.CurrentSearchSource; Query.LogIds = new List<int>(this.CurrentSearchResultsIds); } if (MatchKeywordCB.Checked) { if (MatchKeywordInRequestHeadersSectionCB.Checked || MatchKeywordInRequestBodySectionCB.Checked || MatchKeywordInResponseHeadersSectionCB.Checked || MatchKeywordInResponseBodySectionCB.Checked) { if (MatchKeywordTB.Text.Length > 0) { Query.Keyword = MatchKeywordTB.Text; Query.SearchRequestHeaders = MatchKeywordInRequestHeadersSectionCB.Checked; Query.SearchRequestBody = MatchKeywordInRequestBodySectionCB.Checked; Query.SearchResponseHeaders = MatchKeywordInResponseHeadersSectionCB.Checked; Query.SearchResponseBody = MatchKeywordInResponseBodySectionCB.Checked; } else { ShowStep0Error("Keyword search is enabled but the keyword is empty"); MatchKeywordTB.BackColor = Color.Red; return null; } } else { ShowStep0Error("Keyword search is enabled but no sections are selected"); return null; } } if (MatchRequestUrlCB.Checked) { if (MatchRequestUrlTypeCombo.SelectedIndex > -1) { if (MatchRequestUrlKeywordTB.Text.Length > 0) { Query.UrlMatchMode = MatchRequestUrlTypeCombo.SelectedIndex; Query.UrlMatchString = MatchRequestUrlKeywordTB.Text; } else { ShowStep0Error("Url match is enabled but the match keyword is empty"); MatchRequestUrlKeywordTB.BackColor = Color.Red; return null; } } else { ShowStep0Error("Invalid Url match type is selected"); MatchRequestUrlTypeCombo.BackColor = Color.Red; return null; } } if (MatchRequestMethodsCB.Checked) { string[] Methods = MatchRequestMethodsTB.Text.Split(new char[]{','}); for (int i = 0; i < Methods.Length; i++) { Methods[i] = Methods[i].Trim(); } if (Methods.Length > 0) { if (MatchRequestMethodsPlusRB.Checked) { Query.MethodsToCheck = new List<string>(Methods); } else { Query.MethodsToIgnore = new List<string>(Methods); } } else { ShowStep0Error("Request Methods match is selected but no methods specified Invalid Url match type is selected"); MatchRequestMethodsTB.BackColor = Color.Red; return null; } } if (MatchResponseCodesCB.Checked) { string[] ResponseCodesStrings = MatchResponseCodesTB.Text.Split(new char[] { ',' }); List<int> ResponseCodes = new List<int>(); foreach (string CodeString in ResponseCodesStrings) { try { ResponseCodes.Add(Int32.Parse(CodeString)); } catch { } } if (ResponseCodes.Count > 0) { if (MatchResponseCodesPlusRB.Checked) { Query.CodesToCheck = new List<int>(ResponseCodes); } else { Query.CodesToIgnore = new List<int>(ResponseCodes); } } else { ShowStep0Error("Response Codes match is selected but no methods specified Invalid Url match type is selected"); MatchResponseCodesTB.BackColor = Color.Red; return null; } } if (MatchHostNamesCB.Checked) { string[] HostNames = MatchHostNamesTB.Text.Split(new char[] { ',' }); for (int i = 0; i < HostNames.Length; i++) { HostNames[i] = HostNames[i].Trim(); } if (HostNames.Length > 0) { if (MatchHostNamesPlusRB.Checked) { Query.HostNamesToCheck = new List<string>(HostNames); } else { Query.HostNamesToIgnore = new List<string>(HostNames); } } else { ShowStep0Error("Hostnames match is selected but no methods specified Invalid Url match type is selected"); MatchHostNamesTB.BackColor = Color.Red; return null; } } if (MatchFileExtensionsCB.Checked) { string[] FileExtensions = MatchFileExtensionsTB.Text.Split(new char[] { ',' }); for (int i = 0; i < FileExtensions.Length; i++) { FileExtensions[i] = FileExtensions[i].Trim(); } if (FileExtensions.Length > 0) { if (MatchFileExtensionsPlusRB.Checked) { Query.FileExtensionsToCheck = new List<string>(FileExtensions); } else { Query.FileExtensionsToIgnore = new List<string>(FileExtensions); } } else { ShowStep0Error("Request File Extensions match is selected but no methods specified Invalid Url match type is selected"); MatchFileExtensionsTB.BackColor = Color.Red; return null; } } if (Query.Keyword.Length > 0 || Query.UrlMatchString.Length > 0 || Query.FileExtensionsToCheck.Count > 0 || Query.FileExtensionsToIgnore.Count > 0 || Query.MethodsToCheck.Count > 0 || Query.MethodsToIgnore.Count > 0 || Query.CodesToCheck.Count > 0 || Query.CodesToIgnore.Count > 0 || Query.HostNamesToCheck.Count > 0 || Query.HostNamesToIgnore.Count > 0) { return Query; } else { ShowStep0Error("No options selected in the filter settings"); return null; } }