| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import os |
| 4 |
import pefile |
| 5 |
import peutils |
| 6 |
import sys |
| 7 |
|
| 8 |
def dirwalk(path): |
| 9 |
for f in os.listdir(path): |
| 10 |
fullpath = os.path.join(path, f) |
| 11 |
if os.path.isdir(fullpath) and not os.path.islink(fullpath): |
| 12 |
for x in dirwalk(fullpath): |
| 13 |
yield x |
| 14 |
else: |
| 15 |
yield fullpath |
| 16 |
|
| 17 |
def main(sigfile, path): |
| 18 |
sigs = peutils.SignatureDatabase(sigfile) |
| 19 |
|
| 20 |
def print_match(f): |
| 21 |
try: |
| 22 |
res = sigs.match_all(pefile.PE(f, fast_load=True), ep_only=True) |
| 23 |
if not res: |
| 24 |
res = 'no match' |
| 25 |
print '%s: %s' % (f, res) |
| 26 |
except pefile.PEFormatError, e: |
| 27 |
print '%s: %s' % (f, str(e).strip("'")) |
| 28 |
except KeyboardInterrupt: |
| 29 |
sys.exit(1) |
| 30 |
|
| 31 |
if os.path.isfile(path): |
| 32 |
print_match(path) |
| 33 |
elif os.path.isdir(path): |
| 34 |
for f in dirwalk(path): |
| 35 |
if os.path.isfile(f): |
| 36 |
print_match(f) |
| 37 |
|
| 38 |
if __name__ == '__main__': |
| 39 |
if len(sys.argv) != 3: |
| 40 |
print >>sys.stderr, 'usage: %s <signature file> <file or directory>' % sys.argv[0] |
| 41 |
sys.exit(1) |
| 42 |
main(sys.argv[1], sys.argv[2]) |