-
Notifications
You must be signed in to change notification settings - Fork 43
/
rtsp.go
182 lines (161 loc) · 4.64 KB
/
rtsp.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
package bruteutils
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/netx"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/lowhttp"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
)
type RTSPAuthMethod uint8
const (
RTSPAuthMethod_Origin RTSPAuthMethod = 0
RTSPAuthMethod_Basic = 1
RTSPAuthMethod_Digest = 2
)
func genDESCRIBLE(url string, seq int) string {
msgRet := "DESCRIBE " + url + " RTSP/1.0\r\n"
msgRet += "CSeq: " + strconv.Itoa(seq) + "\r\n"
msgRet += "Accept: application/sdp\r\n"
msgRet += "\r\n"
return msgRet
}
func genDESCRIBLEWithAuth(url string, seq int, username, password string, authMethod RTSPAuthMethod, authHeader string) string {
switch authMethod {
case RTSPAuthMethod_Digest, RTSPAuthMethod_Origin:
msgRet := "DESCRIBE " + url + " RTSP/1.0\r\n"
msgRet += "CSeq: " + strconv.Itoa(seq) + "\r\n"
msgRet += "Authorization: " + authHeader + "\r\n"
msgRet += "Accept: application/sdp\r\n"
msgRet += "\r\n"
return msgRet
case RTSPAuthMethod_Basic:
auth := fmt.Sprintf("%s:%s", username, password)
auth = codec.EncodeBase64(auth)
msgRet := "DESCRIBE " + url + " RTSP/1.0\r\n"
msgRet += "CSeq: " + strconv.Itoa(seq) + "\r\n"
msgRet += "Authorization: Basic " + auth + "\r\n"
msgRet += "Accept: application/sdp\r\n"
msgRet += "\r\n"
return msgRet
default:
msgRet := "DESCRIBE " + url + " RTSP/1.0\r\n"
msgRet += "CSeq: " + strconv.Itoa(seq) + "\r\n"
msgRet += "Accept: application/sdp\r\n"
msgRet += "\r\n"
return msgRet
}
}
var rtspAuth = &DefaultServiceAuthInfo{
ServiceName: "ssh",
DefaultPorts: "554,1554",
DefaultUsernames: []string{
"admin", "root",
},
DefaultPasswords: []string{
"12345", "admin", "1234", "123456", "123", "1111",
},
UnAuthVerify: func(i *BruteItem) *BruteItemResult {
u := i.Target
if !strings.Contains(i.Target, "://") {
target := fixToTarget(i.Target, 554)
i.Target = target
u = "rtsp://" + target
} else {
parsed, err := url.Parse(u)
if err != nil {
log.Errorf("rtsp: %v parse failed: %s", i.Target, err)
return i.Result()
}
parsed.Host = fixToTarget(parsed.Host, 554)
i.Target = parsed.Host
u = parsed.String()
}
res := i.Result()
conn, err := netx.DialTCPTimeout(10*time.Second, i.Target)
if err != nil {
log.Errorf("rtsp: %v conn failed: %s", i.Target, err)
res.Finished = true
return res
}
defer conn.Close()
_, err = conn.Write([]byte(genDESCRIBLE(u, 2)))
if err != nil {
log.Errorf("rtsp: %v write failed: %s", i.Target, err)
res.Finished = true
return res
}
raw, err := utils.ReadConnWithTimeout(conn, 2*time.Second)
if err != nil {
log.Errorf("rtsp: %v read failed: %s", i.Target, err)
res.Finished = true
return res
}
sc := lowhttp.GetStatusCodeFromResponse(raw)
if sc == 200 {
res.Ok = true
return res
}
return res
},
BrutePass: func(i *BruteItem) *BruteItemResult {
res := i.Result()
u := i.Target
target := fixToTarget(i.Target, 554)
if !strings.Contains(i.Target, "://") {
u = "rtsp://" + target
}
_ = u
conn, err := netx.DialTCPTimeout(10*time.Second, target)
if err != nil {
log.Errorf("rtsp: %v conn failed: %s", target, err)
res.Finished = true
return res
}
defer conn.Close()
_, err = conn.Write([]byte(genDESCRIBLE(u, 2)))
if err != nil {
log.Errorf("rtsp: %v write failed: %s", i.Target, err)
res.Finished = true
return res
}
raw, err := utils.ReadConnWithTimeout(conn, 2*time.Second)
if err != nil {
log.Errorf("rtsp: %v read failed: %s", i.Target, err)
res.Finished = true
return res
}
sc := lowhttp.GetStatusCodeFromResponse(raw)
authMethod := RTSPAuthMethod_Origin
authHeader := lowhttp.GetHTTPPacketHeader(raw, "WWW-Authenticate")
authResponseHeader := ""
if sc == 401 {
if strings.HasPrefix(authHeader, "Digest") {
authMethod = RTSPAuthMethod_Digest
_, ah, err := lowhttp.GetDigestAuthorizationFromRequestEx("DESCRIBE", u, "", authHeader, i.Username, i.Password, true)
if err == nil {
authResponseHeader = ah.String()
}
} else if strings.HasPrefix(authHeader, "Basic") {
authMethod = RTSPAuthMethod_Basic
}
}
_, err = conn.Write([]byte(genDESCRIBLEWithAuth(u, 2, i.Username, i.Password, authMethod, authResponseHeader)))
if err != nil {
log.Errorf("rtsp: %v write failed: %s", i.Target, err)
res.Finished = true
return res
}
raw, _ = utils.ReadConnWithTimeout(conn, 2*time.Second)
sc = lowhttp.GetStatusCodeFromResponse(raw)
if sc == 200 {
res.Ok = true
res.Finished = true
}
return res
},
}