Python編寫(xiě)合并字典并實(shí)現(xiàn)敏感目錄的小腳本
0x00 起因
接觸網(wǎng)安快一年了,愛(ài)收集一些字典啊敏感目錄文件啊什么的。收集多了難免有重復(fù),并且有的也需要合并使用方便。自己就寫(xiě)了一個(gè)小小的腳步用來(lái)完成這個(gè)目的。
0x01 代碼
第一次發(fā)表文章,希望大家多提意見(jiàn)。抱拳了!
代碼我就貼出來(lái)了
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),'小時(shí)',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是絕對(duì)路徑
if os.path.isdir(fil): #如果fil是目錄加入到D里
D.write(fil)
__O__(fil) #繼續(xù)執(zhí)行當(dāng)前函數(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ǔn)備執(zhí)行!')
path_list = [] #存放需要打開(kāi)的文件
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)需要打開(kāi)的文件列表
print(' 文件為:',j)
with open(j, 'r') as f:
date = f.readlines()
print(' -t 當(dāng)前執(zhí)行文件為: ' + j,'數(shù)量:',len(date))
for i in date: #讀取每一行
af, aq = os.path.splitext(i) #分裂成路徑及后綴名
aq = aq.lower()
if i != '\n': #如果沒(méi)有換行符會(huì)加上
if '\n' not in i:
i = i + '\n'
aq+='\n'
if aq == '.php\n' and i not in di['new_php.txt']: #判讀后綴名并通過(guò)后綴名放到相應(yīng)的字典中,同時(shí)判讀字典對(duì)應(yīng)的文件列表中是否存在,如果是False則加入進(jìn)去
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("字典文件中有錯(cuò)誤請(qǐng)檢查是否是字典",e)
di1,fi = os.path.split(path)
print(' 分類(lèi)完畢......準(zhǔn)備寫(xiě)入')
for key in di: #寫(xiě)入到文件中
o = open(key,'w')
for i in di[key]:
o.write(i)
Time(n, time.time())
def dir_txt(path): #傳進(jìn)去路徑,返回來(lái)的是路徑下的后綴為txt的文件。如果沒(méi)有則是當(dāng)前路徑。
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(" 開(kāi)始去除重復(fù).....")
for i in path_list: #重復(fù)每一個(gè)給定的密碼文件
F = open(i,'r')
txt = F.readlines()
F.close
for j in txt: #讀取每一個(gè)密碼文件,并且判讀是否存在于password_list列表,如不存在則放進(jìn)列表
if '\n' not in j:
j+='\n'
if j not in password_list:
password_list.append(j)
print(" 完成,并且準(zhǔn)備寫(xiě)入。")
file = open('Password.txt','w') #打開(kāi)即將寫(xiě)入的Password.txt,把passowrd_list列表數(shù)據(jù)全部寫(xiě)入進(jìn)去
for i in password_list:
file.write(i)
file.close
print(" 運(yùn)行完畢。")
except :
print(" 請(qǐng)檢查文件是否存在,路徑是否正確。")
def Optparse():
try:
parser = argparse.ArgumentParser(
"""把字典分類(lèi):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='后臺(tái)目錄字典合并',)
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(" 錯(cuò)誤請(qǐng)檢查輸入。")
except (KeyboardInterrupt):
print(' 以退出')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)字典合并的五種方法(附示例代碼)
- python合并兩個(gè)字典的方法總結(jié)
- Python中的字典合并與列表合并技巧
- 詳解Python實(shí)現(xiàn)字典合并的四種方法
- 詳解Python 合并字典
- python 將列表里的字典元素合并為一個(gè)字典實(shí)例
- Python如何合并多個(gè)字典或映射
- python兩個(gè)_多個(gè)字典合并相加的實(shí)例代碼
- Python合并2個(gè)字典成1個(gè)新字典的方法(9種)
- python 實(shí)現(xiàn)二維字典的鍵值合并等函數(shù)
- Python實(shí)現(xiàn)合并兩個(gè)字典的8種方法
相關(guān)文章
Python的pywifi無(wú)線(xiàn)網(wǎng)絡(luò)庫(kù)的具體使用
pywifi是一個(gè)基于Python的用于操作無(wú)線(xiàn)網(wǎng)絡(luò)的庫(kù),本文就來(lái)介紹一下pywifi的安裝及實(shí)際應(yīng)用場(chǎng)景使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
python爬蟲(chóng) urllib模塊發(fā)起post請(qǐng)求過(guò)程解析
這篇文章主要介紹了python爬蟲(chóng) urllib模塊發(fā)起post請(qǐng)求過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python實(shí)現(xiàn)定時(shí)任務(wù)
本篇文章主要介紹了Python實(shí)現(xiàn)定時(shí)任務(wù),主要有5種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
解決PyCharm IDE環(huán)境下,執(zhí)行unittest不生成測(cè)試報(bào)告的問(wèn)題
這篇文章主要介紹了解決PyCharm IDE環(huán)境下,執(zhí)行unittest不生成測(cè)試報(bào)告的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
如何利用Python實(shí)現(xiàn)簡(jiǎn)單C++程序范圍分析
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)簡(jiǎn)單C++程序范圍分析,文章以舉例說(shuō)明及過(guò)程實(shí)現(xiàn)思路的方式展開(kāi)講解,具有一定的的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-02-02
Python對(duì)文件和目錄進(jìn)行操作的方法(file對(duì)象/os/os.path/shutil 模塊)
下面小編就為大家?guī)?lái)一篇Python對(duì)文件和目錄進(jìn)行操作的方法(file對(duì)象/os/os.path/shutil 模塊)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Python使用Cv2模塊識(shí)別驗(yàn)證碼的操作方法
這篇文章主要介紹了Python使用Cv2模塊識(shí)別驗(yàn)證碼,使用Cv2模塊、pytesseract模塊進(jìn)行操作,pytesseract模塊將智能識(shí)別圖片字體數(shù)字,用于打印出來(lái),本文通過(guò)代碼案例給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01

