前言
由于网站注册入口容易被黑客攻击,存在如下安全问题:
- 暴力破解密码,造成用户信息泄露
- 短信盗刷的安全问题,影响业务及导致用户投诉
- 带来经济损失,尤其是后付费客户,风险巨大,造成亏损无底洞
所以大部分网站及App 都采取图形验证码或滑动验证码等交互解决方案, 但在机器学习能力提高的当下,连百度这样的大厂都遭受攻击导致点名批评, 图形验证及交互验证方式的安全性到底如何? 请看具体分析
一、 宝马中国PC 注册入口
简介:宝马集团是全世界最成功的汽车和摩托车制造商之一,拥有BMW、MINI、Rolls-Royce和BMW Motorrad四大品牌,并提供汽车金融和高档出行服务。作为一家全球性公司,宝马集团在全球设有超过30处生产布局;销售网络遍及140多个国家和地区。
宝马中国, 1994年4月,宝马集团在华设立代表处——宝马汽车公司北京代表处,标志着宝马集团正式进入大中华区市场。2005年10月,宝马(中国)汽车贸易有限公司成立,这是宝马集团对中国市场长期承诺的又一里程碑。
二、 安全性分析报告:
宝马中国技术团队自己研发的滑动验证码,容易被模拟器绕过甚至逆向后暴力攻击,滑动拼图识别率在 95% 以上。
三、 测试方法:
前端界面分析,这是宝马中国技术团队自己研发的滑动验证码,网上没有现成的教学视频,但形式都差不多,难点:
1. 将背景大图填充了白色颜色,使得标准的匹配方法存在问题,匹配率特别低,应对的方法是:从背景图选取一张白色的图片样张解决
这次还是采用模拟器的方式,关键点主要模拟器交互、距离识别和轨道算法3部分
选取出的样图:
- 模拟器交互部分
private OpenCv2 openCv2 = new OpenCv2(64, 128);
private static String INDEX_URL = "https://login.bmw.com.cn/user/register";
@Override
public RetEntity send(WebDriver driver, String areaCode, String phone) {
RetEntity retEntity = new RetEntity();
try {
driver.get(INDEX_URL);
Thread.sleep(1000);
WebElement phoneElemet = ChromeDriverManager.waitElement(driver, By.name("copInputMobile"), 10);
phoneElemet.sendKeys(phone);
// 我已阅读并同意
WebElement statusElement = ChromeDriverManager.waitElement(driver, By.className("cop-check-box-in-bmw"), 1);
if (statusElement == null) {
WebElement agreeElement = driver.findElement(By.xpath("//div[contains(text(),'我已阅读并同意')]"));
agreeElement.click();
}
WebElement sendElement = driver.findElement(By.name("copVCodePinButton"));
sendElement.click();
Thread.sleep(1000);
// pic 1 get big
WebElement bigImgElement = driver.findElement(By.className("imgBg"));
String bigBase64 = bigImgElement.getAttribute("src");
byte[] bigBytes = (bigBase64 != null) ? GetImage.imgStrToByte(bigBase64) : null;
if (bigBytes == null) {
System.out.println("bigBase64=" + bigBase64 + "->bigBytes=" + bigBytes);
return null;
}
// pic 2 get small
byte[] smallBytes = null;
boolean isLocalTp = true;
if (isLocalTp) {
File smallFile = new File("c:\\chrome\\bmw.png");
smallBytes = FileUtils.readFileToByteArray(smallFile);
} else {
WebElement smallImgElement = driver.findElement(By.className("imgCut"));
String smallBase64 = smallImgElement.getAttribute("src");
smallBytes = GetImage.imgStrToByte(smallBase64);
}
if (smallBytes == null) {
System.out.println("smallBytes=" + smallBytes);
return null;
}
String ckSum = GenChecksumUtil.genChecksum(bigBytes);
String[] openResult = openCv2.getOpenCvDistance(ckSum, bigBytes, smallBytes, "Bmw", 0);
if (openResult == null || openResult.length < 2) {
System.out.println("ckSum=" + ckSum + "->openResult=" + openResult);
return null;
}
Double r = 300 * 1.0 / 590;
Double w = Double.parseDouble(openResult[0]);
Double d = Double.parseDouble(openResult[1]);
Double offSet = d / 590 * 22;
BigDecimal disD = new BigDecimal((d - w - offSet) * r).setScale(0, BigDecimal.ROUND_HALF_UP);
int distance = disD.intValue();
WebElement moveElement = driver.findElement(By.className("slider2"));
ActionMove.move(driver, moveElement, distance);
Thread.sleep(500);
WebElement errElement = ChromeDriverManager.waitElement(driver, By.className("msgError"), 1);
String errInfo = (errElement != null) ? errElement.getText() : null;
if (errInfo != null && errInfo.contains("校验失败")) {
System.out.println("errInfo=" + errInfo);
return retEntity;
}
String msg = sendElement.getText();
retEntity.setMsg(msg);
if (msg != null && msg.contains("秒")) {
retEntity.setRet(0);
} else {
Thread.sleep(1500);
WebElement miniRerrorElement = ChromeDriverManager.waitElement(driver, By.className("mini-error"), 5);
String miniRerror = (miniRerrorElement != null) ? miniRerrorElement.getText() : null;
if (miniRerror != null) {
System.out.println("miniRerror=" + miniRerror);
} else {
System.out.println("msg=" + msg);
}
}
retEntity.setRet(0);
return retEntity;
} catch (Exception e) {
System.out.println(e.toString());
retEntity.setRet(-9);
return retEntity;
} finally {
driver.manage().deleteAllCookies();
}
}
- 获取滑动图片及调用移动交互
public boolean getAndMove(WebDriver driver, Integer offSet) {
int distance = -1;
try {
WebElement moveElement = ChromeDriverManager.waitElement(driver, By.className("geetest_slider_button"), 1000);
if (moveElement == null) {
logger.error("getAndMove() moveElement=" + moveElement);
return false;
}
// 下面的js代码根据canvas文档说明而来
// 完整背景图geetest_canvas_fullbg geetest_fade geetest_absolute
StringBuffer base64 = new StringBuffer();
String fullName = "geetest_canvas_fullbg geetest_fade geetest_absolute";
byte[] fullImg = GetImage.callJsByName(driver, fullName, base64);
String bgName = "geetest_canvas_bg geetest_absolute";
byte[] bgImg = GetImage.callJsByName(driver, bgName, base64);
File fullFile = null, bgFile = null;
if (fullImg != null && bgImg != null) {
Long time = System.currentTimeMillis();
fullFile = new File(dataPath + "geet/" + time + "full.png");
FileUtils.writeByteArrayToFile(fullFile, fullImg);
bgFile = new File(dataPath + "geet/" + time + "bg.png");
FileUtils.writeByteArrayToFile(bgFile, bgImg);
if (fullImg.length < 10000) {
System.out.println("fullImg len=" + fullImg.length + " -> err[len<10000]");
return false;
}
}
// 获取滑动距离并删除图片
distance = (fullFile != null && bgFile != null) ? ActionMove.getMoveDistance(fullFile.getAbsolutePath(), bgFile.getAbsolutePath()) : -1;
if (distance < 1) {
logger.error("getAndMove distance=" + distance);
return false;
}
if (offSet != null)
ActionMove.move(driver, moveElement, distance - offSet);
else
ActionMove.move(driver, moveElement, distance);
// 滑动结果
Thread.sleep(1 * 1000);
WebElement infoElement = ChromeDriverManager.getInstance().waitForLoad(By.className("geetest_result_content"), 10);
String gtInfo = (infoElement != null) ? infoElement.getAttribute("innerText") : null;
if (gtInfo != null) {
System.out.println("gtInfo=" + gtInfo);
if (gtInfo.contains("速度超过") || gtInfo.contains("通过验证")) {
return true;
}
} else {
String msg = driver.findElement(By.className("geetest_panel_success_title")).getAttribute("innerText");
System.out.println("msg=" + msg);
}
return false;
} catch (Exception e) {
System.out.println("getAndMove() " + e.toString());
logger.error(e.toString());
return false;
}
}
2. 距离识别
/**
* 计算需要平移的距离
*
* @param fullImgPath
* 完整背景图片文件名
* @param bgImgPath含有缺口背景图片文件名
* @return
* @throws IOException
*/
public static int getMoveDistance(String fullImgPath, String bgImgPath) {
System.out.println("fullImgPath=" + fullImgPath);
File fullFile = new File(fullImgPath);
File bgFile = new File(bgImgPath);
boolean fullExists = fullFile.exists();
boolean bgExists = bgFile.exists();
if (fullExists && bgExists) {
String abPath = bgFile.getAbsolutePath();
int l = abPath.lastIndexOf(".");
String out = abPath.substring(0, l) + "-o" + abPath.substring(l);
return getComareImg(fullFile, bgFile, out);
} else {
System.out.println("fullExists(" + fullImgPath + ")=" + fullExists + "\nbgExists(" + bgImgPath + ")=" + bgExists);
return -1;
}
}
/**
* 计算需要平移的距离
*
* @param driver
* @param fullImgPath完整背景图片文件名
* @param bgImgPath含有缺口背景图片文件名
* @return
* @throws IOException
*/
private static int getComareImg(Object fullObj, Object bgObj, String out) {
System.out.println("getComareImg() begin");
try {
if (fullObj == null || bgObj == null) {
return -1;
}
BufferedImage fullBI = (fullObj instanceof File) ? ImageIO.read((File) fullObj) : ImageIO.read((ByteArrayInputStream) fullObj);
BufferedImage bgBI = (bgObj instanceof File) ? ImageIO.read((File) bgObj) : ImageIO.read((ByteArrayInputStream) bgObj);
List<Integer> list;
Color ca, cb;
Map<Integer, List<Integer>> xMap = new TreeMap<Integer, List<Integer>>();
// 将头35列的最大不同值取出, 作为右边图像的基础差
Long tifTotl = 0L;
int tifLeft = 0;
int tifCount = 0;
for (int i = 0; i < bgBI.getWidth(); i++) {
for (int j = 0; j < bgBI.getHeight(); j++) {
ca = new Color(fullBI.getRGB(i, j));
cb = new Color(bgBI.getRGB(i, j));
int diff = diff(ca, cb);
if (i <= 35 && tifLeft < diff) {
tifLeft = (diff >= 255) ? 255 : diff;
} else if (diff > tifLeft) {
tifTotl += diff;
tifCount++;
}
}
}
Long tifAvg = (tifCount > 0) ? (tifTotl / tifCount) : 0L;
if (tifLeft <= 0 && tifAvg >= 2) {
tifAvg = tifAvg / 2;
}
for (int i = 35; i < bgBI.getWidth(); i++) {
for (int j = 0; j < bgBI.getHeight(); j++) {
ca = new Color(fullBI.getRGB(i, j));
cb = new Color(bgBI.getRGB(i, j));
int diff = diff(ca, cb);
if (diff >= tifAvg) {
list = xMap.get(i);
if (list == null) {
list = new ArrayList<Integer>();
xMap.put(i, list);
}
list.add(j);
xMap.put(i, list);
}
}
}
System.out.println(" |--tifLeft=" + tifLeft + ",tifTotl=" + tifTotl + ",tifCount=" + tifCount + ",tifAvg=" + tifAvg + ",xMap.size=" + xMap.size());
int minX = 0;
int maxX = 0;
for (Integer x : xMap.keySet()) {
list = xMap.get(x);
minX = (minX == 0) ? x : minX;
maxX = x;
for (int y : list) {
cb = new Color(bgBI.getRGB(x, y));
int gray = (int) (0.3 * cb.getRed() + 0.59 * cb.getGreen() + 0.11 * cb.getBlue());
bgBI.setRGB(x, y, gray);
}
}
// 标记直线位置
for (int y = 0; y < bgBI.getHeight(); y++) {
bgBI.setRGB(minX, y, Color.red.getRGB());
}
int width = maxX - minX;
File destFile = new File(out);
Thumbnails.of(bgBI).scale(1f).toFile(destFile);
System.out.println(" |---xMap.size=" + xMap.size() + " minX=" + minX + ",maxX=" + maxX + ",width=" + width);
return minX;
} catch (Exception e) {
System.out.println(e.toString());
for (StackTraceElement elment : e.getStackTrace()) {
System.out.println(elment.toString());
}
logger.error("getMoveDistance() err = " + e.toString());
return 0;
}
}
private static int diff(Color ca, Color cb) {
int d = Math.abs(ca.getRed() - cb.getRed()) + Math.abs(ca.getGreen() - cb.getGreen()) + Math.abs(ca.getBlue() - ca.getBlue());
return d;
}
3. 轨道生成及移动算法
/**
* 双轴轨道生成算法,主要实现平滑加速和减速
*
* @param distance
* @return
*/
public static List<Integer[]> getXyTrack(int distance) {
List<Integer[]> track = new ArrayList<Integer[]>();// 移动轨迹
try {
int a = (int) (distance / 3.0) + random.nextInt(10);
int h = 0, current = 0;// 已经移动的距离
BigDecimal midRate = new BigDecimal(0.7 + (random.nextInt(10) / 100.00)).setScale(4, BigDecimal.ROUND_HALF_UP);
BigDecimal mid = new BigDecimal(distance).multiply(midRate).setScale(0, BigDecimal.ROUND_HALF_UP);// 减速阈值
BigDecimal move = null;// 每次循环移动的距离
List<Integer[]> subList = new ArrayList<Integer[]>();// 移动轨迹
boolean plus = true;
Double t = 0.18, v = 0.00, v0;
while (current <= distance) {
h = random.nextInt(2);
if (current > distance / 2) {
h = h * -1;
}
v0 = v;
v = v0 + a * t;
move = new BigDecimal(v0 * t + 1 / 2 * a * t * t).setScale(4, BigDecimal.ROUND_HALF_UP);// 加速
if (move.intValue() < 1)
move = new BigDecimal(1L);
if (plus) {
track.add(new Integer[] { move.intValue(), h });
} else {
subList.add(0, new Integer[] { move.intValue(), h });
}
current += move.intValue();
if (plus && current >= mid.intValue()) {
plus = false;
move = new BigDecimal(0L);
v = 0.00;
}
}
track.addAll(subList);
int bk = current - distance;
if (bk > 0) {
for (int i = 0; i < bk; i++) {
track.add(new Integer[] { -1, h });
}
}
System.out.println("getMoveTrack(" + midRate + ") a=" + a + ",distance=" + distance + " -> mid=" + mid.intValue() + " size=" + track.size());
return track;
} catch (Exception e) {
System.out.print(e.toString());
return null;
}
}
/**
* 模拟人工移动
*
* @param driver
* @param element页面滑块
* @param distance需要移动距离
* @throws InterruptedException
*/
public static void move(WebDriver driver, WebElement element, int distance) throws InterruptedException {
List<Integer[]> track = getXyTrack(distance);
if (track == null || track.size() < 1) {
System.out.println("move() track=" + track);
}
int moveY, moveX;
StringBuffer sb = new StringBuffer();
try {
Actions actions = new Actions(driver);
actions.clickAndHold(element).perform();
Thread.sleep(20);
long begin, cost;
Integer[] move;
int sum = 0;
for (int i = 0; i < track.size(); i++) {
begin = System.currentTimeMillis();
move = track.get(i);
moveX = move[0];
sum += moveX;
moveY = move[1];
if (moveX < 0) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(moveX);
}
actions.moveByOffset(moveX, moveY).perform();
cost = System.currentTimeMillis() - begin;
if (cost < 3) {
Thread.sleep(3 - cost);
}
}
if (sb.length() > 0) {
System.out.println("-----backspace[" + sb.toString() + "]sum=" + sum + ",distance=" + distance);
}
Thread.sleep(180);
actions.release(element).perform();
Thread.sleep(500);
} catch (Exception e) {
StringBuffer er = new StringBuffer("move() " + e.toString() + "\n");
for (StackTraceElement elment : e.getStackTrace())
er.append(elment.toString() + "\n");
logger.error(er.toString());
System.out.println(er.toString());
}
}
- 图片比对结果测试样例:
四丶结语
宝马中国作为全世界最成功的汽车和摩托车制造商之一的宝马集团旗下公司, 其滑动验证码没有采用市场常用的几家, 有独特的地方, 背景图在被抠出后,并不是采用同行的常用的透明化保留原有图形样式,而是填充白色,所以滑块位置识别需要从背景图中提取, 这样让识别变得更简单,
造成这中特点有两方面原因,
1 直接填充白色背景的技术比透明度的方式更简单
2 从报文看未获取轨迹数据, 显示验证方式中并未验证轨迹,只验证距离是否合适
很多人在短信服务刚开始建设的阶段,可能不会在安全方面考虑太多,理由有很多。
比如:“ 需求这么赶,当然是先实现功能啊 ”,“ 业务量很小啦,系统就这么点人用,不怕的 ” , “ 我们怎么会被盯上呢,不可能的 ”等等。有一些理由虽然有道理,但是该来的总是会来的。前期欠下来的债,总是要还的。越早还,问题就越小,损失就越低。
所以大家在安全方面还是要重视。(血淋淋的栗子!)#安全短信#
戳这里→康康你手机号在过多少网站注册过!!!
谷歌图形验证码在AI 面前已经形同虚设,所以谷歌宣布退出验证码服务, 那么当所有的图形验证码都被破解时,大家又该如何做好防御呢?
>>相关阅读
《腾讯防水墙滑动拼图验证码》
《百度旋转图片验证码》
《网易易盾滑动拼图验证码》
《顶象区域面积点选验证码》
《顶象滑动拼图验证码》
《极验滑动拼图验证码》
《使用深度学习来破解 captcha 验证码》
《验证码终结者-基于CNN+BLSTM+CTC的训练部署套件》