Python編寫合并字典并實現(xiàn)敏感目錄的小腳本
更新時間:2019年02月26日 14:27:36 作者:不成大佬不改名
這篇文章主要介紹了Python編寫合并字典并實現(xiàn)敏感目錄的小腳本,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
0x00 起因
接觸網(wǎng)安快一年了,愛收集一些字典啊敏感目錄文件啊什么的。收集多了難免有重復,并且有的也需要合并使用方便。自己就寫了一個小小的腳步用來完成這個目的。
0x01 代碼
第一次發(fā)表文章,希望大家多提意見。抱拳了!
代碼我就貼出來了
import osimport time
import argparse
import optparse
def Time(n,p):
pat_time = p-n
minute = pat_time/60
hour = minute/60
print(int(hour),'小時',int(minute%60),'分鐘',int(pat_time%60),'秒')
def __O__(path): #遍歷目錄,包括目錄里的子目錄
f = os.listdir(path) #查看路徑下所有文件及目錄
D = open('D.txt','w')
F = open('F.txt','w')
for i in f:
fil = os.path.join(path,i) #dir是絕對路徑
if os.path.isdir(fil): #如果fil是目錄加入到D里
D.write(fil)
__O__(fil) #繼續(xù)執(zhí)行當前函數(shù)
if os.path.isfile(fil): #如果是文件,加入到F里
F.write(fil)
D.close()
F.close()
def type_txt(file,path): #傳入路徑,文件名(列表)
try:
n = time.time()
print(' 準備執(zhí)行!')
path_list = [] #存放需要打開的文件
di = {}
date_list = []
dict_li = ['new_php.txt', 'new_asp.txt', 'new_aspx.txt', 'new_jsp.txt', 'new_dir.txt', 'new_mdb.txt',] #合并的字典列表
for i in dict_li:
di = []
for h in file: #合并給的路徑及文件名
fn = os.path.join(path, h)
path_list.append(fn)
for j in path_list: #循環(huán)需要打開的文件列表
print(' 文件為:',j)
with open(j, 'r') as f:
date = f.readlines()
print(' -t 當前執(zhí)行文件為: ' + j,'數(shù)量:',len(date))
for i in date: #讀取每一行
af, aq = os.path.splitext(i) #分裂成路徑及后綴名
aq = aq.lower()
if i != '\n': #如果沒有換行符會加上
if '\n' not in i:
i = i + '\n'
aq+='\n'
if aq == '.php\n' and i not in di['new_php.txt']: #判讀后綴名并通過后綴名放到相應的字典中,同時判讀字典對應的文件列表中是否存在,如果是False則加入進去
di['new_php.txt'].append(i)
elif aq == '.asp\n' and i not in di['new_asp.txt']:
di['new_asp.txt'].append(i)
elif aq == '.aspx\n' and i not in di['new_aspx.txt']:
di['new_aspx.txt'].append(i)
elif aq == '.jsp\n' and i not in di['new_jsp.txt'] :
di['new_jsp.txt'].append(i)
elif (aq == ('.ini\n' or '.xml\n' or '.cgi\n' or '.log\n' or '.db\n' or '.mdb\n' or '.sql\n' or '.xml\n' or '.htm\n' or '.html\n' or '.bak\n' or '.dbf\n' or '.zip\n' or '.rb\n' or '.js\n' or '.css\n' or '.html?\n')) and i not in di['new_mdb.txt']:
di['new_mdb.txt'].append(i)
elif i not in di['new_dir.txt'] :
di['new_dir.txt'].append(i)
f.close()
except (UnicodeDecodeError) as e:
print("字典文件中有錯誤請檢查是否是字典",e)
di1,fi = os.path.split(path)
print(' 分類完畢......準備寫入')
for key in di: #寫入到文件中
o = open(key,'w')
for i in di[key]:
o.write(i)
Time(n, time.time())
def dir_txt(path): #傳進去路徑,返回來的是路徑下的后綴為txt的文件。如果沒有則是當前路徑。
dir_txt = []
a = os.listdir(path)
for i in a:
dd, ff = os.path.splitext(i)
if ff == '.txt':
dir_txt.append(i)
return dir_txt
def password(file,path):
try:
path_list = []
password_list = []
for i in file:
pathl = os.path.join(path,i)
path_list.append(pathl)
print(" 開始去除重復.....")
for i in path_list: #重復每一個給定的密碼文件
F = open(i,'r')
txt = F.readlines()
F.close
for j in txt: #讀取每一個密碼文件,并且判讀是否存在于password_list列表,如不存在則放進列表
if '\n' not in j:
j+='\n'
if j not in password_list:
password_list.append(j)
print(" 完成,并且準備寫入。")
file = open('Password.txt','w') #打開即將寫入的Password.txt,把passowrd_list列表數(shù)據(jù)全部寫入進去
for i in password_list:
file.write(i)
file.close
print(" 運行完畢。")
except :
print(" 請檢查文件是否存在,路徑是否正確。")
def Optparse():
try:
parser = argparse.ArgumentParser(
"""把字典分類:new_php.txt,new_asp.txt,new_aspx.txt,new_jsp.txt,new_mdb.txt,new_dir.
-t -path C:\\x\\x\*
-t -path C:\\x\\x -file xxx.txt,xx.txt """)
group = parser.add_mutually_exclusive_group()
group.add_argument('-p',action='store_true',help='密碼合并') #-p 和-t參數(shù)是互相排斥的,并不能一起使用。
group.add_argument('-t',action='store_true',help='后臺目錄字典合并',)
parser.add_argument('-file', type=str, help='輸入的文件',required=False)
parser.add_argument('-path',type=str,help='你所指定的目錄',required=False)
args = parser.parse_args()
if args.t: #判讀t是否真
if args.file == None:
path,file = os.path.split(args.path)
dir__txt = dir_txt(path)
type_txt(dir__txt,path)
elif args.file != None:
list_file = args.file.split(',')
type_txt(list_file,args.path)
elif args.p: #判讀p是否真
if args.file == None:
path, file = os.path.split(args.path)
dir__txt = dir_txt(path)
password(dir__txt,path)
elif args.file != None:
list_file = args.file.split(',')
password(list_file,args.path)
else:
print(" 錯誤請檢查輸入。")
except (KeyboardInterrupt):
print(' 以退出')
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
pytorch中如何使用DataLoader對數(shù)據(jù)集進行批處理的方法
這篇文章主要介紹了pytorch中如何使用DataLoader對數(shù)據(jù)集進行批處理的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

