今天一个小网站被黑了,本来就是一个临时做了一两天的小网站,因为不需要登陆,所以只是在后台加入了一个ip过滤,没想那么复杂。但是持续被黑,经过将近一个上午的修改,终于暂时避免被黑。

1、首先昨天被黑,想到的是ip过滤机制出现了问题,查看数据库,果然发现ip不对劲,它是一个真实ip加上了一个随机生成的数字,我看可能是他模拟了ip,把这个真实ip过滤掉了。相安无事。

2、今天上午又被黑了,发现他可以模拟ip,没有规律可查,所以想到了使用验证码,首先是生成一个随机数存放在cookie中,1分钟后又被黑掉。

3、把随机验证码存放在session中,2分钟后被黑掉。

4、把随机验证生成图片,然后数字存放在session中。2分钟后又被黑掉了。

5、查看后台日志,发现程序本身是存在bug的,因为黑网站的人并不是通过浏览器访问,而是通过程序实现的,所以程序修改了下。暂时没有被黑了。

下面是我实现的,漏洞肯定还是有的。只是我不知道罢了。

一个验证码生成器:

public class Scaptcha {
    // 图片的宽度。
    private int width = 120;
    // 图片的高度。
    private int height = 40;
    // 验证码字符个数
    private int codeCount = 4;
    // 验证码干扰线数
    private int lineCount = 50;
    // 验证码
    private String code = null;
    // 验证码图片Buffer
    private BufferedImage buffImg = null;

private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };

// 生成随机数
    private Random random = new Random();

public Scaptcha() {
        this.createCode();
    }

/**
     *
     * @param width
     *            图片宽
     * @param height
     *            图片高
     */
    public Scaptcha(int width, int height) {
        this.width = width;
        this.height = height;
        this.createCode();
    }

/**
     *
     * @param width
     *            图片宽
     * @param height
     *            图片高
     * @param codeCount
     *            字符个数
     * @param lineCount
     *            干扰线条数
     */
    public Scaptcha(int width, int height, int codeCount, int lineCount) {
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        this.createCode();
    }

public void createCode() {
        int codeX = 0;
        int fontHeight = 0;
        fontHeight = height - 5;// 字体的高度
        codeX = width / (codeCount+3);// 每个字符的宽度

// 图像buffer
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImg.createGraphics();

// 将图像填充为白色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);

// 创建字体
        ImgFontByte imgFont = new ImgFontByte();
        Font font = imgFont.getFont(fontHeight);
        g.setFont(font);

// 绘制干扰线
        for (int i = 0; i < lineCount; i++) {
            int xs = getRandomNumber(width);
            int ys = getRandomNumber(height);
            int xe = xs + getRandomNumber(width / 8);
            int ye = ys + getRandomNumber(height / 8);
            g.setColor(getRandomColor());
            g.drawLine(xs, ys, xe, ye);
        }

StringBuffer randomCode = new StringBuffer();
        // 随机产生验证码字符
        for (int i = 0; i < codeCount; i++) {
            String strRand = String.valueOf(codeSequence[random
                    .nextInt(codeSequence.length)]);
            // 设置字体颜色
            g.setColor(getRandomColor());
            // 设置字体位置
            g.drawString(strRand, (i + 1) * codeX,
                    getRandomNumber(height / 2) + 25);
            randomCode.append(strRand);
        }
        code = randomCode.toString();
    }

/** 获取随机颜色 */
    private Color getRandomColor() {
        int r = getRandomNumber(255);
        int g = getRandomNumber(255);
        int b = getRandomNumber(255);
        return new Color(r, g, b);
    }

/** 获取随机数 */
    private int getRandomNumber(int number) {
        return random.nextInt(number);
    }

public void write(String path) throws IOException {
        OutputStream sos = new FileOutputStream(path);
        this.write(sos);
    }

public void write(OutputStream sos) throws IOException {
        ImageIO.write(buffImg, "png", sos);
        sos.close();
    }

public BufferedImage getBuffImg() {
        return buffImg;
    }

public String getCode() {
        return code;
    }

