Skip to content

Commit

Permalink
进行逻辑验证码码的破解识别
Browse files Browse the repository at this point in the history
  • Loading branch information
luyishisi committed Jan 12, 2017
1 parent 0f05924 commit 8e93024
Show file tree
Hide file tree
Showing 89 changed files with 353 additions and 25 deletions.
Binary file modified .DS_Store
Binary file not shown.
File renamed without changes
Binary file added 1.验证码/PIL_EXAMPLES/31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed 1.验证码/PIL_EXAMPLES/6_L.png
Binary file not shown.
39 changes: 19 additions & 20 deletions 1.验证码/PIL_EXAMPLES/rotate.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
# coding:utf-8
from __future__ import print_function
from PIL import Image
import glob, os
#from PIL import Image
import Image
#import glob, os



size = 128,128
#size = 128,128
# 打开图片
im = Image.open("1.png")
#im = Image.open("11.png")
# 旋转45度 并且 显示

# im.rotate(45).show()

# 遍历该目录下的所有PNG文件,打开并且创建略缩图
for infile in glob.glob("*.png"):
file,ext = os.path.splitext(infile)
# print file,ext
im = Image.open(infile)
im.thumbnail(size,Image.ANTIALIAS)
#im.save(file+".thumbnail","JPEG")
# for infile in glob.glob("*.png"):
# file,ext = os.path.splitext(infile)
# # print file,ext
# im = Image.open(infile)
# im.thumbnail(size,Image.ANTIALIAS)
# #im.save(file+".thumbnail","JPEG")

# 创建新图片,并保存
new_img = Image.new("RGB",(512,512),"white")
# 101 31
#new_img = Image.new("RGB",(101,31),"white")
#new_img.save("NEW.png")
im_2 = Image.open("1.1.png")
# im_2 = Image.open("1.1.png")
#im_3 = Image.composite(im.copy(),im_2.copy(),'L')

# 将im与im_2 根据透明度进行合并
Expand All @@ -37,17 +39,14 @@
#输出im的最大最小rgb
#print im_2.getextrema()

im = Image.open('6.png')
im = im.convert('L')
im = im.resize((32,32),Image.ANTIALIAS)
im.save('6_L.png')

#print im.getpixel((0,0))#灰度图没有rgb直接是统一值
#print im.size
im = Image.open('11.png')
#im = im.convert('L')
#im = im.resize((32,32),Image.ANTIALIAS)
#im.save('11_l.png')
for i in xrange(im.size[0]):
for j in xrange(im.size[1]):

#print im.getpixel((i,j))
print im.getpixel((i,j))
l = im.getpixel((j,i))
if( l > 220):
print ('0',end='')
Expand Down
Binary file added 1.验证码/fuhao.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions 1.验证码/num/recognise.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import requests
from PIL import Image
import math
import math,time

def imagesget():
os.mkdir('images')
Expand All @@ -24,7 +24,7 @@ def convert_image(image):
pix=image.getpixel((x,y))
if pix<120:#灰度低于120 设置为 0
image2.putpixel((x,y),0)
image2.save('L.jpeg')#将灰度图存储下来看效果
image2.save('L.png')#将灰度图存储下来看效果
return image2

def cut_image(image):
Expand Down Expand Up @@ -86,12 +86,14 @@ def magnitude(self,concordance):

#计算矢量之间的 cos 值
def relation(self,concordance1, concordance2):

relevance = 0
topvalue = 0
for word, count in concordance1.items():
if word in concordance2:
print type(topvalue),topvalue,count,concordance2[word]
topvalue += count * concordance2[word]
time.sleep(10)
return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))

def recognise(self,image):
Expand Down Expand Up @@ -119,7 +121,8 @@ def recognise(self,image):
if __name__=='__main__':
imageRecognize=CaptchaRecognize()
# 设置图片路径
image=Image.open('3.jpeg')
image=Image.open('3.png')
print image.mode

result=imageRecognize.recognise(image)

Expand Down
Binary file added 1.验证码/num副本/L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions 1.验证码/num副本/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
本部分测试了简单的图像验证码的处理

主要代码为:recognise.py

在main中可以替换为你需要解析的图片路径与名称,

产生L.png是进行了灰度以及二值转换之后的图片在本目录下

还会产生字符切割的效果

用户可以将切割出来的字符人工标记后加入到icon中成为新字符集合.

Binary file added 1.验证码/num副本/cat/1484061493.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061517.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061602.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061618.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061689.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061759.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061798.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061803.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061815.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061821.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484061833.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062418.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062521.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062530.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062547.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062555.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062628.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484062646.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484127810.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1.验证码/num副本/cat/1484128107.png
Binary file added 1.验证码/num副本/cat/1484128325.png
Binary file added 1.验证码/num副本/cat/1484128363.png
Binary file added 1.验证码/num副本/cat/1484128401.png
Binary file added 1.验证码/num副本/cat/1484128417.png
Binary file added 1.验证码/num副本/cat/1484128519.png
Binary file added 1.验证码/num副本/cat/1484128649.png
Binary file added 1.验证码/num副本/cat/1484128765.png
Binary file added 1.验证码/num副本/cat/1484128777.png
Binary file added 1.验证码/num副本/cat/1484128843.png
Binary file added 1.验证码/num副本/cat/1484128879.png
Binary file added 1.验证码/num副本/cat/1484128904.png
Binary file added 1.验证码/num副本/cat/1484128984.png
49 changes: 49 additions & 0 deletions 1.验证码/num副本/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#coding:utf-8

import requests
from recognise import *
from PIL import Image
import base64
import getpass


