用python來過濾無效用戶名或密碼

學了一段時間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]
'''


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章