-
Notifications
You must be signed in to change notification settings - Fork 214
/
SpikeEngine.kt
238 lines (209 loc) · 9.79 KB
/
SpikeEngine.kt
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
package burp
import burp.H2Connection.Companion.buildReq
import burp.network.stack.http2.frame.Frame
import burp.network.stack.http2.frame.FrameFlags
import net.hackxor.api.ConnectionFactory
import net.hackxor.api.Header
import net.hackxor.api.Header.header
import net.hackxor.api.RequestFrameFactory
import net.hackxor.api.SocketFactory
import net.hackxor.utils.DefaultThreadLauncher
import net.hackxor.utils.FrameComparator
import net.hackxor.utils.TrustAllSocketFactory
import java.net.URL
import java.util.concurrent.CountDownLatch
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread
class SpikeEngine(url: String, threads: Int, maxQueueSize: Int, val requestsPerConnection: Int, override val maxRetriesPerRequest: Int, override var idleTimeout: Long = 0, override val callback: (Request, Boolean) -> Boolean, override var readCallback: ((String) -> Boolean)?, val warmLocalConnection: Boolean = true): RequestEngine() {
var threadLauncher: DefaultThreadLauncher
var socketFactory: SocketFactory
init {
requestQueue = if (maxQueueSize > 0) {
LinkedBlockingQueue(maxQueueSize)
}
else {
LinkedBlockingQueue()
}
idleTimeout *= 1000
threadLauncher = DefaultThreadLauncher()
socketFactory = TrustAllSocketFactory()
target = URL(url)
val retryQueue = LinkedBlockingQueue<Request>()
completedLatch = CountDownLatch(threads)
for(j in 1..threads) {
thread {
sendRequests(retryQueue)
}
}
}
private fun sendRequests(retryQueue: LinkedBlockingQueue<Request>) {
var responseStreamHandler: SpikeConnection? = null
while (!Utils.unloaded && !shouldAbandonAttack()) {
val socket = socketFactory.create(target.host, 443)
socket.soTimeout = 10000
socket.tcpNoDelay = false
responseStreamHandler = SpikeConnection(this)
val connectionID = connections.incrementAndGet()
val connectionFactory = ConnectionFactory.create(threadLauncher, responseStreamHandler)
val connection = connectionFactory.createConnection(socket) { } // callback is invoked when connection is killed
val frameFactory = RequestFrameFactory.createSmallFinalDataFrameRequestFrameFactory(connection.negotiatedMaximumFrameSize())
var requestsSent = 0
try {
while (requestsSent < requestsPerConnection && !shouldAbandonAttack()) {
if (responseStreamHandler.inflight.size >= 1){
// todo make this configurable
Thread.sleep(10)
continue
}
var req = retryQueue.poll()
if (req == null) {
req = requestQueue.poll(100, TimeUnit.MILLISECONDS)
}
if (req == null) {
if (attackState.get() == 2) {
waitForPendingRequests(responseStreamHandler)
return
}
continue
}
if (req.gate == null) {
val frames = reqToFrames(req, frameFactory)
responseStreamHandler.inflight[frames[0].G] = req
req.connectionID = connectionID
req.time = System.nanoTime()
connection.sendFrames(frames)
requestsSent += 1
continue
}
val gatedReqs = ArrayList<Request>(10)
req.gate!!.reportReadyWithoutWaiting()
req.connectionID = connectionID
gatedReqs.add(req)
while ((!req.gate!!.fullyQueued.get() || responseStreamHandler.inflight.size != 0) && !shouldAbandonAttack()) {
Thread.sleep(10)
}
while (!req.gate!!.isOpen.get() && !shouldAbandonAttack()) {
//Utils.out("Waiting on ${req.gate!!.remaining.get()} signals for gate to open on ${req.gate!!.name}")
val nextReq = requestQueue.poll(50, TimeUnit.MILLISECONDS) ?: throw RuntimeException("Gate deadlock")
if (nextReq.gate!!.name != req.gate!!.name) {
throw RuntimeException("Over-read while waiting for gate to open")
}
nextReq.connectionID = connectionID
gatedReqs.add(nextReq)
if (nextReq.gate!!.reportReadyWithoutWaiting()) {
break
}
}
val prepFrames = ArrayList<Frame>(gatedReqs.size)
val finalFrames = ArrayList<Pair<Frame, Long>>(gatedReqs.size)
for (gatedReq in gatedReqs) {
val reqFrames = reqToFrames(gatedReq, frameFactory)
for (frame in reqFrames) {
if (frame.isFlagSet(0x01)) {
finalFrames.add(Pair(frame, gatedReq.delayCompletion))
}
else {
prepFrames.add(frame)
}
}
responseStreamHandler.inflight[reqFrames[0].G] = gatedReq
requestsSent += 1
}
socket.tcpNoDelay = false // original
connection.sendFrames(prepFrames)
Thread.sleep(100) // headstart size
for (gatedReq in gatedReqs) {
gatedReq.time = System.nanoTime()
}
if (warmLocalConnection) {
val warmer = burp.network.stack.http2.frame.PingFrame("12345678".toByteArray())
// val warmer = burp.network.stack.http2.frame.DataFrame(finalFrames[0].Q, FrameFlags(0), "".toByteArray())
// using an empty data frame upsets some servers
connection.sendFrames(warmer) // just send it straight away
//finalFrames.add(0, warmer)
}
for (pair in finalFrames) {
//Utils.out("Sending final frame")
if (pair.second != 0L) {
//Utils.out("Sleeping for "+pair.second)
// fixme response arrives before this frame is sent!
Thread.sleep(pair.second)
}
//Utils.out("Finished sleeping")
connection.sendFrames(pair.first)
}
//connection.sendFrames(finalFrames)
}
} catch (ex: Exception) {
if (!responseStreamHandler.inflight.isEmpty()) {
for (req in responseStreamHandler.inflight.values) {
if (shouldRetry(req)) {
retryQueue.add(req)
}
}
}
ex.printStackTrace()
Utils.out(ex.message)
continue
}
}
waitForPendingRequests(responseStreamHandler)
}
private fun waitForPendingRequests(responseStreamHandler: SpikeConnection?) {
for (x in 1..100) {
if (responseStreamHandler != null && !responseStreamHandler.inflight.isEmpty()) {
Thread.sleep(100)
} else {
break
}
}
completedLatch.countDown()
}
fun reqToFrames(req: Request, factory: RequestFrameFactory): List<Frame> {
val headerList = buildReq(HTTP2Request(req.getRequest()), false)
val properHeaders = ArrayList<Header>(headerList.size)
for (pair in headerList) {
properHeaders.add(header(pair.first, pair.second))
}
return factory.framesFor(properHeaders, Utils.getBodyBytes(req.getRequestAsBytes()))
}
fun stop() {
// latch blah
// connection.stop()
threadLauncher.destroy()
}
override fun start(timeout: Int) {
// todo wait for connection?
attackState.set(1)
start = System.nanoTime()
}
override fun buildRequest(template: String, payloads: List<String?>, learnBoring: Int?, label: String?): Request {
return Request(template, payloads, learnBoring ?: 0, label)
}
fun handleResponse(streamID: Int, resp: String, req: Request) {
successfulRequests.getAndIncrement()
req.response = resp
val interesting = processResponse(req, req.getResponseAsBytes()!!)
invokeCallback(req, interesting)
}
// @Throws(IOException::class, InterruptedException::class)
// fun sendSynced(vararg requests: List<Header?>?) {
// val frames: MutableList<Frame> = ArrayList()
// for (headers in requests) {
// frames.addAll(frameFactory.framesFor(headers))
// }
// frames.sortWith(FrameComparator())
// assert(frames.size == requests.size * 2)
// val sublist: List<Frame> = frames.subList(0, requests.size)
// val endlist: List<Frame> = frames.subList(requests.size, frames.size)
// socket.tcpNoDelay = false
// connection.sendFrames(sublist)
// Thread.sleep(500)
// socket.tcpNoDelay = true
// connection.sendFrames(endlist)
// //Thread.sleep(500);
// //socket.setTcpNoDelay(false);
// //responseCollectingStreamFrameProcessor.responses().forEach(System.out::println);
// }
}