环境搭建

安装go环境

sudo tar -C /usr/local -xzf go1.19.5.linux-amd64.tar.gz

vim /etc/profile

export PATH=$PATH:/usr/local/go/bin

source /etc/profile

安装postgrepsql

sudo -i -u postgres

psql

CREATE USER gogs WITH PASSWORD 'gogs';

CREATE DATABASE gogs;

\c gogs

GRANT ALL PRIVILEGES ON DATABASE gogs TO gogs;

GRANT USAGE ON SCHEMA public TO gogs;

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO gogs;

远程主机安装并配置 Delve

1. 安装 Delve(远程主机上)

go install github.com/go-delve/delve/cmd/dlv@latest

Delve 安装后会位于 $HOME/go/bin/dlv,建议加到 PATH 中:

export PATH=$PATH:$HOME/go/bin

2. 构建 Gogs 可调试版本

为了支持调试,需要构建不启用优化和内联的 Go 程序:

go build -gcflags="all=-N -l" -o gogs

3. 启动 Delve 调试服务

在远程服务器上以服务模式(监听端口)启动 Gogs:

dlv exec ./gogs web --headless --listen=:2345 --api-version=2 --accept-multiclient

解释:

  • --headless:无 UI 模式
  • --listen=:2345:在端口 2345 上监听远程调试连接
  • --api-version=2:Delve 的 VS Code 插件使用 API v2
  • --accept-multiclient:允许多个调试客户端连接(可选)
  • --log:输出调试日志

VSCODE 调试环境搭建

安装dlv

go install github.com/go-delve/delve/cmd/dlv@latest

配置launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Gogs Web Server",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "args": [
                "web"
            ],
            "env": {},
            "cwd": "${workspaceFolder}",
            "showLog": true,
            "trace": "verbose",
            "buildFlags": "",
            "dlvFlags": [
                "--check-go-version=false"
            ]
        },
        {
            "name": "Attach to Gogs Process (Local)",
            "type": "go",
            "request": "attach",
            "mode": "local",
            "processId": 0,
            "showLog": true,
            "trace": "verbose"
        },
        {
            "name": "Attach to Remote Gogs (Delve)",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "${workspaceFolder}",
            "port": 2345,
            "host": "127.0.0.1",
            "showLog": true,
            "trace": "verbose",
            "substitutePath": [
                {
                    "from": "${workspaceFolder}",
                    "to": "${workspaceFolder}"
                }
            ]
        },
        {
            "name": "Attach to Remote Gogs (Custom Host)",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/root/Desktop/gogs",
            "port": 2345,
            "host": "192.168.92.131",
            "showLog": true,
            "trace": "verbose",
            "substitutePath": [
                {
                    "from": "d:\\code\\go\\gogs-0.13.3",
                    "to": "/root/Desktop/gogs"
                }
            ]
        }
    ]
}

现在可以从本地连接远程调试端口 2345。

CVE-2024-56731

在 Gogs 中,pre-receive hook 是一个 Git 钩子,通常用于在推送(push)操作前进行检查或执行特定的操作。该钩子是一个脚本,它会在 Git 服务器接收到推送时执行。触发这个 pre-receive 钩子通常是通过向 Gogs 服务器的某个仓库推送代码时自动发生的。

触发条件

  1. Git 推送操作:
  • pre-receive 钩子会在 Git 客户端(如通过 git push 命令)推送代码到远程仓库时触发。
  • 该钩子会检查推送的内容,并在推送成功前运行。
  1. 钩子文件位置:
  • pre-receive 钩子通常存放在 Git 仓库的 .git/hooks/ 目录下。您提供的路径 /root//gogs-repositories/test/10fe6c.git/hooks/pre-receive 表明钩子脚本存在于 10fe6c 仓库的 hooks 目录下。
  1. 配置钩子触发:
  • 该钩子会在您使用 Git 推送代码到该仓库时自动触发。若该钩子脚本的内容中包含特定操作(例如执行命令、检查推送内容等),它将在推送时执行。

手动复现,创建仓库

使用git clone仓库

创建符号链接指向hooks/pre-receive脚本

编辑文件进行提交,添加test目录以及playload

可以看见已经成功写入了

成功触发

CVE-2025-8110

进入UpdateRepoFile函数

首先先创建,可以看见调用os.MkdirAll创建符号链接

调用os.WriteFile,写入文件到filepath

成功写入

如何触发pre-receive

在 Git 仓库中,pre-receive hook 是在推送到远程仓库时自动触发的。要触发它,你需要执行 git push 操作。

编写脚本验证

成功执行命令