Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static packetproxy.http2.frames.FrameUtils.PREFACE;
import static packetproxy.http2.frames.FrameUtils.SETTINGS;
import static packetproxy.http2.frames.FrameUtils.WINDOW_UPDATE;
import static packetproxy.util.Logging.err;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -67,36 +66,14 @@ public SinglePacketAttackController(final OneShotPacket oneshot, final int sleep
}

public void attack(final int count) throws Exception {
if (count <= 0) {
return;
}

sendConnectionPreface();
launchAttack(count);
}

private void sendConnectionPreface() throws Exception {
final var preface = new ByteArrayOutputStream();
preface.write(PREFACE);
preface.write(SETTINGS);
preface.write(WINDOW_UPDATE);

attackConnection.execFastSend(preface.toByteArray());
}

private void launchAttack(final int count) throws Exception {
var currentStreamId = 1;

for (var i = 0; i < count; i++) {
try {
final var request = new SingleAttackRequest(currentStreamId, baseAttackFrames, attackConnection,
sleepTimeMs);

request.execute();
} catch (Exception e) {
err("Stream %d : Single Packet Attack failed with exception: %s", currentStreamId, e.getMessage());
}

currentStreamId += 2;
}
}

private static boolean isHttp2(final OneShotPacket oneshot) {
var alpn = oneshot.getAlpn();
return alpn != null && (alpn.equals("h2") || alpn.equals("grpc") || alpn.equals("grpc-exp"));
Expand Down Expand Up @@ -124,6 +101,53 @@ private static boolean isGetMethod(final OneShotPacket oneshot) {
return method.equals("GET");
}

private void sendConnectionPreface() throws Exception {
final var preface = new ByteArrayOutputStream();
preface.write(PREFACE);
preface.write(SETTINGS);
preface.write(WINDOW_UPDATE);

attackConnection.execFastSend(preface.toByteArray());
}

private void launchAttack(final int count) throws Exception {
final var requests = createRequests(count);

for (final var request : requests) {
request.sendFirstFrames();
}

Thread.sleep(sleepTimeMs);
sendPing();

for (final var request : requests) {
request.sendLastFrames();
}

for (var i = 0; i < requests.size(); i++) {
attackConnection.receive();
}
}

private ArrayList<SingleAttackRequest> createRequests(final int count) throws Exception {
final var requests = new ArrayList<SingleAttackRequest>();

for (var i = 0; i < count; i++) {
final var streamId = i * 2 + 1;
final var request = new SingleAttackRequest(streamId, baseAttackFrames, attackConnection);
requests.add(request);
}

return requests;
}

private void sendPing() throws Exception {
final var pingPayload = new byte[8];
final var pingFrame = new Frame(Frame.Type.PING, 0, 0, pingPayload);
final var pingData = pingFrame.toByteArray();
attackConnection.execFastSend(pingData);
}

private static AttackFrames generateAttackFrames(final OneShotPacket packet) throws Exception {
final var originalFrames = convertPacketToFrames(packet);

Expand Down Expand Up @@ -317,23 +341,13 @@ private static class SingleAttackRequest {
private final AttackFrames originalAttackFrames;
private final DuplexSync connection;
private final AttackFrames streamAttackFrames;
private final int sleepTimeMs;

public SingleAttackRequest(final int streamId, final AttackFrames originalAttackFrames,
final DuplexSync connection, final int sleepTimeMs) throws Exception {
final DuplexSync connection) throws Exception {
this.streamId = streamId;
this.originalAttackFrames = originalAttackFrames;
this.connection = connection;
this.streamAttackFrames = createStreamAttackFrames();
this.sleepTimeMs = sleepTimeMs;
}

public void execute() throws Exception {
sendFirstFrames();
Thread.sleep(sleepTimeMs);
sendPing();
sendLastFrames();
connection.receive();
}

private AttackFrames createStreamAttackFrames() throws Exception {
Expand All @@ -357,15 +371,8 @@ private List<Frame> updateFrameStreamIds(final List<Frame> originalFrames, final

private void sendFirstFrames() throws Exception {
final var firstFramesData = FrameUtils.toByteArray(streamAttackFrames.firstFrames);
connection.execFastSend(firstFramesData);
}

private void sendPing() throws Exception {
final var pingPayload = new byte[8];
final var pingFrame = new Frame(Frame.Type.PING, 0, 0, pingPayload);
final var pingData = pingFrame.toByteArray();

connection.execFastSend(pingData);
connection.execFastSend(firstFramesData);
}

private void sendLastFrames() throws Exception {
Expand Down