-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (145 loc) · 3.97 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
/*
22 93
05
12 F1
20
32 02
FF
*/
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"hash/crc32"
"net"
)
const (
commandGreeting = 0x01
commandProtocolError = 0x02
commandGenericResponse = 0x03
commandSecureResponse = 0x04
commandTimeout = 0x05
commandGetTime = 0x81
commandStartSecurity = 0x82
commandPing = 0x83
commandRequestFlag = 0x84
)
var commandMagic = []byte{0x44, 0x4E, 0x53, 0x4D}
func read(conn net.Conn, len uint8) []byte {
x := make([]byte, len)
conn.Read(x)
return x
}
func main() {
genericMessageCounter := 0
connectionKey := []byte{}
conn, err := net.Dial("tcp", "problems.hackdalton.com:24992")
if err != nil {
panic(err)
}
for {
magic := read(conn, 4)
if !bytes.Equal(magic, commandMagic) {
return
}
fmt.Printf("got magic!\n")
command := read(conn, 1)
_ = read(conn, 1) // sequence number
dataLength := read(conn, 1)
securityFlag := read(conn, 1) // security flag
_ = read(conn, 4) // security message key
_ = read(conn, 4) // security checksum
data := read(conn, dataLength[0])
switch command[0] {
case commandGreeting:
fmt.Printf("is greeting\n")
fmt.Printf("%s\n", string(data))
var command []byte
command = append(command, commandMagic...)
command = append(command, commandGetTime)
command = append(command, 0) // sequence number
command = append(command, 0) // data length
command = append(command, 0) // security flag
command = append(command, 0, 0, 0, 0) // security message key
command = append(command, 0, 0, 0, 0) // security checksum
conn.Write(command)
case commandProtocolError:
fmt.Printf("error: %s\n", string(data))
return
case commandGenericResponse:
fmt.Printf("generic response\n")
switch genericMessageCounter {
case 0:
var command []byte
command = append(command, commandMagic...)
command = append(command, commandStartSecurity)
command = append(command, 1) // sequence number
command = append(command, 0) // data length
command = append(command, 0) // security flag
command = append(command, 0, 0, 0, 0) // security message key
command = append(command, 0, 0, 0, 0) // security checksum
conn.Write(command)
case 1:
// security tip
case 2:
connectionKey = data
pingPayload := make([]byte, 255)
rand.Read(pingPayload)
sumBE := make([]byte, 4)
binary.BigEndian.PutUint32(sumBE, crc32.ChecksumIEEE(pingPayload[:80]))
messageKey := make([]byte, 4)
rand.Read(messageKey)
finalKey := make([]byte, 4)
for i, x := range connectionKey {
finalKey[i] = x + messageKey[i]
}
counter := 0
ciphertext := []byte{}
for _, b := range pingPayload {
ciphertext = append(ciphertext, b^finalKey[counter])
if counter < 3 {
counter++
} else {
counter = 0
}
}
var command []byte
command = append(command, commandMagic...)
command = append(command, commandPing)
command = append(command, 2) // sequence number
command = append(command, 255) // data length
command = append(command, 1) // security flag
command = append(command, messageKey...) // security message key
command = append(command, sumBE...) // security checksum
command = append(command, ciphertext...)
conn.Write(command)
}
genericMessageCounter++
case commandSecureResponse:
if securityFlag[0] != 1 {
return
}
/*finalKey := make([]byte, 4)
for i, x := range connectionKey {
finalKey[i] = x + securityMessageKey[i]
}
counter := 0
plaintext := make([]byte, len(data))
for _, b := range data {
plaintext = append(plaintext, b^finalKey[counter])
if counter < 3 {
counter++
} else {
counter = 0
}
}*/
fmt.Printf("%s\n", string(data))
fmt.Printf("%x\n", data)
return
default:
fmt.Printf("unknown command: 0x%x\n", command)
}
}
}