从12306网站新验证码看Web验证码设计与破解

2015年3月16日,铁路官方购票网站12306又出新招,在登录界面推出了全新的验证方式,用户在填写好登录名和密码之后,还要准确的选取图片验证码才能登陆成功。据悉,12306验证码改版后,目前所有抢票工具都已经无法登录。

多么惨绝人寰的消息,小编相信各大互联网公司都在潜心钻研新的抢票助手,来破解全新的验证码模式。

下面小编带大家看看各种验证码的设计原理及其破解方法。

首先是纯文本式验证码,是比较原始的一种。

这种验证码并不符合验证码的定义,因为只有自动生成的问题才能用做验证码,这种文字验证码都是从题库里选择出来的,数量有限。破解方式也很简单,多 刷新几次,建立题库和对应的答案,用正则从网页里抓取问题,寻找匹配的答案后破解。也有些用随机生成的数学公式,比如 随机数 [+-*/]随机运算符 随机数=?,小学生水平的程序员也可以搞定……

这种验证码也不是一无是处,对于很多见到表单就来一发的spam bot来说,实在没必要单独为了一个网站下那么大功夫。对于铁了心要在你的网站大量灌水的人,这种验证码和没有一样。

第二个是目前比较主流的图片验证码:

这类图片验证码的原理就是通过字符的粘连增加及其识别的难度,而上边这种一般用于不大的网站。

这类验证码处理方式:

图片预处理

怎么去掉背景干扰呢?可以注意到每个验证码数字或字母都是同一颜色,所以把验证码平均分成5份

计算每个区域的颜色分布,除了白色之外,颜色值最多的就是验证码的颜色,因此很容易将背景去掉

代码:

  1. 1.public static BufferedImage removeBackgroud(String picFile)
  2. 2.            throws Exception {
  3. 3.        BufferedImage img = ImageIO.read(new File(picFile));
  4. 4.        img = img.getSubimage(1, 1, img.getWidth() - 2, img.getHeight() - 2);
  5. 5.        int width = img.getWidth();
  6. 6.        int height = img.getHeight();
  7. 7.        double subWidth = (double) width / 5.0;
  8. 8.        for (int i = 0; i < 5; i++) {
  9. 9.            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  10. 10.            for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth
  11. 11.                    && x < width - 1; ++x) {
  12. 12.                for (int y = 0; y < height; ++y) {
  13. 13.                    if (isWhite(img.getRGB(x, y)) == 1)
  14. 14.                        continue;
  15. 15.                    if (map.containsKey(img.getRGB(x, y))) {
  16. 16.                        map.put(img.getRGB(x, y), map.get(img.getRGB(x, y)) + 1);
  17. 17.                    } else {
  18. 18.                        map.put(img.getRGB(x, y), 1);
  19. 19.                    }
  20. 20.                }
  21. 21.            }
  22. 22.            int max = 0;
  23. 23.            int colorMax = 0;
  24. 24.            for (Integer color : map.keySet()) {
  25. 25.                if (max < map.get(color)) {
  26. 26.                    max = map.get(color);
  27. 27.                    colorMax = color;
  28. 28.                }
  29. 29.            }
  30. 30.            for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth
  31. 31.                    && x < width - 1; ++x) {
  32. 32.                for (int y = 0; y < height; ++y) {
  33. 33.                    if (img.getRGB(x, y) != colorMax) {
  34. 34.                        img.setRGB(x, y, Color.WHITE.getRGB());
  35. 35.                    } else {
  36. 36.                        img.setRGB(x, y, Color.BLACK.getRGB());
  37. 37.                    }
  38. 38.                }
  39. 39.            }
  40. 40.        }
  41. 41.        return img;
得到与下图

接着是对图片进行纵向扫描进行切割。

再对每一部分横向扫描

然后进行训练

最后因为固定大小,识别跟 验证码识别--1 里面一样,像素比较就可以了。

