用python来过滤无效用户名或密码

本文介绍了一个使用Python实现的功能,旨在去除无效的用户名和密码:删除仅包含用户名而无密码的行;过滤掉密码长度小于6的行;针对特定用户名的暴力破解尝试超过10次;以及使用相同密码暴力测试用户名超过10次的记录。

学了一段时间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]
'''


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值