好像挺久之前就看到滑动验证码,然后最近又看见一些文章,好像不是很难,就试了一下
主要是使用PhantomJS模拟浏览器,然后识别滑动距离,再进行滑动。
也有人直接模拟数据包,不过看不太懂js就只能用模拟浏览器的方法了。
网上很多例子了就不多说
先截图获得带拼图的与原图进行反相
直接计算纵坐标上像素点灰度超过指定值的点的数量就得出坐标,接着模拟滑动就完成了。
只是移动的坐标需要混淆一下,太机械不行。
代码先运行js,等需要输入时运行image.py得出坐标,输入即可。
因为不知道PhantomJS怎么跟py通信哈哈,所以手速要快。验证码的Demo是github上下的,测试通过率还行,可能服务器带学习的话就要改改坐标算法。
不是很懂js所以写的很老土哈哈
js:
function waitFor(testFx, onReady, timeOutMillis) {
var _x = 86;
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 2000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("init ok");
page.evaluate(function() {
document.getElementById('popup-submit').click();
});
clearInterval(interval);
start2 = new Date().getTime(),
condition = false,
interval2 = setInterval(function() {
if ( (new Date().getTime() - start2 < maxtimeOutMillis) ) {
console.log('.');
}else{
clearInterval(interval2);
page.render('aa.png');
page.evaluate(function() {
var mouseMoveArr = [[122,30],[122,30],[122,30],[122,30],[122,30],[122,30],[122,30],[122,30],[122,30],[122,30]];
var createEvent = function(eventName, ofsx, ofsy) {var ev = document.createEvent('MouseEvents');
ev.initMouseEvent(eventName, true, false, null, 0, 0, 0, ofsx, ofsy, false, false, false, false, 0, null); return ev;};
//createEvent("click",200,500);
var _auto_down = createEvent("mousedown",86,876);
var _obj = document.querySelectorAll(".gt_slider_knob");
_obj = _obj[1];
_obj.dispatchEvent(_auto_down);
});
//等出现拼图
waitStart = new Date().getTime();
intervalWait = setInterval(function(){
if(new Date().getTime() - waitStart > 1000){
clearInterval(intervalWait);
page.render('bb.png');
//获取位移量
var input = ''; var sys = require('system');
console.log('input:');
input = sys.stdin.readLine();
console.log(input);
var moveInt = parseInt(input);
var startX = 86;
console.log('distance:',moveInt);
var distance = moveInt - 80;
function getX(index){
var temp = 0;
if (index < 7){
temp = 80 + 1.169 * index * distance/10 - (index/10) * (index/10) * 17;
}else{
temp = 80 + distance * index / 10 + (1.169 * index * distance/10 - (index/10) * (index/10) * 17 - distance * index / 10) * (index / 10);
}
return temp;
};
console.log(getX(0),getX(1),getX(2),getX(3),getX(4),getX(5),getX(6),getX(7),getX(8),getX(9));
page.evaluate(function(input) {
var startX = 86;
var distance = input - 80;
function getX(index){
var temp = 0;
if (index < 7){
temp = 1.169 * index * distance / 10 - (index/10) * (index/10) * 17;
}else{
temp = distance * index / 10 + (1.169 * index * distance - (index/10) * (index/10) * 17 - distance * index / 10) * (index / 10);
}
return temp;
};
var mouseMoveArr = [[getX(0),876],[getX(1),876],[getX(2),876],[getX(3),876],[getX(4),876],[getX(5),876],[getX(6),876],[getX(7),876],[getX(8),876],[input,876]];
var createEvent = function(eventName, ofsx, ofsy) {var ev = document.createEvent('MouseEvents');
ev.initMouseEvent(eventName, true, false, null, 0, 0, 0, ofsx, ofsy, false, false, false, false, 0, null); return ev;};
//createEvent("click",200,500);
var _auto_down = createEvent("mousedown",86,876);
//var _obj = document.getElementById("popup-submit");
var _obj = document.querySelectorAll(".gt_slider_knob");
_obj = _obj[1];
//_obj.dispatchEvent(_auto_down);
MoveStart = new Date().getTime();
var moveIndex = 0;
IntervalMove = setInterval(function(){
//var moveIndex = Math.round(Math.random() * (mouseMoveArr.length - 1));
_obj.dispatchEvent(createEvent("mousemove",mouseMoveArr[moveIndex][0],mouseMoveArr[moveIndex][1]));
moveIndex = moveIndex + 1;
if (moveIndex >= mouseMoveArr.length - 1){
moveIndex = mouseMoveArr.length - 1;
}
if(new Date().getTime() - MoveStart > 1256){
clearInterval(IntervalMove);
_obj.dispatchEvent(createEvent("mouseup",mouseMoveArr[9][0],mouseMoveArr[9][1]));
}
},100);
},moveInt+78);
start3 = new Date().getTime(),
interval3 = setInterval(function() {
if ( (new Date().getTime() - start3 < 3000) ) {
console.log('%d',_x);
_x = _x + 1;
if(_x == 87){
page.render('00.png');
}
if(_x == 88){
page.render('01.png');
}
if(_x == 90){
page.render('02.png');
}
if(_x == 92){
page.render('03.png');
}
//page.sendEvent("mousemove",_x,876);
}else{
clearInterval(interval3);
page.render('dd.png');
phantom.exit();
}
},250);
}else{
console.log('d')
}
},100);
//_obj.dispatchEvent(createEvent("mousemove",150,876));
}
},250);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.open('http://127.0.0.1:5000/', function(status) {
console.log("Status: " + status);
if(status == "success") {
waitFor(function check() { return page.evaluate(function () { return (document.querySelectorAll('.gt_cut_bg_slice').length >= 50) && (document.querySelectorAll('.gt_cut_fullbg_slice').length <= 50);//确保页面已经渲染完成,出现了背景图
});
}, function() {
console.log("The x should be visible now.");
page.evaluate(function() {document.getElementById('popup-submit').click();});
waitFor(1,1==1);
console.log("clicked");
page.render('example2.png');
phantom.exit();
});
}
});
image.py:
#coding:utf-8
from PIL import Image
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
img1 = Image.open("aa.png").convert('L')
img2 = Image.open("bb.png").convert('L')
region = (65,718,324,832)
#裁切图片
cropImg1 = img1.crop(region)
cropImg2 = img2.crop(region)
x,y = cropImg1.size
print x,y
#反相
img1 = array(cropImg1)
img2 = img1 - array(cropImg2)
#保存裁切后的图片
cropImg1.save('crop1.jpg')
cropImg2.save('crop2.jpg')
for i in range(x):
for j in range(y):
#print img2[j,i],j,i
if img2[j,i] >= 230: #255白色
img2[j,i] = 0
img3 = Image.fromarray(img2)
img3.save("fanxiang.jpg")
pixCount = [0] * x
for i in range(x):
for j in range(y):
if img2[j,i] > 100:
pixCount[i] = pixCount[i] + 1
print pixCount
for i in range(x):
if i > 61 and pixCount[i] > 20:
print i
endx = i
break
img=np.array(pixCount)
plt.bar(range(len(pixCount)), pixCount)
#plt.show()