源码:

  1. 1.public class ImagePreProcess2 {
  2. 2.
  3. 3.    private static Map<BufferedImage, String> trainMap = null;
  4. 4.    private static int index = 0;
  5. 5.
  6. 6.    public static int isBlack(int colorInt) {
  7. 7.        Color color = new Color(colorInt);
  8. 8.        if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {
  9. 9.            return 1;
  10. 10.        }
  11. 11.        return 0;
  12. 12.    }
  13. 13.
  14. 14.    public static int isWhite(int colorInt) {
  15. 15.        Color color = new Color(colorInt);
  16. 16.        if (color.getRed() + color.getGreen() + color.getBlue() > 100) {
  17. 17.            return 1;
  18. 18.        }
  19. 19.        return 0;
  20. 20.    }
  21. 21.
  22. 22.    public static BufferedImage removeBackgroud(String picFile)
  23. 23.            throws Exception {
  24. 24.        BufferedImage img = ImageIO.read(new File(picFile));
  25. 25.        return img;
  26. 26.    }
  27. 27.
  28. 28.    public static BufferedImage removeBlank(BufferedImage img) throws Exception {
  29. 29.        int width = img.getWidth();
  30. 30.        int height = img.getHeight();
  31. 31.        int start = 0;
  32. 32.        int end = 0;
  33. 33.        Label1: for (int y = 0; y < height; ++y) {
  34. 34.            int count = 0;
  35. 35.            for (int x = 0; x < width; ++x) {
  36. 36.                if (isWhite(img.getRGB(x, y)) == 1) {
  37. 37.                    count++;
  38. 38.                }
  39. 39.                if (count >= 1) {
  40. 40.                    start = y;
  41. 41.                    break Label1;
  42. 42.                }
  43. 43.            }
  44. 44.        }
  45. 45.        Label2: for (int y = height - 1; y >= 0; --y) {
  46. 46.            int count = 0;
  47. 47.            for (int x = 0; x < width; ++x) {
  48. 48.                if (isWhite(img.getRGB(x, y)) == 1) {
  49. 49.                    count++;
  50. 50.                }
  51. 51.                if (count >= 1) {
  52. 52.                    end = y;
  53. 53.                    break Label2;
  54. 54.                }
  55. 55.            }
  56. 56.        }
  57. 57.        return img.getSubimage(0, start, width, end - start + 1);
  58. 58.    }
  59. 59.
  60. 60.    public static List<BufferedImage> splitImage(BufferedImage img)
  61. 61.            throws Exception {
  62. 62.        List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
  63. 63.        int width = img.getWidth();
  64. 64.        int height = img.getHeight();
  65. 65.        List<Integer> weightlist = new ArrayList<Integer>();
  66. 66.        for (int x = 0; x < width; ++x) {
  67. 67.            int count = 0;
  68. 68.            for (int y = 0; y < height; ++y) {
  69. 69.                if (isWhite(img.getRGB(x, y)) == 1) {
  70. 70.                    count++;
  71. 71.                }
  72. 72.            }
  73. 73.            weightlist.add(count);
  74. 74.        }
  75. 75.        for (int i = 0; i < weightlist.size();) {
  76. 76.            int length = 0;
  77. 77.            while (weightlist.get(i++) > 1) {
  78. 78.                length++;
  79. 79.            }
  80. 80.            if (length > 12) {
  81. 81.                subImgs.add(removeBlank(img.getSubimage(i - length - 1, 0,
  82. 82.                        length / 2, height)));
  83. 83.                subImgs.add(removeBlank(img.getSubimage(i - length / 2 - 1, 0,
  84. 84.                        length / 2, height)));
  85. 85.            } else if (length > 3) {
  86. 86.                subImgs.add(removeBlank(img.getSubimage(i - length - 1, 0,
  87. 87.                        length, height)));
  88. 88.            }
  89. 89.        }
  90. 90.        return subImgs;
  91. 91.    }
  92. 92.
  93. 93.    public static Map<BufferedImage, String> loadTrainData() throws Exception {
  94. 94.        if (trainMap == null) {
  95. 95.            Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
  96. 96.            File dir = new File("train2");
  97. 97.            File[] files = dir.listFiles();
  98. 98.            for (File file : files) {
  99. 99.                map.put(ImageIO.read(file), file.getName().charAt(0) + "");
  100. 100.            }
  101. 101.            trainMap = map;
  102. 102.        }
  103. 103.        return trainMap;
  104. 104.    }
  105. 105.
  106. 106.    public static String getSingleCharOcr(BufferedImage img,
  107. 107.            Map<BufferedImage, String> map) {
  108. 108.        String result = "";
  109. 109.        int width = img.getWidth();
  110. 110.        int height = img.getHeight();
  111. 111.        int min = width * height;
  112. 112.        for (BufferedImage bi : map.keySet()) {
  113. 113.            int count = 0;
  114. 114.            int widthmin = width < bi.getWidth() ? width : bi.getWidth();
  115. 115.            int heightmin = height < bi.getHeight() ? height : bi.getHeight();
  116. 116.            Label1: for (int x = 0; x < widthmin; ++x) {
  117. 117.                for (int y = 0; y < heightmin; ++y) {
  118. 118.                    if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) {
  119. 119.                        count++;
  120. 120.                        if (count >= min)
  121. 121.                            break Label1;
  122. 122.                    }
  123. 123.                }
  124. 124.            }
  125. 125.            if (count < min) {
  126. 126.                min = count;
  127. 127.                result = map.get(bi);
  128. 128.            }
  129. 129.        }
  130. 130.        return result;
  131. 131.    }
  132. 132.
  133. 133.    public static String getAllOcr(String file) throws Exception {
  134. 134.        BufferedImage img = removeBackgroud(file);
  135. 135.        List<BufferedImage> listImg = splitImage(img);
  136. 136.        Map<BufferedImage, String> map = loadTrainData();
  137. 137.        String result = "";
  138. 138.        for (BufferedImage bi : listImg) {
  139. 139.            result += getSingleCharOcr(bi, map);
  140. 140.        }
  141. 141.        ImageIO.write(img, "JPG", new File("result2//" + result + ".jpg"));
  142. 142.        return result;
  143. 143.    }
  144. 144.
  145. 145.    public static void downloadImage() {
  146. 146.        HttpClient httpClient = new HttpClient();
  147. 147.        GetMethod getMethod = null;
  148. 148.        for (int i = 0; i < 30; i++) {
  149. 149.            getMethod = new GetMethod("http://www.pkland.net/img.php?key="
  150. 150.                    + (2000 + i));
  151. 151.            try {
  152. 152.                // 执行getMethod
  153. 153.                int statusCode = httpClient.executeMethod(getMethod);
  154. 154.                if (statusCode != HttpStatus.SC_OK) {
  155. 155.                    System.err.println("Method failed: "
  156. 156.                            + getMethod.getStatusLine());
  157. 157.                }
  158. 158.                // 读取内容
  159. 159.                String picName = "img2//" + i + ".jpg";
  160. 160.                InputStream inputStream = getMethod.getResponseBodyAsStream();
  161. 161.                OutputStream outStream = new FileOutputStream(picName);
  162. 162.                IOUtils.copy(inputStream, outStream);
  163. 163.                outStream.close();
  164. 164.                System.out.println(i + "OK!");
  165. 165.            } catch (Exception e) {
  166. 166.                e.printStackTrace();
  167. 167.            } finally {
  168. 168.                // 释放连接
  169. 169.                getMethod.releaseConnection();
  170. 170.            }
  171. 171.        }
  172. 172.    }
  173. 173.
  174. 174.    public static void trainData() throws Exception {
  175. 175.        File dir = new File("temp");
  176. 176.        File[] files = dir.listFiles();
  177. 177.        for (File file : files) {
  178. 178.            BufferedImage img = removeBackgroud("temp//" + file.getName());
  179. 179.            List<BufferedImage> listImg = splitImage(img);
  180. 180.            if (listImg.size() == 4) {
  181. 181.                for (int j = 0; j < listImg.size(); ++j) {
  182. 182.                    ImageIO.write(listImg.get(j), "JPG", new File("train2//"
  183. 183.                            + file.getName().charAt(j) + "-" + (index++)
  184. 184.                            + ".jpg"));
  185. 185.                }
  186. 186.            }
  187. 187.        }
  188. 188.    }
  189. 189.
  190. 190.    /**
  191. 191.     * @param args
  192. 192.     * @throws Exception
  193. 193.     */
  194. 194.    public static void main(String[] args) throws Exception {
  195. 195.        // downloadImage();
  196. 196.        for (int i = 0; i < 30; ++i) {
  197. 197.            String text = getAllOcr("img2//" + i + ".jpg");
  198. 198.            System.out.println(i + ".jpg = " + text);
  199. 199.        }
  200. 200.    }
  201. 201.}

