Python 破解极验滑动验证码
Python 破解极验滑动验证码
测试开发社区 1周前
阅读目录
极验滑动验证码
实现
位移移动需要的基础知识
对比两张图片,找出缺口
获得图片
按照位移移动
详细代码
回到顶部
极验滑动验证码
以上图片是最典型的要属于极验滑动认证了,极验官网:http://www.geetest.com/。
现在极验验证码已经更新到了 3.0 版本,截至 2017 年 7 月全球已有十六万家企业正在使用极验,每天服务响应超过四亿次,广泛应用于直播视频、金融服务、电子商务、游戏娱乐、政府企业等各大类型网站
对于这类验证,如果我们直接模拟表单请求,繁琐的认证参数与认证流程会让你蛋碎一地,我们可以用selenium驱动浏览器来解决这个问题,大致分为以下几个步骤
1、输入用户名,密码
2、点击按钮验证,弹出没有缺口的图
3、获得没有缺口的图片
4、点击滑动按钮,弹出有缺口的图
5、获得有缺口的图片
6、对比两张图片,找出缺口,即滑动的位移
7、按照人的行为行为习惯,把总位移切成一段段小的位移
8、按照位移移动
9、完成登录
回到顶部
实现
位移移动需要的基础知识
位移移动相当于匀变速直线运动,类似于小汽车从起点开始运行到终点的过程(首先为匀加速,然后再匀减速)。
其中a为加速度,且为恒量(即单位时间内的加速度是不变的),t为时间
位移移动的代码实现
def get_track(distance):
'''
拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速
匀变速运动基本公式:
①v=v0+at
②s=v0t+(1/2)at²
③v²-v0²=2as :param distance: 需要移动的距离
:return: 存放每0.2秒移动的距离
'''
# 初速度
v=0
# 单位时间为0.2s来统计轨迹,轨迹即0.2内的位移
t=0.1
# 位移/轨迹列表,列表内的一个元素代表0.2s的位移
tracks=[]
# 当前的位移
current=0
# 到达mid值开始减速
mid=distance * 4/5 distance += 10 # 先滑过一点,最后再反着滑动回来 while current < distance:
if current < mid:
# 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细
a = 2 # 加速运动
else:
a = -3 # 减速运动 # 初速度
v0 = v
# 0.2秒时间内的位移
s = v0*t+0.5*a*(t**2)
# 当前的位置
current += s
# 添加到轨迹列表
tracks.append(round(s)) # 速度已经达到v,该速度作为下次的初速度
v= v0+a*t # 反着滑动到大概准确位置
for i in range(3):
tracks.append(-2)
for i in range(4):
tracks.append(-1)
return tracks
对比两张图片,找出缺口
def get_distance(image1,image2):
'''
拿到滑动验证码需要移动的距离
:param image1:没有缺口的图片对象
:param image2:带缺口的图片对象
:return:需要移动的距离
'''
# print('size', image1.size) threshold = 50
for i in range(0,image1.size[0]): # 260
for j in range(0,image1.size[1]): # 160
pixel1 = image1.getpixel((i,j))
pixel2 = image2.getpixel((i,j))
res_R = abs(pixel1[0]-pixel2[0]) # 计算RGB差
res_G = abs(pixel1[1] - pixel2[1]) # 计算RGB差
res_B = abs(pixel1[2] - pixel2[2]) # 计算RGB差
if res_R > threshold and res_G > threshold and res_B > threshold:
return i # 需要移动的距离
获得图片
def merge_image(image_file,location_list):
"""
拼接图片
:param image_file:
:param location_list:
:return:
"""
im = Image.open(image_file)
im.save('code.jpg')
new_im = Image.new('RGB',(260,116))
# 把无序的图片 切成52张小图片
im_list_upper = []
im_list_down = []
# print(location_list)
for location in location_list:
# print(location['y'])
if location['y'] == -58: # 上半边
im_list_upper.append(im.crop((abs(location['x']),58,abs(location['x'])+10,116)))
if location['y'] == 0: # 下半边
im_list_down.append(im.crop((abs(location['x']),0,abs(location['x'])+10,58))) x_offset = 0
for im in im_list_upper:
new_im.paste(im,(x_offset,0)) # 把小图片放到 新的空白图片上
x_offset += im.size[0] x_offset = 0
for im in im_list_down:
new_im.paste(im,(x_offset,58))
x_offset += im.size[0]
new_im.show()
return new_im def get_image(driver,div_path):
'''
下载无序的图片 然后进行拼接 获得完整的图片
:param driver:
:param div_path:
:return:
'''
time.sleep(2)
background_images = driver.find_elements_by_xpath(div_path)
location_list = []
for background_image in background_images:
location = {}
result = re.findall('background-image: url\("(.*?)"\); background-position: (.*?)px (.*?)px;',background_image.get_attribute('style'))
# print(result)
location['x'] = int(result[0][1])
location['y'] = int(result[0][2]) image_url = result[0][0]
location_list.append(location) print('==================================')
image_url = image_url.replace('webp','jpg')
# '替换url http://static.geetest.com/pictures/gt/579066de6/579066de6.webp'
image_result = requests.get(image_url).content
# with open('1.jpg','wb') as f:
# f.write(image_result)
image_file = BytesIO(image_result) # 是一张无序的图片
image = merge_image(image_file,location_list) return image