/** 字体样式类 */
    class ImgFontByte {
        public Font getFont(int fontHeight) {
            try {
                Font baseFont = Font.createFont(Font.TRUETYPE_FONT,
                        new ByteArrayInputStream(hex2byte(getFontByteStr())));
                return baseFont.deriveFont(Font.PLAIN, fontHeight);
            } catch (Exception e) {
                return new Font("Arial", Font.PLAIN, fontHeight);
            }
        }

private byte[] hex2byte(String str) {
            if (str == null)
                return null;
            str = str.trim();
            int len = str.length();
            if (len == 0 || len % 2 == 1)
                return null;

byte[] b = new byte[len / 2];
            try {
                for (int i = 0; i < str.length(); i += 2) {
                    b[i / 2] = (byte) Integer.decode(
                            "0x" + str.substring(i, i + 2)).intValue();
                }
                return b;
            } catch (Exception e) {
                return null;
            }
        }

// 字体文件的十六进制字符串
        private String getFontByteStr() {

return "字段很长,就不放进来了";
        }
    }
}

然后是生成验证码的servlet

public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        ArticleDao articleDao = new ArticleDao();
        // 组装图片的路径
        Scaptcha instance = new Scaptcha();
        /*Cookie cookie = new Cookie("scaptcha", instance.getCode());
        cookie.setMaxAge(1800);
        response.addCookie(cookie);
        String code=instance.getCode();*/
        String code=instance.getCode();
        request.getSession().setAttribute("scaptcha", code);
        System.out.println(code);
        String temp=request.getRealPath("/upload");
        instance.write(temp+"//ss.jpg");
        final String bitmapUrl = "upload/upload/";
        List<Article> articles= articleDao.findArticleAll();
        for(int i = 0 ; i<articles.size() ; i++){
            String bitmap = bitmapUrl+articles.get(i).getPhotoUrl();
            articles.get(i).setPhotoUrl(bitmap);
//            System.out.println("图片路径                                           "+bitmap);
            
        }
        request.setAttribute("articles", articles);
        request.getRequestDispatcher("/jsp/many_vote.jsp").forward(request, response);
    }

最后是验证的servlet

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        ArticleDao articleDao = new ArticleDao();
        // 多个投票的id
        String[] many_vote = request.getParameterValues("article_vote");
        String scaptcha=request.getParameter("scaptcha").toUpperCase();
        String scaptchaC="";
        String code=(String) request.getSession().getAttribute("scaptcha");
        if(code==null){
            scaptchaC="";
        }else{
            scaptchaC=code;
        }
        // 获取ip
        // String ip = request.getRemoteAddr();
        // String ip = InetAddress.getLocalHost().getHostAddress();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if(scaptcha.equals("")||scaptcha==null){
            System.out.println("scaptcha是空的");
            
            out.print("<script type='text/javascript'>alert('请输入验证码...!');location.href='"
                    + request.getContextPath() + "/index.jsp';</script>");
        }else{
            if(!scaptcha.equals(scaptchaC)){
                out.print("<script type='text/javascript'>alert('该验证码不匹配...!');location.href='"
                        + request.getContextPath() + "/index.jsp';</script>");
            }
            if (many_vote != null && many_vote.length == 20) {
                
                //判断ip是否为空
                if(ip!=null && !ip.equals("") && !ip.contains(",") && interapt(ip)){
                    // 判断ip是否重复
                    if (articleDao.isIpRepetition(ip)) {
                        out.print("<script type='text/javascript'>alert('此ip已经参加投票...!');location.href='"
                                + request.getContextPath() + "/index.jsp';</script>");
                    } else {
                        // 循环插入
                        for (int i = 0; i < many_vote.length; i++) {
                            articleDao.voteAdd(ip, many_vote[i]);
                        }
                        out.print("<script type='text/javascript'>alert('投票成功...!');location.href='"
                                + request.getContextPath() + "/index.jsp';</script>");
                    }
                }else{
                    out.print("<script type='text/javascript'>alert('系统繁忙,请稍后投票...!');location.href='"
                            + request.getContextPath() + "/index.jsp';</script>");
                }
                
                
            } else {
                // out.print("<script type='text/javascript'>alert('请检查投票数是否达到20人...!');location.href='"
                // + request.getContextPath() + "/index.jsp';</script>");

out.print("<script type='text/javascript'>alert('请检查投票数是否达到20人...!');window.history.back(-1);</script>");
            }
        }
        System.out.println(scaptchaC+"####"+scaptcha);

}

