滑块验证码识别 java版本
https://blog.csdn.net/qq_19383667/article/details/77879895
好久没有更新技术文章了,很久之前研究过滑块验证码的破解,照着别人的代码改,将其他版本的代码改成java的,加上自己的一些研究,凑合凑合出了第一个java版本的,此版本不是控制浏览器进行验证,纯java控制(包含java js引擎执行js),目前都完成了,只是滑动轨迹的生成被后台识别出是机器人,除了这个问题以外,其他的均没有任何问题!如果谁能弄出轨迹算法出来,可以留言或者加我qq:1069478446,不胜感激
废话不多说,自己上代码:
package com.test;
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import net.sf.json.JSONObject;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.util.DBUtil;
public class Geek {
DBUtil dbu=new DBUtil();
CloseableHttpClient httpclient = HttpClients.createDefault();
String challenge="";
String gt="";
String json="";
Random rnd = new Random();
public boolean checkVcome()
{
String url="http://www.tianyancha.com/company/7117464";
HttpGet httpPost=new HttpGet(url);
String html="";
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entitySort = response.getEntity();
html=EntityUtils.toString(entitySort, "utf-8");
if(html.indexOf("为确认本次访问为正常用户行为,请您协助验证")!=-1)
return true;
if(!"".equals(html.trim()))
{
return true;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void startPama()
{
String url="http://www.gsxt.gov.cn/SearchItemCaptcha?v="+System.currentTimeMillis();
HttpGet httpPost=new HttpGet(url);
httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("Host", "www.gsxt.gov.cn");
httpPost.setHeader("Referer", "http://www.gsxt.gov.cn/corp-query-homepage.html");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
String html="";
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entitySort = response.getEntity();
html=EntityUtils.toString(entitySort, "utf-8");
System.out.println(html);
JSONObject json=JSONObject.fromObject(html);
this.challenge=json.getString("challenge");
this.gt=json.getString("gt");
System.out.println("gt:"+gt);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getpage_comp()
{
String url="http://api.geetest.com/get.php?gt=#{gt}&challenge=#{challenge}&product=embed&offline=false";
HttpGet httpPost=new HttpGet(url.replace("#{gt}", this.gt).replace("#{challenge}", this.challenge));
httpPost.setHeader("Connection", "Keep-Alive");
httpPost.setHeader("Host", "api.geetest.com");
httpPost.setHeader("Pragma", "no-cache");
httpPost.setHeader("Referer", "http://www.geetest.com/exp_normal");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
String html="";
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entitySort = response.getEntity();
html=EntityUtils.toString(entitySort, "utf-8");
int sindex=html.indexOf("new Geetest(")+12;
if(sindex<1)
return;
int eindex=html.indexOf(",true");
this.json=html.substring(sindex, eindex);
JSONObject json=JSONObject.fromObject(this.json);
this.challenge=json.getString("challenge");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getImage(String name,String url)
{
System.out.println(url);
try {
HttpGet httpimg = new HttpGet(url);
httpimg.setHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
httpimg.setHeader("Connection", "keep-alive");
httpimg.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpimg.setHeader("Host", "static.geetest.com");
httpimg.setHeader("Upgrade-Insecure-Requests", "1");
httpimg.setHeader("Referer", "http://www.geetest.com/exp_normal");
HttpResponse responseimg = httpclient.execute(httpimg);
File file=new File("E:\\zjt\\img\\"+name+".jpg");
OutputStream out=new FileOutputStream(file);
responseimg.getEntity().writeTo(out);
out.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "E:\\zjt\\img\\"+name+".jpg";
}
/**
* 合成指定的多张图片到一张图片
*
* @param imgSrcList 图片的地址列表
* @param topLeftPointList 每张小图片的偏移量
* @param countOfLine 每行的小图片个数
* @param cutWidth 每张小图片截取的宽度(像素)
* @param cutHeight 每张小图片截取的高度(像素)
* @param savePath 合并后图片的保存路径
* @param subfix 合并后图片的后缀
* @return 是否合并成功
*/
public static boolean combineImages(ArrayList<String> imgSrcList, ArrayList<String[]> topLeftPointList, int countOfLine, int cutWidth, int cutHeight, String savePath, String subfix) {
if (imgSrcList == null || savePath == null || savePath.trim().length() == 0)
return false;
BufferedImage lastImage = new BufferedImage(cutWidth * countOfLine, cutHeight * ((int) (Math.floor(imgSrcList.size() / countOfLine))), BufferedImage.TYPE_INT_RGB);
String prevSrc = "";
BufferedImage prevImage = null;
try {
for (int i = 0; i < imgSrcList.size(); i++) {
String src = imgSrcList.get(i);
BufferedImage image;
if (src.equals(prevSrc))
image = prevImage;
else {
if (src.trim().toLowerCase().startsWith("http"))
image = ImageIO.read(new URL(src));
else
image = ImageIO.read(new File(src));
prevSrc = src;
prevImage = image;
}
if (image == null)
continue;
String[] topLeftPoint = topLeftPointList.get(i);
int[] pixArray = image.getRGB(0 - Integer.parseInt(topLeftPoint[0].trim()), 0 - Integer.parseInt(topLeftPoint[1].trim()), cutWidth, cutHeight, null, 0, cutWidth);
int startX = ((i) % countOfLine) * cutWidth;
int startY = ((i) / countOfLine) * cutHeight;
lastImage.setRGB(startX, startY, cutWidth, cutHeight, pixArray, 0, cutWidth);
}
File file = new File(savePath);
return ImageIO.write(lastImage, subfix, file);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
/**
* 通过背景的完整图片与有缺失的图片进行对比,得到偏移量
* @param imgSrc1 有缺失的图片路径
* @param imgSrc2 完整的背景图路径
* @return
*/
public static int findXDiffRectangeOfTwoImage(String imgSrc1, String imgSrc2) {
try {
BufferedImage image1 = ImageIO.read(new File(imgSrc1));
BufferedImage image2 = ImageIO.read(new File(imgSrc2));
int width1 = image1.getWidth();
int height1 = image1.getHeight();
int width2 = image2.getWidth();
int height2 = image2.getHeight();
if (width1 != width2) return -1;
if (height1 != height2) return -1;
int left = 0;
/**
* 从左至右扫描
*/
boolean flag = false;
for (int i = 0; i < width1; i++) {
for (int j = 0; j < height1; j++)
if (isPixelNotEqual(image1, image2, i, j)) {
left = i;
flag = true;
break;
}
if (flag) break;
}
return left;
} catch (Exception ex) {
ex.printStackTrace();