def login(username,passwd):
session=requests.session()
session.get('http://wsxk.hust.edu.cn/login.jsp').text
img=session.get('http://wsxk.hust.edu.cn/randomImage.action').content
with open('captcha.jpeg','wb') as imgfile:
imgfile.write(img)
imageRecognize=CaptchaRecognize()
image=Image.open('captcha.jpeg')
result=imageRecognize.recognise(image)
string=''
for item in result:
string+=item[1]
print(string)
data={
'usertype':"xs",
'username':username,
'password':passwd,
'rand':string,
'sm1':"",
'ln':"app610.dc.hust.edu.cn"
}
headers = {
'Host':"wsxk.hust.edu.cn",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
'Referer':"http://wsxk.hust.edu.cn/login.jsp",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0"}
session.post('http://wsxk.hust.edu.cn/hublogin.action',data=data,headers=headers)
html=session.get('http://wsxk.hust.edu.cn/select.jsp',headers=headers).text
print(html)
return session

def main():
username=input('username:')
passwd=base64.b64encode(getpass.getpass('Passwd:').encode()).decode()
login(username,passwd)

main()
144 changes: 144 additions & 0 deletions 1.验证码/num副本/recognise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#coding:utf-8

import os
import requests
from PIL import Image
import math,time

def convert_image(image):
image=image.convert('L')#灰度
image2=Image.new('L',image.size,255)
for x in range(image.size[0]):
for y in range(image.size[1]):
pix=image.getpixel((x,y))
if pix<120:#灰度低于120 设置为 0
image2.putpixel((x,y),0)
image2.save('L.png')#将灰度图存储下来看效果
return image2

def cut_image(image):
''' 字符切割,根据黑色的连续性,当某一列出现黑色为标志,当黑色消失为结束点'''
inletter=False
foundletter=False
letters=[]
start=0
end=0
for x in range(image.size[0]):
for y in range(image.size[1]):
pix=image.getpixel((x,y))
if(pix==0):
inletter=True
if foundletter==False and inletter ==True:
foundletter=True
start=x
if foundletter==True and inletter==False:
end=x
letters.append((start,end))
foundletter=False
inletter=False
images=[]
for letter in letters:
img=image.crop((letter[0],0,letter[1],image.size[1]))
#img.save(str(letter[0])+'.png')#展示切割效果
img.save("./cat/"+str(int(time.time()))+'.png')#展示切割效果
images.append(img)
return images

def buildvector(image):
''' 图片转换成矢量,将二维的图片转为一维'''
result={}
count=0
for i in image.getdata():
result[count]=i
count+=1
#print result
return result


class CaptchaRecognize:
def __init__(self):
self.letters=['0','1','2','3','4','5','6','7','8','9']
self.loadSet()

def loadSet(self):
self.imgset=[]
for letter in self.letters:
temp=[]
for img in os.listdir('./icon/%s'%(letter)):
temp.append(buildvector(Image.open('./icon/%s/%s'%(letter,img))))
self.imgset.append({letter:temp})

#计算矢量大小
def magnitude(self,concordance):
total = 0
for word,count in concordance.items():
try:
if(type(count) == type(())):
total += count[0] ** 2
#print type(total),total,type(count),count ** 2
else:
total += count ** 2
except Exception,e:
print type(total),total,type(count),count
print e
return math.sqrt(total)

#计算矢量之间的 cos 值
def relation(self,concordance1, concordance2):
relevance = 0
topvalue = 0
for word, count in concordance1.items():
if word in concordance2:
print type(topvalue),topvalue,count,concordance2[word]
time.sleep(1)
topvalue += count * concordance2[word][0]
#time.sleep(10)
return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))

def recognise(self,image):
image=convert_image(image)#二值化
images=cut_image(image)#字符单独切割出来
vectors=[]
for img in images:
vectors.append(buildvector(img))
result=[]
for vector in vectors:
guess=[]
for image in self.imgset:
for letter,temp in image.items():
relevance=0
num=0
for img in temp:
relevance+=self.relation(vector,img)
num+=1
relevance=relevance/num
guess.append((relevance,letter))
guess.sort(reverse=True)
result.append(guess[0])
return result

if __name__=='__main__':
import os
dir="./temp"
name_list = []
for root,dirs,files in os.walk(dir):
for file in files:
#name_list.append(file)
name = os.path.join(root,file)
name_list.append(name)

print name_list
for i in name_list:
#name = '11'
name = i
print name

imageRecognize=CaptchaRecognize()
# 设置图片路径
image=Image.open(name)
#image=Image.open('./temp/2.png')
print image.mode
result=imageRecognize.recognise(image)
string=[''.join(item[1]) for item in result]
print(string)
break
Binary file added 1.验证码/num副本/temp/1.png
Binary file added 1.验证码/num副本/temp/2.png
Binary file added 1.验证码/num副本/temp/21.png
Binary file added 1.验证码/num副本/temp/3.png
Binary file added 1.验证码/num副本/temp/31.png
Binary file added 1.验证码/num副本/temp/32.png
Binary file added 1.验证码/num副本/temp/4.png
Binary file added 1.验证码/num副本/temp/41.png
Binary file added 1.验证码/num副本/temp/42.png
Binary file added 1.验证码/num副本/temp/5.png
Binary file added 1.验证码/num副本/temp/6.png
Binary file added 1.验证码/num副本/temp/61.png
Binary file added 1.验证码/num副本/temp/7.png
Binary file added 1.验证码/num副本/temp/71.png
Binary file added 1.验证码/num副本/temp/8.png
Binary file added 1.验证码/num副本/temp/81.png
Binary file added 1.验证码/num副本/temp/9.png
Binary file added 1.验证码/num副本/temp/91.png
Binary file added 1.验证码/num副本/temp/92.png
Loading

0 comments on commit 8e93024

Please sign in to comment.