前言
由于網站注冊入口容易被黑客攻擊,存在如下安全問題:
- 暴力破解密碼,造成用戶信息泄露
- 短信盜刷的安全問題,影響業務及導致用戶投訴
- 帶來經濟損失,尤其是后付費客戶,風險巨大,造成虧損無底洞
所以大部分網站及App 都采取圖形驗證碼或滑動驗證碼等交互解決方案, 但在機器學習能力提高的當下,連百度這樣的大廠都遭受攻擊導致點名批評, 圖形驗證及交互驗證方式的安全性到底如何? 請看具體分析
一、 JJ斗地主PC 注冊入口
簡介: JJ斗地主 是由 競技世界(北京)網絡技術有限公司 開發的一款熱門在線棋牌游戲,主打競技斗地主玩法,同時包含多種創新模式和賽事體系,深受全國玩家喜愛。
1. 注冊引導頁
2. 會員注頁面
二、 安全性分析報告:
JJ斗地主采用的是自己研發的滑動驗證碼,容易被模擬器繞過甚至逆向后暴力攻擊,滑動拼圖識別率在 95% 以上。
三、 測試方法:
前端界面分析,這是JJ斗地主自己研發的滑動驗證碼,網上沒有現成的教學視頻,但形式都差不多,沒什么難度 , 這次還是采用模擬器的方式,關鍵點主要模擬器交互、距離識別和軌道算法3部分 。
1. 模擬器交互部分
private OpenCv2 openCv2 = new OpenCv2(64, 128);private static String INDEX_URL = "https://www.jj.cn/reg/reg_new.html";@Overridepublic RetEntity send(WebDriver driver, String areaCode, String phone) {RetEntity retEntity = new RetEntity();try {driver.get(INDEX_URL);// 輸入手機號WebElement phoneElement = ChromeUtil.waitElement(driver, By.id("phone_number"), 100);phoneElement.sendKeys(phone);// 點擊獲取驗證碼boolean isRobot = false;WebElement sendElement = driver.findElement(By.xpath("//button[contains(text(), '獲取驗證碼')]"));if (isRobot) {int top = 76;RobotMove.clickByTop(sendElement, top);} else {ActionMove.humanLikeClick(driver, sendElement);// ((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendElement);}// pic 1 get bigWebElement bigElement = ChromeUtil.waitElement(driver, By.className("jjcaptcha-slider-btm"), 20);if (bigElement == null) {System.out.println("bigElement=" + bigElement);return null;}byte[] bigBytes = GetImage.getCanvasAsImage(driver, "jjcaptcha-slider-btm");int bigLen = (bigBytes != null) ? bigBytes.length : 0;if (bigLen < 100) {System.out.println("bigImgUrl->bigLen=" + bigLen);return null;}// pic 2 get smallbyte[] smallBytes = GetImage.getCanvasAsImage(driver, "jjcaptcha-slider-top");if (smallBytes == null) {System.out.println("smallBytes=" + smallBytes);return null;}String ckSum = GenChecksumUtil.genChecksum(bigBytes);Map<String, Double> openResult = openCv2.getOpenCvDistance(ckSum, bigBytes, smallBytes, "JjCn", 0);if (openResult == null || openResult.size() < 2) {System.out.println("ckSum=" + ckSum + "->openResult=" + openResult);return null;}Double r = 1.1125;Double minX = openResult.get("minX");BigDecimal disD = new BigDecimal((minX + 6) * r).setScale(0, BigDecimal.ROUND_HALF_UP);int distance = disD.intValue();System.out.println("distance=" + distance);WebElement moveElement = driver.findElement(By.className("jjcaptcha-slider-block"));ActionMove.move(driver, moveElement, distance);Thread.sleep(1000);WebElement noticeElement = ChromeUtil.waitElement(driver, By.xpath("//div[@class='box_input']/span[@class='notice_no']"), 5);String notice = (noticeElement != null) ? noticeElement.getText() : null;if (notice != null) {retEntity.setMsg(notice);if (notice.contains("已注冊")) {retEntity.setRet(0);retEntity.setExist(1);}return retEntity;}WebElement infoElement = ChromeUtil.waitElement(driver, By.xpath("//button[contains(text(),'獲取中')]"), 20);String info = (infoElement != null) ? infoElement.getText() : null;retEntity.setMsg(info);if (info != null && info.contains("獲取中")) {retEntity.setRet(0);}return retEntity;} catch (Exception e) {System.out.println("phone=" + phone + ",e=" + e.toString());for (StackTraceElement ele : e.getStackTrace()) {System.out.println(ele.toString());}return null;} finally {if (driver != null)driver.manage().deleteAllCookies();}}
2. 距離識別
/*** * @param ckSum* @param bigBytes* @param smallBytes* @param factory* @return { width, maxX }*/public String[] getOpenCvDistance(String ckSum, byte bigBytes[], byte smallBytes[], String factory, int border) {try {String basePath = ConstTable.codePath + factory + "/";File baseFile = new File(basePath);if (!baseFile.isDirectory()) {baseFile.mkdirs();}// 小圖文件File smallFile = new File(basePath + ckSum + "_s.png");FileUtils.writeByteArrayToFile(smallFile, smallBytes);// 大圖文件File bigFile = new File(basePath + ckSum + "_b.png");FileUtils.writeByteArrayToFile(bigFile, bigBytes);// 邊框清理(去干擾)byte[] clearBoder = (border > 0) ? ImageIOHelper.clearBoder(smallBytes, border) : smallBytes;File tpFile = new File(basePath + ckSum + "_t.png");FileUtils.writeByteArrayToFile(tpFile, clearBoder);String resultFile = basePath + ckSum + "_o.png";return getWidth(tpFile.getAbsolutePath(), bigFile.getAbsolutePath(), resultFile);} catch (Throwable e) {logger.error("getMoveDistance() ckSum=" + ckSum + " " + e.toString());for (StackTraceElement elment : e.getStackTrace()) {logger.error(elment.toString());}return null;}}/*** Open Cv 圖片模板匹配* * @param tpPath* 模板圖片路徑* @param bgPath* 目標圖片路徑* @return { width, maxX }*/private String[] getWidth(String tpPath, String bgPath, String resultFile) {try {Rect rectCrop = clearWhite(tpPath);Mat g_tem = Imgcodecs.imread(tpPath);Mat clearMat = g_tem.submat(rectCrop);Mat cvt = new Mat();Imgproc.cvtColor(clearMat, cvt, Imgproc.COLOR_RGB2GRAY);Mat edgesSlide = new Mat();Imgproc.Canny(cvt, edgesSlide, threshold1, threshold2);Mat cvtSlide = new Mat();Imgproc.cvtColor(edgesSlide, cvtSlide, Imgproc.COLOR_GRAY2RGB);Imgcodecs.imwrite(tpPath, cvtSlide);Mat g_b = Imgcodecs.imread(bgPath);Mat edgesBg = new Mat();Imgproc.Canny(g_b, edgesBg, threshold1, threshold2);Mat cvtBg = new Mat();Imgproc.cvtColor(edgesBg, cvtBg, Imgproc.COLOR_GRAY2RGB);int result_rows = cvtBg.rows() - cvtSlide.rows() + 1;int result_cols = cvtBg.cols() - cvtSlide.cols() + 1;Mat g_result = new Mat(result_rows, result_cols, CvType.CV_32FC1);Imgproc.matchTemplate(cvtBg, cvtSlide, g_result, Imgproc.TM_CCOEFF_NORMED); // 歸一化平方差匹配法// 歸一化相關匹配法MinMaxLocResult minMaxLoc = Core.minMaxLoc(g_result);Point maxLoc = minMaxLoc.maxLoc;Imgproc.rectangle(cvtBg, maxLoc, new Point(maxLoc.x + cvtSlide.cols(), maxLoc.y + cvtSlide.rows()), new Scalar(0, 0, 255), 1);Imgcodecs.imwrite(resultFile, cvtBg);String width = String.valueOf(cvtSlide.cols());String maxX = String.valueOf(maxLoc.x + cvtSlide.cols());System.out.println("OpenCv2.getWidth() width=" + width + ",maxX=" + maxX);return new String[] { width, maxX };} catch (Throwable e) {System.out.println("getWidth() " + e.toString());logger.error("getWidth() " + e.toString());for (StackTraceElement elment : e.getStackTrace()) {logger.error(elment.toString());}return null;}}public Rect clearWhite(String smallPath) {try {Mat matrix = Imgcodecs.imread(smallPath);int rows = matrix.rows();// height -> yint cols = matrix.cols();// width -> xSystem.out.println("OpenCv2.clearWhite() rows=" + rows + ",cols=" + cols);Double rgb;double[] arr;int minX = 255;int minY = 255;int maxX = 0;int maxY = 0;Color c;for (int x = 0; x < cols; x++) {for (int y = 0; y < rows; y++) {arr = matrix.get(y, x);rgb = 0.00;for (int i = 0; i < 3; i++) {rgb += arr[i];}c = new Color(rgb.intValue());int b = c.getBlue();int r = c.getRed();int g = c.getGreen();int sum = r + g + b;if (sum >= 5) {if (x <= minX)minX = x;else if (x >= maxX)maxX = x;if (y <= minY)minY = y;else if (y >= maxY)maxY = y;}}}int boder = 1;if (boder > 0) {minX = (minX > boder) ? minX - boder : 0;maxX = (maxX + boder < cols) ? maxX + boder : cols;minY = (minY > boder) ? minY - boder : 0;maxY = (maxY + boder < rows) ? maxY + boder : rows;}int width = (maxX - minX);int height = (maxY - minY);System.out.println("openCv2 minX=" + minX + ",minY=" + minY + ",maxX=" + maxX + ",maxY=" + maxY + "->width=" + width + ",height=" + height);Rect rectCrop = new Rect(minX, minY, width, height);return rectCrop;} catch (Throwable e) {StringBuffer er = new StringBuffer("clearWrite() " + e.toString() + "\n");for (StackTraceElement elment : e.getStackTrace()) {er.append(elment.toString() + "\n");}logger.error(er.toString());System.out.println(er.toString());return null;}}
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(50);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 < 5) {Thread.sleep(5 - 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());}}
4. 圖片比對結果測試樣例:
四丶結語
J斗地主 是由 競技世界(北京)網絡技術有限公司 開發的一款熱門在線棋牌游戲,主打競技斗地主玩法,同時包含多種創新模式和賽事體系,深受全國玩家喜愛,作為受眾巨大的網絡游戲廠商,技術實力雄厚, 人才濟濟,采用的是自己研發的滑動驗證產品, 在一定程度上提高了用戶體驗, 不過隨著圖形識別技術及機器學習能力的提升,所以在網上破解的文章和教學視頻也是大量存在,并且經過驗證的確有效, 所以除了滑動驗證方式, 花樣百出的產品層出不窮,但本質就是犧牲用戶體驗來提高安全。
很多人在短信服務剛開始建設的階段,可能不會在安全方面考慮太多,理由有很多。
比如:“ 需求這么趕,當然是先實現功能啊 ”,“ 業務量很小啦,系統就這么點人用,不怕的 ” , “ 我們怎么會被盯上呢,不可能的 ”等等。有一些理由雖然有道理,但是該來的總是會來的。前期欠下來的債,總是要還的。越早還,問題就越小,損失就越低。
所以大家在安全方面還是要重視。(血淋淋的栗子!)#安全短信#
戳這里→康康你手機號在過多少網站注冊過!!!
谷歌圖形驗證碼在AI 面前已經形同虛設,所以谷歌宣布退出驗證碼服務, 那么當所有的圖形驗證碼都被破解時,大家又該如何做好防御呢?
>>相關閱讀
《騰訊防水墻滑動拼圖驗證碼》
《百度旋轉圖片驗證碼》
《網易易盾滑動拼圖驗證碼》
《頂象區域面積點選驗證碼》
《頂象滑動拼圖驗證碼》
《極驗滑動拼圖驗證碼》
《使用深度學習來破解 captcha 驗證碼》
《驗證碼終結者-基于CNN+BLSTM+CTC的訓練部署套件》