3 # Description: Utility to scan a file path for encrypted and obfuscated files
4 # Authors: Ben Hagen (ben.hagen@neohapsis.com)
5 # Scott Behrens (scott.behrens@neohapsis.com)
9 # pep-0008 - Is stupid. TABS FO'EVER!
11 # Try catch regular expressions/bad path/bad filename/bad regex/
19 from collections
import defaultdict
20 from optparse
import OptionParser
23 """Class that calculates a file's Index of Coincidence as
24 as well as a a subset of files average Index of Coincidence.
27 """Initialize results arrays as well as character counters."""
28 self
.char_count
= defaultdict(int)
29 self
.total_char_count
= 0
31 self
.ic_total_results
= ""
33 def calculate_char_count(self
,data
):
34 """Method to calculate character counts for a particular data file."""
40 charcount
= data
.count(char
)
41 self
.char_count
[char
] += charcount
42 self
.total_char_count
+= charcount
46 def calculate_IC(self
):
47 """Calculate the Index of Coincidence for the self variables"""
49 for val
in self
.char_count
.values():
53 total
+= val
* (val
-1)
56 ic_total
= float(total
)/(self
.total_char_count
* (self
.total_char_count
- 1))
59 self
.ic_total_results
= ic_total
62 def calculate(self
,data
,filename
):
63 """Calculate the Index of Coincidence for a file and append to self.ic_results array"""
71 charcount
= data
.count(char
)
72 char_count
+= charcount
* (charcount
- 1)
73 total_char_count
+= charcount
75 ic
= float(char_count
)/(total_char_count
* (total_char_count
- 1))
76 self
.results
.append({"filename":filename
, "value":ic
})
77 # Call method to calculate_char_count and append to total_char_count
78 self
.calculate_char_count(data
)
82 self
.results
.sort(key
=lambda item
: item
["value"])
83 self
.results
= resultsAddRank(self
.results
)
85 def printer(self
, count
):
86 """Print the top signature count match files for a given search"""
87 # Calculate the Total IC for a Search
89 print "\n[[ Average IC for Search ]]"
90 print self
.ic_total_results
91 print "\n[[ Top %i lowest IC files ]]" % (count
)
92 for x
in range(count
):
93 print ' {0:>7.4f} {1}'.format(self
.results
[x
]["value"], self
.results
[x
]["filename"])
97 """Class that calculates a file's Entropy."""
100 """Instantiate the entropy_results array."""
103 def calculate(self
,data
,filename
):
104 """Calculate the entropy for 'data' and append result to entropy_results array."""
110 p_x
= float(data
.count(chr(x
)))/len(data
)
112 entropy
+= - p_x
* math
.log(p_x
, 2)
113 self
.results
.append({"filename":filename
, "value":entropy
})
117 self
.results
.sort(key
=lambda item
: item
["value"])
118 self
.results
.reverse()
119 self
.results
= resultsAddRank(self
.results
)
121 def printer(self
, count
):
122 """Print the top signature count match files for a given search"""
123 print "\n[[ Top %i entropic files for a given search ]]" % (count
)
124 for x
in range(count
):
125 print ' {0:>7.4f} {1}'.format(self
.results
[x
]["value"], self
.results
[x
]["filename"])
129 """Class that determines the longest word for a particular file."""
131 """Instantiate the longestword_results array."""
134 def calculate(self
,data
,filename
):
135 """Find the longest word in a string and append to longestword_results array"""
140 words
= re
.split("[\s,\n,\r]", data
)
147 self
.results
.append({"filename":filename
, "value":longest
})
151 self
.results
.sort(key
=lambda item
: item
["value"])
152 self
.results
.reverse()
153 self
.results
= resultsAddRank(self
.results
)
155 def printer(self
, count
):
156 """Print the top signature count match files for a given search"""
157 print "\n[[ Top %i longest word files ]]" % (count
)
158 for x
in range(count
):
159 print ' {0:>7} {1}'.format(self
.results
[x
]["value"], self
.results
[x
]["filename"])
162 class SignatureNasty
:
163 """Generator that searches a given file for nasty expressions"""
166 """Instantiate the longestword_results array."""
169 def calculate(self
, data
, filename
):
172 # Lots taken from the wonderful post at http://stackoverflow.com/questions/3115559/exploitable-php-functions
173 valid_regex
= re
.compile('(eval\(|base64_decode|python_eval|exec\(|passthru\(|popen\(|proc_open\(|pcntl_|assert\()')
174 matches
= re
.findall(valid_regex
, data
)
175 self
.results
.append({"filename":filename
, "value":len(matches
)})
179 self
.results
.sort(key
=lambda item
: item
["value"])
180 self
.results
.reverse()
181 self
.results
= resultsAddRank(self
.results
)
183 def printer(self
, count
):
184 """Print the top signature count match files for a given search"""
185 print "\n[[ Top %i signature match counts ]]" % (count
)
186 for x
in range(count
):
187 print ' {0:>7} {1}'.format(self
.results
[x
]["value"], self
.results
[x
]["filename"])
190 def resultsAddRank(results
):
193 previousValue
= False
196 if (previousValue
and previousValue
!= file["value"]):
200 previousValue
= file["value"]
205 """Generator that searches a given filepath with an optional regular
206 expression and returns the filepath and filename"""
207 def search_file_path(self
, args
, valid_regex
):
208 for root
, dirs
, files
in os
.walk(args
[0]):
210 filename
= os
.path
.join(root
, file)
211 if (valid_regex
.search(file) and os
.path
.getsize(filename
) > 60):
213 data
= open(root
+ "/" + file, 'rb').read()
216 print "Could not read file :: %s/%s" % (root
, file)
219 if __name__
== "__main__":
220 """Parse all the options"""
221 parser
= OptionParser(usage
="usage: %prog [options] <start directory> <OPTIONAL: filename regex>",
223 parser
.add_option("-c", "--csv",
227 help="generate CSV outfile",
229 parser
.add_option("-a", "--all",
233 help="Run all tests [Entropy, Longest Word, IC, Signature]",)
234 parser
.add_option("-e", "--entropy",
238 help="Run entropy Test",)
239 parser
.add_option("-l", "--longestword",
243 help="Run longest word test",)
244 parser
.add_option("-i", "--ic",
249 parser
.add_option("-s", "--signature",
253 help="Run signature test",)
254 parser
.add_option("-A", "--auto",
258 help="Run auto file extension tests",)
260 (options
, args
) = parser
.parse_args()
262 # Error on invalid number of arguements
264 parser
.error("Wrong number of arguments")
266 # Error on an invalid path
267 if os
.path
.exists(args
[0]) == False:
268 parser
.error("Invalid path")
271 if (len(args
) == 2 and options
.is_auto
is False):
273 valid_regex
= re
.compile(args
[1])
275 parser
.error("Invalid regular expression")
277 valid_regex
= re
.compile('.*')
281 valid_regex
= re
.compile('(\.php|\.asp|\.aspx|\.scath|\.bash|\.zsh|\.csh|\.tsch|\.pl|\.py|\.txt|\.cgi|\.cfm)$')
284 tests
.append(LanguageIC())
285 tests
.append(Entropy())
286 tests
.append(LongestWord())
287 tests
.append(SignatureNasty())
289 if options
.is_entropy
:
290 tests
.append(Entropy())
291 if options
.is_longest
:
292 tests
.append(LongestWord())
294 tests
.append(LanguageIC())
295 if options
.is_signature
:
296 tests
.append(SignatureNasty())
298 # Instantiate the Generator Class used for searching, opening, and reading files
299 locator
= SearchFile()
301 # CSV file output array
303 csv_header
= ["filename"]
305 # Grab the file and calculate each test against file
306 for data
, filename
in locator
.search_file_path(args
, valid_regex
):
308 # a row array for the CSV
310 csv_row
.append(filename
)
312 calculated_value
= test
.calculate(data
, filename
)
313 # Make the header row if it hasn't been fully populated, +1 here to account for filename column
314 if len(csv_header
) < len(tests
) + 1:
315 csv_header
.append(test
.__class
__.__name
__)
316 csv_row
.append(calculated_value
)
317 csv_array
.append(csv_row
)
320 csv_array
.insert(0,csv_header
)
321 fileOutput
= csv
.writer(open(options
.is_csv
, "wb"))
322 fileOutput
.writerows(csv_array
)
324 # Print top rank lists
329 for file in test
.results
:
330 rank_list
[file["filename"]] = rank_list
.setdefault(file["filename"], 0) + file["rank"]
332 rank_sorted
= sorted(rank_list
.items(), key
=lambda x
: x
[1])
334 print "\n[[ Top cumulative ranked files ]]"
336 print ' {0:>7} {1}'.format(rank_sorted
[x
][1], rank_sorted
[x
][0])