像BAT这种巨头的验证码通过干扰线、加粗不加粗混用、采用中文常用字(中文常用字大概有5000个,笔画繁复,形似字多,比起26个字母难度高很多)、不同的字体混用,比如楷体、宋体、幼圆混用、拼音,扭曲字体、需要准确识别13位汉字,大大增加了失败概率。

当然除了主流的图片验证码外,一些网站为了照顾视力不好的用户,采用语音验证码。一般这种验证码是机器生成一段读数字的语音。但是在这方面上很多程序员都偷懒了,预先找了10个数字的声音录音,然后生成的时候把他们随机拼到一起,结果就是这样:

设计原理如下:

整体效果

•字符数量一定范围内随机

•字体大小一定范围内随机

•波浪扭曲(角度方向一定范围内随机)

•防识别

•不要过度依赖防识别技术

•不要使用过多字符集-用户体验差

•防分割 •

重叠粘连比干扰线效果好

•备用计划

•同样强度完全不同的一套验证码

既然原理都已经知道了,那么如何破解就变得简单了。

但是问题来了,这次12306的验证码居然是图片,以上方式都不能使用,那么就不能破解了么?

有人认为12306的网站图片内存不会太大,完全可以扒下来,然后进行破解。当然这是纸上谈兵,有一种非常先进又非常原始的办法叫做“网络打码”或者“人肉打码”

