Skip to content

Instantly share code, notes, and snippets.

@DiabloHorn
Created January 16, 2018 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiabloHorn/16afc0982710f755cc584bbcea54c1c1 to your computer and use it in GitHub Desktop.
Save DiabloHorn/16afc0982710f755cc584bbcea54c1c1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#DiabloHorn - https://diablohorn.com
import sys
import os
import csv
import argparse
import shutil
try:
import magic
except:
print "sudo pip install python-magic"
sys.exit()
def file_ident(treeroot):
EXCLUDEDEXTENSIONS = ('.json','.html')
for root, dirs, files in os.walk(treeroot):
for f in files:
if f.endswith(EXCLUDEDEXTENSIONS):
continue
fullpath = os.path.join(root,f)
if os.path.isfile(fullpath) and not os.path.islink(fullpath):
csvstdout = csv.writer(sys.stdout, quoting=csv.QUOTE_ALL)
csvstdout.writerow([os.path.abspath(fullpath),magic.from_file(fullpath)])
def file_move(csvfile,keywords,targetdir):
with open(csvfile,'r') as f:
reader = csv.reader(f)
for row in reader:
if any(keyword in row[1] for keyword in keywords):
print 'moving {0} to {1}'.format(row[0], targetdir)
try:
shutil.move(row[0],targetdir)
except shutil.Error, e:
print e
if 'already exists' in str(e):
os.remove(row[0])
print '**removed**'
continue
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='split file in a PE aware manner')
parser.add_argument('input',type=str,help='target directory or csv file to process')
parser.add_argument('--identify',action='store_true',help='identify the file types and write CSV output to stdout')
parser.add_argument('--move',action='store_true',help='move files with keywords to targetdir')
parser.add_argument('--keywords',type=str,nargs='*',help='keywords to search for')
parser.add_argument('--targetdir',help='target directory to store files')
myargs = parser.parse_args()
if myargs.identify:
file_ident(myargs.input)
if myargs.move and myargs.targetdir and myargs.keywords:
file_move(myargs.input, myargs.keywords, myargs.targetdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment