Skip to content

Commit

Permalink
Added "Date modified" filter
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent committed Mar 3, 2020
1 parent 7df57a5 commit 41436f6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
44 changes: 41 additions & 3 deletions src/SauronEye/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ public class ArgumentParser {
public bool SearchContents;
public bool SystemDirs;
public RegexSearch regexSearcher;
public DateTime BeforeDate;
public DateTime AfterDate;

public ArgumentParser() {
Directories = new List<string>();
FileTypes = new List<string>();
Keywords = new List<string>();
DefaultFileTypes = new string[] { ".docx", ".txt" };
DefaultKeywords = new string[] { "pass*", "wachtw*" };
SearchContents = true;
SearchContents = false;
SystemDirs = false;
BeforeDate = DateTime.MinValue;
AfterDate = DateTime.MinValue;
}

// Parses the arguments passed to SauronEye, using Mono.Options
Expand All @@ -42,8 +46,10 @@ public void ParseArgumentsOptions(string[] args) {
Keywords.Add(v);
currentParameter = "k";
} },
{ "c|contents","Search file contents", c => SearchContents = c != null},
{ "s|systemdirs","Search in filesystem directories %APPDATA% and %WINDOWS%", s => SystemDirs = s != null},
{ "c|contents","Search file contents", c => SearchContents = c != null },
{ "b|beforedate=", "Filter files last modified before this date, \n format: yyyy-MM-dd", b => { CheckDate(b, "before"); } },
{ "a|afterdate=", "Filter files last modified after this date, \n format: yyyy-MM-dd", a => { CheckDate(a, "after"); } },
{ "s|systemdirs","Search in filesystem directories %APPDATA% and %WINDOWS%", s => SystemDirs = s != null },
{ "h|help","Show help", h => shouldShowHelp = h != null },
{ "<>", v => {
switch(currentParameter) {
Expand Down Expand Up @@ -105,6 +111,38 @@ public void CheckArgs() {

}

private void CheckDate(string date, string whichdate) {
try {
bool result = false;
if (whichdate.Equals("before")) {
result = DateTime.TryParseExact(
date,
"yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out this.BeforeDate);
} else {
result = DateTime.TryParseExact(
date,
"yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out this.AfterDate);
}
if (!result) {
Console.WriteLine("[!] Incorrect --{0}date format. Try SauronEye.exe --help", whichdate);
System.Environment.Exit(1);
}
if (BeforeDate != DateTime.MinValue && AfterDate != DateTime.MinValue) {
Console.WriteLine("[!] Parameters --beforedate and --afterdate are mutually exclusive. Try SauronEye.exe --help");
System.Environment.Exit(1);
}
} catch (Exception) {
Console.WriteLine("[!] Incorrect --{0}date format. Try SauronEye.exe --help", whichdate);
System.Environment.Exit(1);
}
}

private bool isNullOrWhiteSpace(string s) {
return String.IsNullOrEmpty(s) || s.Trim().Length == 0;
}
Expand Down
10 changes: 8 additions & 2 deletions src/SauronEye/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ public static void Main(string[] args) {
Console.WriteLine("For file types: " + string.Join(", ", ArgumentParser.FileTypes));
Console.WriteLine("Containing: " + string.Join(", ", ArgumentParser.Keywords));
Console.WriteLine("Search contents: " + ArgumentParser.SearchContents.ToString());
Console.WriteLine("Search Program Files directories: " + ArgumentParser.SystemDirs.ToString() + "\n");
Console.WriteLine("Search Program Files directories: " + ArgumentParser.SystemDirs.ToString());
if (ArgumentParser.BeforeDate != DateTime.MinValue) {
Console.WriteLine("Only files before: " + ArgumentParser.BeforeDate.ToString("yyyy-MM-dd") + "\n");
}
if (ArgumentParser.AfterDate != DateTime.MinValue) {
Console.WriteLine("Only files after: " + ArgumentParser.AfterDate.ToString("yyyy-MM-dd") + "\n");
}
Stopwatch sw = new Stopwatch();

sw.Start();

var options = new ParallelOptions { MaxDegreeOfParallelism = ArgumentParser.Directories.Count };
Parallel.ForEach(ArgumentParser.Directories, options, (dir) => {
Console.WriteLine("Searching in parallel: " + dir);
var fileSystemSearcher = new FSSearcher(dir, ArgumentParser.FileTypes, ArgumentParser.Keywords, ArgumentParser.SearchContents, ArgumentParser.SystemDirs, ArgumentParser.regexSearcher);
var fileSystemSearcher = new FSSearcher(dir, ArgumentParser.FileTypes, ArgumentParser.Keywords, ArgumentParser.SearchContents, ArgumentParser.SystemDirs, ArgumentParser.regexSearcher, ArgumentParser.BeforeDate, ArgumentParser.AfterDate);
fileSystemSearcher.Search();

});
Expand Down
31 changes: 29 additions & 2 deletions src/SauronEye/Searcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ class FSSearcher {
private bool SystemDirs;
private IEnumerable<string> FilesFilteredOnExtension;
private RegexSearch RegexSearcher;
private DateTime BeforeDate;
private DateTime AfterDate;

public FSSearcher(string d, List<string> f, List<string> k, bool s, bool systemdirs, RegexSearch regex) {
public FSSearcher(string d, List<string> f, List<string> k, bool s, bool systemdirs, RegexSearch regex, DateTime beforedate, DateTime afterdate) {
this.SearchDirectory = d;
this.Filetypes = f;
this.Keywords = k;
this.Results = new List<string>();
this.searchContents = s;
this.SystemDirs = systemdirs;
this.RegexSearcher = regex;
if (beforedate != null) {
this.BeforeDate = beforedate;
}
if (afterdate != null) {
this.AfterDate = afterdate;
}
}


Expand All @@ -45,7 +53,14 @@ public void Search() {
}
}
foreach (string i in Results) {
Console.WriteLine("[+] {0}", i);
if (BeforeDate != DateTime.MinValue || AfterDate != DateTime.MinValue) {
if (MatchesLastWrite(i)) {
Console.WriteLine("[+] {0}", i);
}
} else {
Console.WriteLine("[+] {0}", i);
}

}

// Now search contents
Expand Down Expand Up @@ -109,6 +124,18 @@ private bool EndsWithExtension(string path) {
}
return false;
}

public bool MatchesLastWrite(string path) {
FileInfo fi = new FileInfo(path);
var lastmodified = fi.LastWriteTime;
if (BeforeDate != DateTime.MinValue && lastmodified.Date < BeforeDate.Date) {
return true;
}
if (AfterDate != DateTime.MinValue && lastmodified.Date > AfterDate.Date) {
return true;
}
return false;
}
}


Expand Down

0 comments on commit 41436f6

Please sign in to comment.