2016.10.09 09:55


디렉토리내 전체 파일을 얻어낸 뒤(압축까지 해제) 스트링을 검색하여 파일에 저장 

하드코딩된 스트링을 찾기위해서 막 만든거임.


import os

import sys

import base64


FILE_LIST=[]


tmpFile = "result.txt"

def USAGE():

print "python findStr.py [path] [string]"


def init():

if os.path.exists(tmpFile):

os.unlink(tmpFile)


def makeFileList( dirname ):

for (path,dir,files) in os.walk(dirname):

for filename in files :

ext = os.path.splitext(filename)[-1]

full_filename = path+"/"+filename

FILE_LIST.append(full_filename)

if ext.find("tar.gz") > -1 :

os.system("tar xvfz %s -C %s 1>/dev/null" % ( full_filename, path ))

elif ext.find("tar") > -1 :

pass;

os.system("tar xvf %s -C %s 1>/dev/null"  % ( full_filename, path ))

elif ext.find("zip") > -1 :

os.system("unzip %s -d %s 1>/dev/null" %( full_filename, path ))

else :

pass


def findStr():

fp = open(tmpFile,"a+")

cmd1 = "grep -rn %s %s -A 5 " % (sys.argv[2] ,sys.argv[1])

cmd2 = "grep -rn %s %s -A 5 " % (base64.encodestring(sys.argv[2]) ,sys.argv[1])

stdData1 = os.popen(cmd1).read()

stdData2 = os.popen(cmd1).read()

if len(stdData1) >= len(sys.argv[2]) :

fp.write(stdData1)

if len(stdData2) >= len(sys.argv[2]) :

fp.write(stdData2)

fp.close()


def main():

init()

if (len(sys.argv) < 2):

USAGE()

sys.exit(1)

makeFileList( sys.argv[1] )

findStr()


if __name__ == "__main__":

main() 


Posted by k1rha