按照位移移动

print('第一步,点击滑动按钮')
ActionChains(driver).click_and_hold(on_element=element).perform() # 点击鼠标左键,按住不放
time.sleep(1)
print('第二步,拖动元素')
for track in track_list:
ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 鼠标移动到距离当前位置(x,y)
if l<100:
ActionChains(driver).move_by_offset(xoffset=-2, yoffset=0).perform()
else:
ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform()
time.sleep(1)
print('第三步,释放鼠标')
ActionChains(driver).release(on_element=element).perform()
详细代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # 等待元素加载的
from selenium.webdriver.common.action_chains import ActionChains #拖拽
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from PIL import Image
import requests
import time
import re
import random
from io import BytesIO def merge_image(image_file,location_list):
"""
拼接图片
:param image_file:
:param location_list:
:return:
"""
im = Image.open(image_file)
im.save('code.jpg')
new_im = Image.new('RGB',(260,116))
# 把无序的图片 切成52张小图片
im_list_upper = []
im_list_down = []
# print(location_list)
for location in location_list:
# print(location['y'])
if location['y'] == -58: # 上半边
im_list_upper.append(im.crop((abs(location['x']),58,abs(location['x'])+10,116)))
if location['y'] == 0: # 下半边
im_list_down.append(im.crop((abs(location['x']),0,abs(location['x'])+10,58))) x_offset = 0
for im in im_list_upper:
new_im.paste(im,(x_offset,0)) # 把小图片放到 新的空白图片上
x_offset += im.size[0] x_offset = 0
for im in im_list_down:
new_im.paste(im,(x_offset,58))
x_offset += im.size[0]
new_im.show()
return new_im def get_image(driver,div_path):
'''
下载无序的图片 然后进行拼接 获得完整的图片
:param driver:
:param div_path:
:return:
'''
time.sleep(2)
background_images = driver.find_elements_by_xpath(div_path)
location_list = []
for background_image in background_images:
location = {}
result = re.findall('background-image: url\("(.*?)"\); background-position: (.*?)px (.*?)px;',background_image.get_attribute('style'))
# print(result)
location['x'] = int(result[0][1])
location['y'] = int(result[0][2]) image_url = result[0][0]
location_list.append(location) print('==================================')
image_url = image_url.replace('webp','jpg')
# '替换url http://static.geetest.com/pictures/gt/579066de6/579066de6.webp'
image_result = requests.get(image_url).content
# with open('1.jpg','wb') as f:
# f.write(image_result)
image_file = BytesIO(image_result) # 是一张无序的图片
image = merge_image(image_file,location_list) return image def get_track(distance):
'''
拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速
匀变速运动基本公式:
①v=v0+at
②s=v0t+(1/2)at²
③v²-v0²=2as :param distance: 需要移动的距离
:return: 存放每0.2秒移动的距离
'''
# 初速度
v=0
# 单位时间为0.2s来统计轨迹,轨迹即0.2内的位移
t=0.2
# 位移/轨迹列表,列表内的一个元素代表0.2s的位移
tracks=[]
# 当前的位移
current=0
# 到达mid值开始减速
mid=distance * 7/8 distance += 10 # 先滑过一点,最后再反着滑动回来
# a = random.randint(1,3)
while current < distance:
if current < mid:
# 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细
a = random.randint(2,4) # 加速运动
else:
a = -random.randint(3,5) # 减速运动 # 初速度
v0 = v
# 0.2秒时间内的位移
s = v0*t+0.5*a*(t**2)
# 当前的位置
current += s
# 添加到轨迹列表
tracks.append(round(s)) # 速度已经达到v,该速度作为下次的初速度
v= v0+a*t # 反着滑动到大概准确位置
for i in range(4):
tracks.append(-random.randint(2,3))
for i in range(4):
tracks.append(-random.randint(1,3))
return tracks def get_distance(image1,image2):
'''
拿到滑动验证码需要移动的距离
:param image1:没有缺口的图片对象
:param image2:带缺口的图片对象
:return:需要移动的距离
'''
# print('size', image1.size) threshold = 50
for i in range(0,image1.size[0]): # 260
for j in range(0,image1.size[1]): # 160
pixel1 = image1.getpixel((i,j))
pixel2 = image2.getpixel((i,j))
res_R = abs(pixel1[0]-pixel2[0]) # 计算RGB差
res_G = abs(pixel1[1] - pixel2[1]) # 计算RGB差
res_B = abs(pixel1[2] - pixel2[2]) # 计算RGB差
if res_R > threshold and res_G > threshold and res_B > threshold:
return i # 需要移动的距离 def main_check_code(driver, element):
"""
拖动识别验证码
:param driver:
:param element:
:return:
"""
image1 = get_image(driver, '//div[@class="gt_cut_bg gt_show"]/div')
image2 = get_image(driver, '//div[@class="gt_cut_fullbg gt_show"]/div')
# 图片上 缺口的位置的x坐标 # 2 对比两张图片的所有RBG像素点,得到不一样像素点的x值,即要移动的距离
l = get_distance(image1, image2)
print('l=',l)
# 3 获得移动轨迹
track_list = get_track(l)
print('第一步,点击滑动按钮')
ActionChains(driver).click_and_hold(on_element=element).perform() # 点击鼠标左键,按住不放
time.sleep(1)
print('第二步,拖动元素')
for track in track_list:
ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 鼠标移动到距离当前位置(x,y)
time.sleep(0.002)
# if l>100:
ActionChains(driver).move_by_offset(xoffset=-random.randint(2,5), yoffset=0).perform()
time.sleep(1)
print('第三步,释放鼠标')
ActionChains(driver).release(on_element=element).perform()
time.sleep(5)
def main_check_slider(driver):
"""
检查滑动按钮是否加载
:param driver:
:return:
"""
while True:
try :
driver.get('http://www.cnbaowen.net/api/geetest/')
element = WebDriverWait(driver, 30, 0.5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'gt_slider_knob')))
if element:
return element
except TimeoutException as e:
print('超时错误,继续')
time.sleep(5)
if __name__ == '__main__':
try:
count = 6 # 最多识别6次
driver = webdriver.Chrome()
# 等待滑动按钮加载完成
element = main_check_slider(driver)
while count > 0:
main_check_code(driver,element)
time.sleep(2)
try:
success_element = (By.CSS_SELECTOR, '.gt_holder .gt_ajax_tip.gt_success')
# 得到成功标志
print('suc=',driver.find_element_by_css_selector('.gt_holder .gt_ajax_tip.gt_success'))
success_images = WebDriverWait(driver, 20).until(EC.presence_of_element_located(success_element))
if success_images:
print('成功识别!!!!!!')
count = 0
break
except NoSuchElementException as e:
print('识别错误,继续')
count -= 1
time.sleep(2)
else:
print('too many attempt check code ')
exit('退出程序')
finally:
driver.close()
成功识别标志css
本文来源作者:一只小小寄居蟹
链接:https://www.cnblogs.com/xiao-apple36/p/8878960.html
如有侵权,请联系我们删除
Python 破解极验滑动验证码的更多相关文章
- Python——破解极验滑动验证码
极验滑动验证码 以上图片是最典型的要属于极验滑动认证了,极验官网:http://www.geetest.com/. 现在极验验证码已经更新到了 3.0 版本,截至 2017 年 7 月全球已有十六万家 ...
- selenium+java破解极验滑动验证码的示例代码
转自: https://www.jianshu.com/p/1466f1ba3275 selenium+java破解极验滑动验证码 卧颜沉默 关注 2017.08.15 20:07* 字数 3085 ...
- selenium+java破解极验滑动验证码
摘要 分析验证码素材图片混淆原理,并采用selenium模拟人拖动滑块过程,进而破解验证码. 人工验证的过程 打开威锋网注册页面(https://passport.feng.com/?r=user/r ...
- python验证码识别(2)极验滑动验证码识别
目录 一:极验滑动验证码简介 二:极验滑动验证码识别思路 三:极验验证码识别 一:极验滑动验证码简介 近些年来出现了一些新型验证码,不想旧的验证码对人类不友好,但是这种验证码对于代码来说识别难度上 ...
- 破解极验(geetest)验证码
破解极验(geetest)验证码 这是两年前的帖子: http://www.v2ex.com/t/138479 一个月前的破解程序,我没用过 asp.net ,不知道是不是真的破解了, demo ...
- thinkphp整合系列之极验滑动验证码
对于建站的筒子们来说:垃圾广告真是让人深恶痛绝:为了清净:搞个难以识别的验证码吧:又被用户各种吐槽:直到后来出现了极验这个滑动的验证码:这真是一个体验好安全高的方案:官网:http://www.gee ...
- vue_drf之实现极验滑动验证码
一.需求 1,场景 我们在很多登录和注册场景里,为了避免某些恶意攻击程序,我们会添加一些验证码,也就是行为验证,让我们相信现在是一个人在交互,而不是一段爬虫程序.现在市面上用的比较多的,比较流行的是极 ...
- selenium处理极验滑动验证码
要爬取一个网站遇到了极验的验证码,这周都在想着怎么破解这个,网上搜了好多知乎上看到有人问了这问题https://www.zhihu.com/question/28833985,我按照这思路去大概实现了 ...
- luffy之多条件登录与极验滑动验证码
多条件登录 JWT扩展的登录视图,在收到用户名与密码时,也是调用Django的认证系统中提供的authenticate()来检查用户名与密码是否正确. 我们可以通过修改Django认证系统的认证后端( ...
随机推荐
- 测试Java程序执行耗费的时间
package test; public class Main { public static void main(String[] args) { long start = System.curre ...
- Travelling Businessmen Problem
Travelling Businessmen Problem 先求出图的两个部分,可能只有一个部分 然后用set模拟,得到不同部分差最小的 #include <bits/stdc++.h> ...
- 2019CSP-J游记
2019-10-19:开一个坑,今天初赛,我是我们考场唯一几个坚持到16:45收卷的人,我们是机试,竟然可以用编译器. 这次初赛总体感觉打得不错,卷面满分200,最后实际分数,就是卷面分除以二. 初赛 ...
- 吴裕雄--天生自然 PYTHON3开发学习:正则表达式
import re print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配 print(re.match('com', 'www.runoo ...
- python机器学习(2:KNN算法)
1.KNN 简介:knn算法是监督学习中分类方法的一种.它又被叫k近邻算法,是一个概念极其简单而分类效果又很优秀的分类算法. 核心思想:在训练集中选出离输入的数据最近的k个数据,根据这k个数据的类别判 ...
- 23)PHP,数组操作函数
汇总:
- LeetCode No.109,110,111
No.109 SortedListToBST 有序链表转换二叉搜索树 题目 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的 ...
- mean|mode|median|sample的表达方式
Measures of Center measures of central tendency:the center or most typical value:average Mean:its ar ...
- EXAM-2018-7-27
EXAM-2018-7-27 未完成 [ ] F A 要用ll,然后注意正方形的情况,细心一点 E 有点动态规划的感觉,状态的转移,不难,要注意不要漏掉状态 K 正解是DFS 然后用贪心数据弱的话能过 ...
- Xshell中使用xftp怎么选择默认编辑器,如nodepad
工具-选项-高级-编辑器路径