实现图片验证码,其实就是简单的验证码实现,记录一下相关推荐

  1. python图像验证码识别_python 简单图像识别--验证码

    python  简单图像识别--验证码 记录下,准备工作安装过程很是麻烦. 首先库:pytesseract,image,tesseract,PIL windows安装PIL,直接exe进行安装更方便( ...

  2. 浙大python读者验证码_Python实现简单生成验证码功能【基于random模块】

    本文实例讲述了Python实现简单生成验证码功能.分享给大家供大家参考,具体如下: 验证码一般用来验证登陆.交易等行为,减少对端为机器操作的概率,python中可以使用random模块,char()内 ...

  3. java 验证码_java实现简单的验证码功能

    最近要做一个网站,要求实现验证码程序,经过不断调试,终于成功实现功能. 一.验证码生成类 生成验证码的话需要用到java的Graphics类库,画出一个验证码 废话不多说,直接上代码 package ...

  4. python爬虫验证码识别 (手把手教会你验证码识别)opencv图像处理 图片处理 验证码处理 降噪 简单易懂验证码处理

    前言:验证码是个烦人的小家伙!当然有很多打码平台,可以轻松解决!但可以自己来,干嘛靠别人!有技术不学是傻儿童!今天主要讲opencv来解决验证码!抱着怀里教你!让你向前迈一大步!详细讲,慢慢看!简单易 ...

  5. java创建一个图片_Java 创建一个简单的验证码图片

    代码如下: package lixin.gan.test; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2 ...

  6. 验证码实现php 难点,php实现简单的验证码功能

    php实现简单的验证码功能<?php //简单的验证码 //随机数 //为什么要循环0-15之间的数呢? //因为要实现最简单的字母和数字混搭 //十六进制0-9 a-f //dechex -- ...

  7. php 简单图片验证码,PHP 实现简单图片验证码

    验证码是网站会员系统中不可缺少的,目前验证码有很多种,但用的比较多的还是图片验证码,这里就用面向对象的方式来简单实现图片验证码, 注意!我这里使用的是 PHP 的 gd 库,如果要查看是否启用了 gd ...

  8. 使用Tensorflow构建和训练自己的CNN来做简单的验证码识别

    Tensorflow是目前最流行的深度学习框架,我们可以用它来搭建自己的卷积神经网络并训练自己的分类器,本文介绍怎样使用Tensorflow构建自己的CNN,怎样训练用于简单的验证码识别的分类器.本文 ...

  9. 一个简单的验证码识别教程

    一.起因 前几天准备做一个自动计算gpa的网站,学校的教务登录时候需要输入验证码.本来想把验证码图片显示出来让用户手动输入,但是搞了半天没搞定...所以决定自己写一个识别的程序. 直接说结果吧,最终写 ...

最新文章

  1. 欧盟「人脑计划」​最新进展:新算法模拟生物进化,为大脑如何工作提供新见解...
  2. 阿里巴巴开源项目: 基于mysql数据库binlog的增量订阅消费
  3. python --那些你应该知道的知识点
  4. [搬运] iOS 7 侧滑返回手势使用和错误集
  5. jqgrid的实用方法集合
  6. 我们需要什么样的开源教育?
  7. 如何用Vue实现简易的富文本编辑器,并支持Markdown语法
  8. linux下如何做ghost,又简单又方便,很实用的方法!!!
  9. HDOJ 1286 HDU 1286 找新朋友 ACM 1286 IN HDU
  10. java 中计算时间差
  11. 11.大数据架构详解:从数据获取到深度学习 --- 大数据云化
  12. 中保车服灾备云,为保险公司“上保险”
  13. python积最大的分解_pyfactor
  14. 小超市的大梦想,京东的梦醒时分
  15. 人型自走输入法(网页日语输入法)
  16. ETL数据同步工具Kettle简介
  17. 【基于深度学习的脑电图识别】应用篇:DEEP LEARNING APPROACHES FOR AUTOMATIC ANALYSIS OF EEGS
  18. tomcat警告:consider increasing the maximum size of the cache. After eviction approximately [9,267] KB
  19. 微服务--应对每秒上万并发下的参数优化实战(实战经验)
  20. php函数有什么用,有用的的PHP函数

热门文章

  1. Nginx+Tomcat+memcached负载均衡实现seccion存储
  2. linux——不同系统间的文件传输和打包压缩
  3. java中在做除法操作时,对有余数的结果进行取整
  4. ehcache springboot_Spring Boot应用缓存实践之:Ehcache加持
  5. linux man手册_读书笔记:Linux命令行与shell脚本编程大全 第一章~第五章
  6. 神经网络测试时间计算机,卷积神经网络的时代到此结束了?
  7. java用jsoup爬网页数据_java使用jsoup爬取网页数据
  8. weex css单位,Weex系列(7) ——踩坑填坑的总总
  9. c malloc 头文件_C 数据类型
  10. java mysql 文本导入数据语句_Java利用MYSQL LOAD DATA LOCAL INFILE实现大批量导入数据到MySQL...