学了一段时间python,刚好工作中要用到一个除去无效用户名及密码的功能,现编了一段代码,主要实现以下功能:
1.去掉只有用户名,没有密码的行.
2.去掉密码长度小于6的行.
3.去掉针对一个用户名进行暴力破解,密码超过10次以上,认为是无效.
4.去掉用同一个密码暴力测试用户名超过10次以上,认为是无效.
上代码。
# -*- coding: cp936 -*-
import os,sys,time
os.chdir(sys.path[0]) #到当前目录
class countTxtFreq:
def __init__(self,infile,outfile,freq='user',fnum=10):
self.infile=infile
self.outfile=outfile
self.freq=freq
self.fnum=fnum
self.countFreq={}
self.Allrow=[]
self.Resultrows=[]
self._getFreq()
self._writeFile()
def _getFreq(self):
self.Allrow=[line.rstrip().split('\t',1) for line in open(self.infile) if line.find('\t')>1 and len(line.split('\t',1)[1])>7] #
if self.freq=='user':
for irow in self.Allrow:
if irow[0] in self.countFreq:
self.countFreq[irow[0]]+=1
else:
self.countFreq.setdefault(irow[0], 1)
#self.countFreq[irow[0]]=1
elif self.freq=='pass': #
for irow in self.Allrow:
if irow[1] in self.countFreq:
self.countFreq[irow[1]]+=1
else:
self.countFreq.setdefault(irow[1], 1)
#self.countFreq[irow[1]]=1
else:
print u'请输入正确的提取参数!'
def _writeFile(self): #
for i in self.Allrow:
if self.freq=='user':
if self.countFreq.get(i[0])<=self.fnum:
self.Resultrows.append(i[0]+'\t'+i[1]+'\n')
elif self.freq=='pass':
if self.countFreq.get(i[1])<=self.fnum:
self.Resultrows.append(i[0]+'\t'+i[1]+'\n')
out=open(self.outfile,'w')
out.writelines(self.Resultrows)
out.close()
if __name__ =='__main__':
time1=time.time()
a=countTxtFreq('pass_new.txt','pass_new2.txt','pass',2)
time2=time.time()
print u'一处处理了条%s记录,用时%s'%(str(len(a.Resultrows)),str(time2-time1))
'''
for key in a.countFreq:
print key,a.countFreq[key]
'''
本文介绍了一个使用Python实现的功能,旨在去除无效的用户名和密码:删除仅包含用户名而无密码的行;过滤掉密码长度小于6的行;针对特定用户名的暴力破解尝试超过10次;以及使用相同密码暴力测试用户名超过10次的记录。
803

被折叠的 条评论
为什么被折叠?



