-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsharpfiles.cs
161 lines (132 loc) · 3.57 KB
/
sharpfiles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
string line;
List<string> shares = new List<string>();
string sharesFile = args[0];
int maxThreads = Int32.Parse(args[1]);
string outFile = args[2];
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(args[0]);
while((line = file.ReadLine()) != null)
{
line = line.Split('\t')[0].Trim();
if(!line.Contains("$") && !line.Contains("NETLOGON") && ! line.Contains("SYSVOL"))
{
shares.Add(line);
}
}
file.Close();
FileSearcher fSearcher = new FileSearcher(maxThreads, outFile);
fSearcher.ProcessShares(shares);
}
}
public class FileSearcher
{
private readonly Object _CountLock = new Object();
private readonly Object _ListLock = new Object();
private int _ThreadCount;
private int _MaxThreads;
private string _OutputFile;
private string[] terms = new string[] {".iso", ".wim", ".vmdk", "pass", "cred", "admin", "login", "logon", ".sql", "secret", "unattend", "sensitive", "root"};
private List<string> _FilesFound = new List<string>();
public FileSearcher(int maxThreads, string outputFile)
{
_ThreadCount = 0;
_MaxThreads = maxThreads;
_OutputFile = outputFile;
}
private Thread StartThread(string path)
{
Thread t = new Thread(() => ProcessShare( path ));
t.Start();
return t;
}
public void ProcessShares(List<string> shares)
{
int counter = 0;
foreach(string path in shares)
{
try
{
while(true)
{
lock(_CountLock)
{
if(_ThreadCount < _MaxThreads)
{
_ThreadCount++;
break;
}
}
Thread.Sleep(1000);
}
StartThread(path);
}
catch
{
continue;
}
counter++;
Console.WriteLine(String.Format("Processed : {0}", path));
Console.WriteLine(String.Format("{0} of {1} Directories", counter, shares.Count));
}
}
private void ProcessShare(string path)
{
ProcessDirectory(path);
lock(_CountLock)
{
_ThreadCount--;
}
}
public void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
try{
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);
}
catch{}
// Recurse into subdirectories of this directory.
try{
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
catch{}
}
// Insert logic for processing found files here.
public void ProcessFile(string path)
{
string[] filesplit = path.Split('\\');
string file = filesplit[filesplit.Length - 1];
foreach( string term in terms)
{
if( file.ToLower().Contains(term.ToLower()) )
{
lock(_ListLock)
{
_FilesFound.Add(path);
using (System.IO.StreamWriter outFile =
new System.IO.StreamWriter(_OutputFile, true))
{
outFile.WriteLine(path);
}
}
//Console.WriteLine(path);
break;
}
}
}
}