ddxiami

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3973|回复: 3

https劫持处理

[复制链接]
发表于 2020-9-3 16:59:41 | 显示全部楼层 |阅读模式
#nginx + lua的一键包
http://t.ddxiami.com/forum.php?m ... =502&extra=page%3D1

#问题描述:
原本以为https可以防劫持,最近碰到https被植入劫持代码的案例,https也是靠不住。参照:https://www.toutiao.com/i6795490 ... 6795490364368093700

#判断是否被植入劫持代码,直接用浏览器的view-source,也就是"查看源码";有被劫持的,一般20次以内会出现劫持代码。这种劫持基于域名,有可能是中间人攻击、dns劫持。
链接: view-source:http://my_site.com/?tag=rand_str
出现这种代码的就是被劫持了: <meta http-equiv="refresh" content="0;url=http://rob_link.com/?tag=rand_str">

#基本步骤
1、先安装nginx+lua的环境;
2、配置virtual_host;
3、配置lua脚本。

回复

使用道具 举报

 楼主| 发表于 2020-9-3 17:01:03 | 显示全部楼层
##nginx的virtual_host范例
# 设置返回数据变量: set $resp_body '';
# 关联lua文件: body_filter_by_lua_file /usr/local/nginx/conf/test.lua;
=====================================================================server {
    listen       80;

    server_name www.test.com;
    access_log  /data/logs/www.test.com/access.log main;
    error_log   /data/logs/www.test.com/error.log error;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

   
    #若有url重写规则,可在这个位置添加,结构如下
    #rewrite **** ******
    root    /data/www/www.test.com;
   
    set $resp_body '';
    location / {
        index           index.htm index.html index.php;
        body_filter_by_lua_file /usr/local/nginx/conf/test.lua;
    }


    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    #禁止执行PHP的目录。
    location ~ .*(Images|images|Img|img|Template|template|Upload|upload|Public|public)/.*\.php$ {
        deny all;
    }

    #设置图片缓存为30天,暂时注释掉
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        #expires 30d;
    }

    #设置js和css缓存为12小时,暂时注释掉
    location ~ .*\.(js|css)?$
    {
        #expires 12h;
    }

        #允许执行PHP的配置。
    location ~ \.php {
        include fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;

        #定义变量 $path_info ,用于存放pathinfo信息
        set $path_info "";
        #定义变量 $real_script_name,用于存放真实地址
        set $real_script_name $fastcgi_script_name;
        #如果地址与引号内的正则表达式匹配
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            #将文件地址赋值给变量 $real_script_name
            set $real_script_name $1;
            #将文件地址后的参数赋值给变量 $path_info
            set $path_info $2;
        }
        #配置fastcgi的一些参数
        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
        fastcgi_param SCRIPT_NAME $real_script_name;
        fastcgi_param PATH_INFO $path_info;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

}
=====================================================================
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-3 17:02:06 | 显示全部楼层

##test.lua脚本
# 先执行下面两条命令,生成lua_log.txt的日志文件
# touch /usr/local/nginx/conf/lua_log.txt
# chmod 777 /usr/local/nginx/conf/lua_log.txt
=====================================================================

local f, err = io.open("/usr/local/nginx/conf/lua_log.txt", "a+") -- 通常没必要用b二进制,也要确认是否有写权限
-- ngx.log(ngx.ERR, " lua_err:", err)   -- 调试日志

resp_body = string.sub(ngx.arg[1],1,10000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
    ngx.var.resp_body = ngx.ctx.buffered
end

str_resp_body = tostring(ngx.var.resp_body) -- 转换成string

-- 是否包含字符串
hacker_keyword = "rob_link.com"
check_str = string.find(str_resp_body, hacker_keyword)  -- 空值为nil,其它为数字

-- 输出页面内容,自动刷新当前页面,跟劫持页面重复刷新,覆盖执行
return_html=[[
<meta http-equiv="refresh" content="0">
]]

-- 判断是否包含劫持关键字
if(check_str ~= nil)
then
    -- 包字符串,需要重新跳转
    ngx.arg[1] = return_html
    -- ngx.arg[2] = true
    f:write("被劫持了:") f:write(ngx.var.time_iso8601) f:write("|") f:write(ngx.var.host) f:write("\r\n") f:flush()
end

f:close()

=====================================================================
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-3 17:02:18 | 显示全部楼层
##被篡改的页面代码
# 最后输出刷新当前页面的meta代码,两个刷新事件,最后一个覆盖掉被劫持植入的代码,造成重新刷新一次,避免被转走链接
=====================================================================
<meta http-equiv="refresh" content="0;url=http://rob_link.com/?tag=rand_str"><meta http-equiv="refresh" content="0">
=====================================================================

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|技术文档库 ( 闽ICP备15017263号-2 )|网站地图

GMT+8, 2025-5-18 23:29 , Processed in 0.035548 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表