Advertisement
opexxx

bgrep.py

May 29th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Python 2.x
  5.  
  6. # a simple binary grep utility
  7.  
  8. import os, glob, mmap, shutil, sys, array
  9.  
  10. if len(sys.argv)!=2:
  11.     print "usage: bgrep.py hex_string"
  12.     print "example of hex_string: 001122aabb"
  13.     exit(0)
  14.  
  15. pattern=array.array('B', sys.argv[1].decode("hex"))
  16.  
  17. for dir, subdir, files in os.walk(os.getcwd()):
  18.     for file in files:
  19.         try:
  20.             fullfname=dir+"/"+file
  21.             fp = open(fullfname, 'r+')
  22.             if os.stat(fp.name).st_size > 0:
  23.                 #print ("processing %s" % fp.name)
  24.                 mm = mmap.mmap(fp.fileno(), os.stat(fp.name).st_size)
  25.                 if mm.find(pattern, 0)!=-1:
  26.                     sys.stdout.write ("[%s] pattern found\n" % fp.name)
  27.                     sys.stdout.flush()
  28.                 mm.close()
  29.         except IOError:
  30.             sys.stderr.write ("%s: IOError\n" % fullfname)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement