GIT文档同步MinDoc - MinDoc模拟登陆

介绍

  • GIT文档同步Mindoc - Mindoc模拟登陆部分

实现方式

  • 跨站伪造用户的请求,模拟用户的操作

脚本语言

PHP

MinDoc 安全机制

CSRF 防御

MinDoc 采用token检测法

token就是服务端返回给客户端类似sessionid那样一长串的类值(长是为了防暴力猜解)。csrf依赖于浏览器该问链接时自动对应网站的cookie带上,token不放cookie(一般form表单加个hidden属性的input标签来存放)csrf就没法获取token,这样我们就可以通过检测发送过来的数据包中是否有正确的token值来决定是否响应请求。

图片验证码

在MinDoc,配置管理页面,可以关闭验证码
在这里插入图片描述

模拟登录实战

  • 打开登陆页面获取初始的cookie信息
//登陆页面域名
$url = "http://xxxxxxx.com/login";
//登录页面cookie存放地址在这里插入代码片
$get_cookie = "/xxx/getlogin.cookie";
//登录后cookie存放地址
$set_cookie = "/xxxxxx`在这里插入代码片`/login.cookie";
//获取登录页面cookie内容
$contents = get_content($url, $get_cookie);

//从cookie中提取 _xsrf参数, 也可以从html内容中通过正则截取
$data = extractCookies($cookie_contents);
foreach ($data as $key => $value) {
        $list = explode("|", $value['value']);
        $xsrf = base64_decode($list[0]);
}

$post = array(
    '_xsrf' => $xsrf,//验证token
    'account' => 'account',//账户
    'password' => 'psd',//密码
    'is_remember' => 'yes',保持登录
);
//模拟登陆
$state = login_post($url, $get_cookie, $post, $set_cookie);
var_dump($state)

$state = login_post($url, $get_cookie, $post, $set_cookie);
/**
 * @param $url  请求地址
 * @param $cookie cookie存放路径
 * @return mixed
 */
function get_content($url, $cookie){
    $ch = curl_init(); //初始化curl模块
    curl_setopt($ch, CURLOPT_URL, $url); //登录提交的地址
    curl_setopt($ch, CURLOPT_HEADER, 0); //是否显示头信息
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //是否自动显示返回的信息
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);//设置cookie信息保存在指定的文件夹中
    $rs = curl_exec($ch); //执行curl转去页面内容
    curl_close($ch);
    return $rs; //返回字符串
}

/**
 * @param $url 请求地址
 * @param $get_cookie 请求携带的cookie路径
 * @param $post 请求参数
 * @param $set_cookie 保存cookie的路径
 */
function login_post($url, $get_cookie, $post, $set_cookie){

    $ch = curl_init(); //初始化curl模块
    curl_setopt($ch, CURLOPT_URL, $url); //登录提交的地址
    curl_setopt($ch, CURLOPT_HEADER, 0); //是否显示头信息
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //是否自动显示返回的信息
    curl_setopt($ch, CURLOPT_COOKIEFILE, $get_cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $set_cookie);//设置cookie信息保存在指定的文件夹中
    curl_setopt($ch, CURLOPT_POST, 1); //以POST方式提交
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//要执行的信息
    curl_exec($ch); //执行CURL
//        var_dump(curl_exec($ch));
    curl_close($ch);
}

/**
 * @param $string
 * @return array
 */
function extractCookies($string) {
 
        $lines = explode(PHP_EOL, $string);
 
        foreach ($lines as $line) {
 
            $cookie = array();
 
            // detect httponly cookies and remove #HttpOnly prefix
            if (substr($line, 0, 10) == '#HttpOnly_') {
                $line = substr($line, 10);
                $cookie['httponly'] = true;
            } else {
                $cookie['httponly'] = false;
            }
 
            // we only care for valid cookie def lines
            if (strlen($line) > 0 && $line[0] != '#' && substr_count($line, "\t") == 6) {
 
                // get tokens in an array
                $tokens = explode("\t", $line);
 
                // trim the tokens
                $tokens = array_map('trim', $tokens);
 
                // Extract the data
                $cookie['domain'] = $tokens[0]; // The domain that created AND can read the variable.
                $cookie['flag'] = $tokens[1];   // A TRUE/FALSE value indicating if all machines within a given domain can access the variable.
                $cookie['path'] = $tokens[2];   // The path within the domain that the variable is valid for.
                $cookie['secure'] = $tokens[3]; // A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
 
                $cookie['expiration-epoch'] = $tokens[4];  // The UNIX time that the variable will expire on.   
                $cookie['name'] = urldecode($tokens[5]);   // The name of the variable.
                $cookie['value'] = urldecode($tokens[6]);  // The value of the variable.
                // Convert date to a readable format
                $cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
 
                // Record the cookie.
                $cookies[] = $cookie;
            }
        }
 
        return $cookies;
    }

结语

通过以上代码,就能拿到用户登录后的cookie值,请求的时候携带该cookie,就可以模拟用户请求啦

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值