Skip to content

Commit

Permalink
Fixed a bug where SauronEye would crash on PathTooLongException
Browse files Browse the repository at this point in the history
Now handles paths larger than 260 and skips them (Windows is also not really able to process them reliably).
  • Loading branch information
vincent committed Jan 31, 2020
1 parent ee123a6 commit 06cd7b3
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/SauronEye/Searcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOpti
return dirFiles.Concat(Directory.EnumerateFiles(path, searchPattern));
} catch (UnauthorizedAccessException ex) {
return Enumerable.Empty<string>();
} catch (PathTooLongException ex) {
// Microsoft solution: https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-enumerate-directories-and-files
Console.WriteLine("[!] {0} is too long. Continuing with next directory.", path);
return Enumerable.Empty<string>();
}
}

Expand Down Expand Up @@ -124,25 +128,32 @@ public ContentsSearcher(IEnumerable<string> directories, List<string> keywords,

// Searches the contents of filtered files. Does not care about exceptions.
public void Search() {
foreach (String dir in Directories) {
var fileInfo = new FileInfo(dir);
string fileContents;
if (fileInfo.Length < MAX_FILE_SIZE) {
if (IsOfficeExtension(fileInfo.Extension)) {
try {
var reader = new FilterReader(fileInfo.FullName);
fileContents = reader.ReadToEnd();
CheckForKeywords(fileContents, fileInfo);
} catch (Exception e) { Console.WriteLine("[-] Could not read contents of {0}", fileInfo.FullName); }
foreach (String dir in Directories) {
try {
var fileInfo = new FileInfo(dir);
string fileContents;
if (fileInfo.Length < MAX_FILE_SIZE) {
if (IsOfficeExtension(fileInfo.Extension)) {
try {
var reader = new FilterReader(fileInfo.FullName);
fileContents = reader.ReadToEnd();
CheckForKeywords(fileContents, fileInfo);
} catch (Exception e) { Console.WriteLine("[-] Could not read contents of {0}", fileInfo.FullName); }
} else {
//normal file
try {
CheckForKeywords(File.ReadAllText(fileInfo.FullName), fileInfo);
} catch (Exception e) { Console.WriteLine("[-] Could not read contents of {0}", fileInfo.FullName); }

}
} else {
//normal file
try {
CheckForKeywords(File.ReadAllText(fileInfo.FullName), fileInfo);
} catch (Exception e) { Console.WriteLine("[-] Could not read contents of {0}", fileInfo.FullName); }

Console.WriteLine("[-] File exceeds 1MB file size {0}", fileInfo.FullName);
}
} else {
Console.WriteLine("[-] File exceeds 1MB file size {0}", fileInfo.FullName);
} catch (PathTooLongException ex) {
Console.WriteLine("[-] Path {0} is too long. Skipping.", dir);
continue;
} catch (Exception e) {
Console.WriteLine("[-] Some unknown exception {0} occured while processing {1}. Continuing with the next directory.", e.Message, dir);
}
}
}
Expand Down

0 comments on commit 06cd7b3

Please # to comment.