HTTP接続を9時間維持する方法
2023年10月23日
17 分で読めますあまりにも
ただし、
SnykのCapture the Flagイベント運営方法
Snykでは
その
登録は登録用ランディングページからのみ
受け付ける。 CTFdサーバーに
アカウントを 自動作成する。 一意の
エイリアスを 生成する。 一意で
複雑な パスワードを 設定する。 まだ
ユーザーに 通知しない。
イベントが
近づいたら、 事前登録した すべての ユーザーに、 認証情報の 取得方法を 一括 メールで 通知する プロセスを 実行する。 CTFdの
登録モードを 切り替え、 新規ユーザーには 登録時に 認証情報を メールで 送信する。
この
CTFd APIを活用する
CTFdには、
ほとんどの429(ctfd-account-hookアプリケーションの
CTFdアカウントフックアプリの開発
マーケティングチームが
セキュアな
エンドポイント アカウントフックへの
入力は 最小限に する (メールアドレスのみ) 登録時に
メール通知を 送信しない モードと、 送信する モードを 切り替えられるように する 理想と
しては、 モード切り替え時に 登録用ランディングページの 設定を 一切 変更しなくて 済む こと。
Spring Boot、
アカウントの作成
ctfd-account-hookアプリへの
内部辞書から
ユーザーの/api/v1/usersをnotifyが
POST /api/v1/users?notify=trueクエリ文字列パラメーターnotifyを
Spring BootをnotifyOverrideと
@Value("#{ @environment['ctfd.api.notify-override'] ?: false }")
private Boolean notifyOverride;デフォルトではnotifyOverrideはfalseにctfd.api.notify-overrideをtrueに
…
String notify = (notifyOverride || req.getNotify()) ? "?notify=true" : "";
String uri = API_URI + "/users" + notify;アプリはHerokuに
heroku config:set ctfd.api.notify-override=true一括メールの送信
メール通知の
イベントの
この
this.webClient.post().uri(uri)
.bodyValue(emailText)
.retrieve()
…
.bodyToMono(CtfdUserResponse.class)
.retryWhen(retryBackoffSpec)
.block();この.retryWhen(retryBackoffSpec)にretryBackoffSpecの
this.retryBackoffSpec = Retry.backoff(maxAttempts, Duration.ofSeconds(backoffSeconds))
.doBeforeRetry(retrySignal -> log.debug(
"Waiting {} seconds. Retry #{} of {} after exception: {}",
backoffSeconds, (retrySignal.totalRetriesInARow()+1), maxAttempts,
retrySignal.failure().getLocalizedMessage()
))
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> retrySignal.failure());最初のmaxAttemptsとbackoffSecondsを429エラーでしょう。5xxエラーが
バックオフ/リトライ方式が
次の
Spring Bootには
@PostMapping("/api/v1/update-and-email/{affiliation}")
public SseEmitter updateAndEmailUsers(@PathVariable String affiliation) {
SseEmitter emitter = new SseEmitter(1000*60*60*24L);
ctfdApiService.updateAndEmail(emitter, affiliation);
return emitter;
}メソッドのSseEmitterオブジェクトをctfdApiService.updateAndEmailをupdateAndEmailメソッドは
サービスコードを
@SpringBootApplication
@EnableAsync
public class CtfdAccountHookApplication {
public static void main(String[] args) {
SpringApplication.run(CtfdAccountHookApplication.class, args);
}
}次に、@AsyncアノテーションをCtfdApiServiceImplクラスにupdateAndEmailメソッドの
@Async
@Override
public void updateAndEmail(SseEmitter emitter, String affiliation) {
Integer page = 1;
int processed = 0;
do {
try {
CtfdUserPaginatedResponse ctfdUserResponse =
getUsersByAffiliation(affiliation, page);
for (CtfdUser ctfdUser : ctfdUserResponse.getData()) {
SseEmitter.SseEventBuilder event = SseEmitter.event()
.data("Processing - " + ctfdUser.getId() + " - " + LocalTime.now().toString())
.id(String.valueOf(ctfdUser.getId()))
.name(ctfdUser.getId() + " - " + ctfdUser.getName());
emitter.send(event);
…
ctfdUser = updatePassword(ctfdUser);
emailUser(ctfdUser);
}
page = ctfdUserResponse.getMeta().getPagination().getNext();
processed += ctfdUserResponse.getData().length;
…
} catch (Exception e) {
log.error("Failure while update/email operation: {}", e.getMessage());
emitter.completeWithError(e);
return;
}
} while (page != null);
…
emitter.complete();
}Spring Bootは、
http POST \
https://<ctfd account hook url>/api/v1/update-and-email/fetch2023 \
x-api-key:"<api token>"
HTTP/1.1 200
data:Processing - 1 - 17:19:00.595414016
id:1
event:1 - raw-blue-armant
data:Finished Processing - 1 - 17:19:03.958247179
id:1
event:1 - raw-blue-armantサーバーログを
Processing user id: 1, name: raw-blue-armant
Password updated for user id: 1
Email sent for user id: 1
…
Processing user id: 10, name: conscious-harlequin-cursinu
Password updated for user id: 10
Waiting 10 seconds. Retry #1 of 10 after exception: 429 Too Many Requests from POST https://snyk.ctf.games/api/v1/users/10/email
Waiting 10 seconds. Retry #2 of 10 after exception: 429 Too Many Requests from POST https://snyk.ctf.games/api/v1/users/10/email
Waiting 10 seconds. Retry #3 of 10 after exception: 429 Too Many Requests from POST https://snyk.ctf.games/api/v1/users/10/email
Email sent for user id: 10ここでは、
思わぬ障害
ローカル環境ですべてを
本番環境では
Herokuには
更新後の
@PostMapping("/api/v1/update-and-email/{affiliation}")
public SseEmitter updateAndEmailUsers(@PathVariable String affiliation) {
// TODO - should probs be another env var setting
SseEmitter emitter = new SseEmitter(1000*60*60*24L);
ctfdApiService.emitterHeartBeat(emitter);
ctfdApiService.updateAndEmail(emitter, affiliation);
return emitter;
}emitterHeartBeatとupdateAndEmailはemitterHeartBeatメソッドは
@Async
@Override
public void emitterHeartBeat(SseEmitter emitter) {
try {
do {
emitter.send("beat");
Thread.sleep(5000);
} while (true);
} catch (Exception e) {
log.debug("exception during emitter: {}", e.getMessage());
}
}これにbeatメッセージが
さあ、Fetch the Flagへ
ctfd-account-hookプロジェクトを
このjob idを
202



