Skip to content

Commit 8e5f529

Browse files
committed
【更新】检测平台访问限制
1 parent 738524d commit 8e5f529

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

dockerfile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ RUN apt-get update && apt-get install -y \
1010
&& rm -rf /var/lib/apt/lists/*
1111

1212
# 复制源代码
13-
COPY . .
13+
COPY --from=builder /build/shieldml_server /www/dk_project/dk_app/shieldml/
14+
COPY --from=builder /build/shieldml_scan.html /www/dk_project/dk_app/shieldml/
15+
COPY bt-shieldml /www/dk_project/dk_app/shieldml/
1416

1517
# 安装Go依赖
1618
RUN go mod download
@@ -45,8 +47,8 @@ COPY bt-shieldml /www/dk_project/dk_app/shieldml/
4547
RUN chmod +x /www/dk_project/dk_app/shieldml/shieldml_server && \
4648
chmod +x /www/dk_project/dk_app/shieldml/bt-shieldml && \
4749
echo '{"results":[]}' > /www/dk_project/dk_app/shieldml/data/webshellJson.json && \
48-
chmod 777 /www/dk_project/dk_app/shieldml/data/webshellJson.json && \
49-
chmod 777 /www/dk_project/dk_app/shieldml/data
50+
chmod 755 /www/dk_project/dk_app/shieldml/data/webshellJson.json && \
51+
chmod 755 /www/dk_project/dk_app/shieldml/data
5052

5153
# 暴露端口
5254
EXPOSE 6528
@@ -55,5 +57,9 @@ EXPOSE 6528
5557
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
5658
CMD wget -qO- http://localhost:6528/shieldml_scan.html || exit 1
5759

60+
# 创建非特权用户
61+
RUN groupadd -r shieldml && useradd -r -g shieldml shieldml
62+
USER shieldml
63+
5864
# 启动服务
5965
CMD ["/www/dk_project/dk_app/shieldml/shieldml_server"]

shieldml_server.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,41 @@ var scanLock sync.Mutex
4646
// 上次扫描时间
4747
var lastScanTime time.Time
4848

49+
// 添加安全相关HTTP头
50+
func securityMiddleware(next http.Handler) http.Handler {
51+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
// 防止目录列表
53+
w.Header().Set("X-Content-Type-Options", "nosniff")
54+
// 防止点击劫持
55+
w.Header().Set("X-Frame-Options", "DENY")
56+
// XSS保护
57+
w.Header().Set("X-XSS-Protection", "1; mode=block")
58+
// 内容安全策略
59+
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://cdn.jsdelivr.net; font-src 'self' https://fonts.gstatic.com https://cdn.jsdelivr.net; img-src 'self' data:;")
60+
// 不缓存敏感页面
61+
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
62+
next.ServeHTTP(w, r)
63+
})
64+
}
65+
4966
func main() {
67+
// API路由
5068
http.HandleFunc("/api/scan", scanHandler)
51-
http.Handle("/", http.FileServer(http.Dir(".")))
69+
70+
// 静态文件处理
71+
fileHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
72+
// 只允许访问shieldml_scan.html
73+
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
74+
http.Redirect(w, r, "/shieldml_scan.html", http.StatusFound)
75+
} else if r.URL.Path == "/shieldml_scan.html" {
76+
http.ServeFile(w, r, "shieldml_scan.html")
77+
} else {
78+
http.Error(w, "拒绝访问", http.StatusForbidden)
79+
}
80+
})
81+
82+
// 应用安全中间件
83+
http.Handle("/", securityMiddleware(fileHandler))
5284

5385
fmt.Println("服务已启动:http://localhost:6528/shieldml_scan.html")
5486
http.ListenAndServe(":6528", nil)

0 commit comments

Comments
 (0)