一些技术大牛把验证码发送的自制的“打码”软件上,而一些“打码工”通过这个程序来输入机器自动注册,出来的验证码,传输到自动注册机器,完成验证。

目前来看这种简单粗暴的方法可以应对目前的情况。

结语:

12306这次可谓出了杀招,把所有抢票软件一刀砍死,黄牛们不开心我们就可以买到票了。既解决了黄牛问题又为广大程序员出了一道难题。

http://mobile.51cto.com/hot-468559.htm

时间: 2024-08-02 07:45:38

从12306网站新验证码看Web验证码设计与破解的相关文章

12306网站正替谁挨骂?

又到一年春运时,因铁道部规定车票可以提前60天购买,如今这个时间段,12306网站基本已经度过最难熬的时期,但关于这个简陋网站的讨论,更确切地说是吐槽和谩骂远没有停止:抢不到票的人自然有理由抱怨12306系统缓慢,用户体验垃圾得要死:抢到票的人也会心有余悸地发泄一番,毕竟,抢票的过程总是伴随着心跳加速和手指抽筋,不吐槽也很容易过不好年. 12306网站成立以来,不断改善也不断遭遇吐槽,按照正常逻辑上讲,它算得上铁道部的一大进步.事实上,没有网络购票的时候,笔者一般是不去火车站的,一方面可能真不需

12306网站做得验证码真的好烂

12306网站做得验证码真的好烂,这种产品经理.开发人员都是吃什么长大的啊!都不用脑子想问题吗?直接上图,免得说冤枉它了. 使用一点都不方便.优秀的互联网产品经理啊,救救12306吧! 12306网站做得验证码真的好烂,布布扣,bubuko.com

我给12306当五毛,我支持验证码

我给12306当五毛,我支持验证码 李铁军 12月13日 10:31----猎豹安全专家 分类 :互联网 阅读:8 抢沙发 过去的一周,几乎所有人都在吐槽12306验证码,似乎12306特别不讲情怀,12306在给要回家的人制造种种麻烦,那个要12306猜茅台的大哥也成了网红.这个时候,我来给12306当五毛,有被吐口水的可能.管他呢,吐就吐吧 为什么必须有验证码,还必须是高难度的 验证码,其目的是把机器人(外挂.插件)拦在外面,让真人能进来.简单的验证码,早就挡不住破解大军.所以,必须增加难度

Java Web验证码

1 验证码生成类RandomCode RandomCode是一个生成验证码的工具类,支持英文和数字验证码,验证码包括英文大小写和数组,其中英文i.o和数字0.1因为容易产生混淆,不包括在生成验证码中.RandomCode支持输出jpg/bmp/png/gif图片格式的验证码. /** * RandomCode验证码可以通过静态方法和实例方法生成. * * 静态方法: * * //生成长度为4的随机验证码 * String code = RandomCode.randomString(4); *

最近公司网站一直给人刷验证码,以及API接口。

最近这几天公司网站一直给人刷验证码,以及API接口.虽然可以使用iptables把它ip封了,但是那些人都是半夜来刷的,真可恶,同时有好几十个肉鸡过来刷,并且有时候有几个ip居然用iptables封不了,这几天疯狂了解相关的DDOS攻击原理信息,看能否找到有效的方法拦截.

ucenter 验证码看不到的解决办法

ucenter 验证码看不到的解决办法,很简单,很实用,本人亲试成功~http://www.jinyuanbao.cn 把images /fonts /en 的ttf 刪除可以了!

从各大高校到12306网站看SSL问题

最近闲来无事,看了许多高校的数字信息平台以及12306官网,从中发现了一个问题--高校"联合"12306正在培养学生"坏习惯".作为一名学生,我想有两个网站不得不去登录,一个是学校的数字信息平台,不登录无法查询自己的成绩等等:另一个是12306,学生往返火车票需要订购--特别是现在激烈的抢票大战,哪么,作为高校学生必须要登录的两个网站,安全性怎么样呢? 作为服务于广大群体的两个网站来说,都有其安全意识.但我要说的是,正是这种不完善的安全意识培养了广大学生群体的不良习

java web验证码生成总结(包括servlet、jsp和struts2实现)(转)

一.使用纯Servlet实现验证码 (1)在web.xml配置: [java] view plaincopy <servlet> <servlet-name>image</servlet-name> <servlet-class>org.test.web.AuthImage</servlet-class> </servlet> <servlet-mapping> <servlet-name>image</

PHP算式验证码和汉字验证码的实现方法

在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗? 大家知道简单数字或者字母验证码很容易被破解,但是算式验证码或者中文汉字验证码不容易被破解,所以建议大家在使用验证码的时候,尽量用算式验证码或者中文汉字验证码. 下面是我写的两种验证码代码,有用到的朋友可以参考下: 1.算式验证码: <?php session_start(); header("Content-type: im