大虾张无忌快进来看看.用自定义IP头发UDP包的咋就死活不成功呢,解决不好,年都过不好. ( 积分: 200 )

  • 主题发起人 主题发起人 serenaiad
  • 开始时间 开始时间
S

serenaiad

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock2;

type
IP_HDR=record
VerLen:Byte;
TOS:Byte;
TotalLen:WORD;
Ident:WORD;
Frag_Flags:WORD;
TTL:Byte;
PROTO:Byte;
ChkSum:WORD;
Source:DWORD;
Dest:DWORD;
end;
UDP_HDR=Record
SourcePort:Word;
DestPort:word;
Len:Word;
ChkSum:word;
End;
PSDHDR=Record
Source:DWORD;
Dest:DWORD;
ZeroByte:byte;
Prot:Byte;
Len:Word;
End;



TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
EditIP: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FSocket:TSocket;
Procedure IPHdrCheckSum(var Hdr:IP_HDR;UDPHDR:UDP_HDR);
Procedure UdpHdrCheckSum(var Hdr:Udp_HDR;var PsdHdr:PsdHdr);
Function RandomIP:string;
implementation




Procedure IPHdrCheckSum(var Hdr:IP_HDR;UDPHDR:UDP_HDR);
var Section,chkSum:Word;
CheckSum:longword;
Buf:array[0..29]of byte;
i:integer;
Begin
CheckSum:=0;
Hdr.ChkSum:=0;
CopyMemory(@Buf[0],@Hdr,20);
CopyMemory(@Buf[20],@UDPHDR,8);
Buf[28]:=79;
Buf[29]:=75;

for i:=0 to 14 do
begin
CopyMemory(@Section,@Buf[i*2],2);
CheckSum:=CheckSum+Section;

end;



CheckSum:=(CheckSum shr 16)+ (CheckSum and $FFFF);
CheckSum:=CheckSum+(CheckSum shr 16);
//CheckSum:= CheckSum xor $FFFF;
ChkSum:=CheckSum and $FFFF;
ChkSum:=not ChkSum ;

hdr.ChkSum:=ChkSum;

End;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var wsadata:TWsaData;
Flag:longbool;
iFlag:integer;
timeout:integer;
begin
WSaStartUp(2*256+2,WsaData);
FSocket:=WSASocket(AF_INET,SOCK_RAW,IPPROTO_UDP ,0,0,WSA_FLAG_OVERLAPPED);
if FSocket=Invalid_Socket then
Begin
ShowMessage('Create Socket Error');


End;


Flag:=true; // 注意,这个Flag是LongBool,4字节的, Boolean是1字节的
//showmessage(inttostr(flag and $1));
iFlag:=1;
if setsockopt(FSocket,IPPROTO_IP,IP_HDRINCL,pchar(@Flag),sizeof(Flag))<>0 THen
Begin
CloseSocket(FSocket);
ShowMessage('setsockopt IP_HDRINCL 失败! Error Code:'+inttostr(WSaGetLastError));

End;

TimeOut:=1000;
if SetSockOpt(FSocket,SOL_SOCKET,SO_SNDTIMEO,pchar(@TimeOut),sizeof(TimeOut))<>0 then
Begin
CloseSocket(FSocket);
ShowMessage('SetSendTimeOut 失败! Error Code:'+inttostr(WSaGetLastError));
End;

end;

procedure TForm1.Button2Click(Sender: TObject);
var IPHDR:IP_HDR;
UDPHDR:UDP_HDR;
Psd:PSdHdr;
addr:DWord;
B1,B2,B3,B4:Byte;
sock:TSocket;
sockAddr:TSockAddr;
FBuf:array[0..29] of byte;
Begin
Randomize;
IPHDR.VerLen:=4;
IPHDR.VerLen:=(IPHDR.VerLen shl 4) + 5;
IPHDR.TOS:=0;
IPHDR.TotalLen:=htons(sizeof(IPHDR)+sizeof(UDPHDR)+2);
IPHDR.Ident:=htons(trunc(random* 65535)+1);
IPHDR.Frag_Flags:=0;//htons($0040);
IPHDR.TTL:=128;
IPHDR.PROTO:=IPPROTO_UDP; // IPPROTO_TCP

IPHDR.Dest:=Inet_Addr(pchar(Editip.text));

IPHDR.Source:=Inet_Addr(pchar(randomip));

psd.Source:=iphdr.Source;
psd.Dest:=iphdr.Dest;
psd.ZeroByte:=0;
psd.Prot:=IPPROTO_UDP;
Psd.Len:=htons(10);

UDPHDR.SourcePort:=htons(100);
udphdr.DestPort:=htons(80);
udphdr.Len:=htons(10);
udphdr.ChkSum:=0;

//Showmessage(inttostr(sizeof(IPHDR)));

UdpHdrCheckSum(UDPHDR,psd);
IPhdrCheckSum(IPHDR,UdpHDR);
//SplitIpAddr(Editip.text,B1,B2,B3,B4);
sockAddr.sin_family:=af_inet;
sockAddr.sin_port:=htons(80);
sockAddr.sin_addr.S_addr:=Inet_Addr(pchar(Editip.text));
ZeroMemory(@FBuf,30);
CopyMemory(@FBuf[0],@IPHDR,20);
CopyMemory(@FBuf[20],@UDPHDR,8);
FBuf[28]:=79;
FBuf[29]:=75;

if (sendto(FSocket,FBuf[0],30,0,@SockAddr,Sizeof(SockAddr))=SOCKET_ERROR) then
showmessage('Send Error:'+inttostr(getlasterror));




end;
Procedure UdpHdrCheckSum(var Hdr:Udp_HDR;var PsdHdr:PsdHdr);
var Section,chkSum:Word;
CheckSum:longword;
Buf:array[0..21]of byte;
i:integer;
Begin
CheckSum:=0;
Hdr.ChkSum:=0;
PsdHdr.Len:=hdr.Len;
Copymemory(@Buf[0],@PsdHdr,12);
CopyMemory(@Buf[12],@Hdr,8);
Buf[20]:=79;
Buf[21]:=75;

for i:=0 to 3 do
begin
CopyMemory(@Section,@Buf[i*2],2);
CheckSum:=CheckSum+Section;

end;


CheckSum:=(CheckSum shr 16)+ (CheckSum and $FFFF);
CheckSum:=CheckSum +(CheckSum shr 16);
//CheckSum:= CheckSum xor $FFFF;
ChkSum:=CheckSum and $FFFF;
ChkSum:=not ChkSum ;
hdr.ChkSum:=ChkSum;

End;
Function RandomIP:string;
var ip:string;
Begin
ip:=inttostr(trunc(Random * 255));
ip:=ip+'.'+inttostr(trunc(Random * 255));
ip:=ip+'.'+inttostr(trunc(Random * 255));
ip:=ip+'.'+inttostr(trunc(Random * 255));
Result:=ip;
End;

procedure TForm1.Button3Click(Sender: TObject);
var sck:TSOcket;
addr:SockAddr;
strSend:string;
buf:array[0..8] of byte;
begin
strSend:='**** You!';
Sck:=socket(af_inet,SOCK_DGRAM,IPPROTO_UDP);
ZeroMemory(@Buf[0],9);

//move( strSend,buf[0],9);
CopyMemory(@Buf[0],pchar(strSend),9);

addr.sin_family:=af_inet;
addr.sin_port:=htons(80);
addr.sin_addr.S_addr:=inet_addr(pchar(editip.text));
sendto(sck,Buf[0],9,0,@addr,sizeof(addr));


end;
end.

我以下一些疑问:
IP头校验和:
我看C的SYN Flood 程序,校验IP头的时候,把TCP头也传进去一起校验,是否要把UDP头传进去一起校验呢?
UDP头校验和:
UDP头校验的时候要一个伪头,UPD伪头里的那个长度是UDP头的长度还是整个UDP长度呢?
IP头里的片偏移问题:
我这个UPD传的是"OK",长度肯定不用分片的,所以把片偏移变为0,可以俺看的Syn Flood的c代码为0X40,这个应该置为多少呢?SYN Flood 就一个IP头和TCP头,应该也不用分片的,为什么设为0X40呢?
如果你把这个自己调一下可以运行了,希望你能把代码贴上来
我这个里面的Winsock2这个单元是
http://www.jointdev.com/Download.asp?id=23
下载的.
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock2;

type
IP_HDR=record
VerLen:Byte;
TOS:Byte;
TotalLen:WORD;
Ident:WORD;
Frag_Flags:WORD;
TTL:Byte;
PROTO:Byte;
ChkSum:WORD;
Source:DWORD;
Dest:DWORD;
end;
UDP_HDR=Record
SourcePort:Word;
DestPort:word;
Len:Word;
ChkSum:word;
End;
PSDHDR=Record
Source:DWORD;
Dest:DWORD;
ZeroByte:byte;
Prot:Byte;
Len:Word;
End;



TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
EditIP: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FSocket:TSocket;
Procedure IPHdrCheckSum(var Hdr:IP_HDR;UDPHDR:UDP_HDR);
Procedure UdpHdrCheckSum(var Hdr:Udp_HDR;var PsdHdr:PsdHdr);
Function RandomIP:string;
implementation




Procedure IPHdrCheckSum(var Hdr:IP_HDR;UDPHDR:UDP_HDR);
var Section,chkSum:Word;
CheckSum:longword;
Buf:array[0..29]of byte;
i:integer;
Begin
CheckSum:=0;
Hdr.ChkSum:=0;
CopyMemory(@Buf[0],@Hdr,20);
CopyMemory(@Buf[20],@UDPHDR,8);
Buf[28]:=79;
Buf[29]:=75;

for i:=0 to 14 do
begin
CopyMemory(@Section,@Buf[i*2],2);
CheckSum:=CheckSum+Section;

end;



CheckSum:=(CheckSum shr 16)+ (CheckSum and $FFFF);
CheckSum:=CheckSum+(CheckSum shr 16);
//CheckSum:= CheckSum xor $FFFF;
ChkSum:=CheckSum and $FFFF;
ChkSum:=not ChkSum ;

hdr.ChkSum:=ChkSum;

End;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var wsadata:TWsaData;
Flag:longbool;
iFlag:integer;
timeout:integer;
begin
WSaStartUp(2*256+2,WsaData);
FSocket:=WSASocket(AF_INET,SOCK_RAW,IPPROTO_UDP ,0,0,WSA_FLAG_OVERLAPPED);
if FSocket=Invalid_Socket then
Begin
ShowMessage('Create Socket Error');


End;


Flag:=true; // 注意,这个Flag是LongBool,4字节的, Boolean是1字节的
//showmessage(inttostr(flag and $1));
iFlag:=1;
if setsockopt(FSocket,IPPROTO_IP,IP_HDRINCL,pchar(@Flag),sizeof(Flag))<>0 THen
Begin
CloseSocket(FSocket);
ShowMessage('setsockopt IP_HDRINCL 失败! Error Code:'+inttostr(WSaGetLastError));

End;

TimeOut:=1000;
if SetSockOpt(FSocket,SOL_SOCKET,SO_SNDTIMEO,pchar(@TimeOut),sizeof(TimeOut))<>0 then
Begin
CloseSocket(FSocket);
ShowMessage('SetSendTimeOut 失败! Error Code:'+inttostr(WSaGetLastError));
End;

end;

procedure TForm1.Button2Click(Sender: TObject);
var IPHDR:IP_HDR;
UDPHDR:UDP_HDR;
Psd:PSdHdr;
addr:DWord;
B1,B2,B3,B4:Byte;
sock:TSocket;
sockAddr:TSockAddr;
FBuf:array[0..29] of byte;
Begin
Randomize;
IPHDR.VerLen:=4;
IPHDR.VerLen:=(IPHDR.VerLen shl 4) + 5;
IPHDR.TOS:=0;
IPHDR.TotalLen:=htons(sizeof(IPHDR)+sizeof(UDPHDR)+2);
IPHDR.Ident:=htons(trunc(random* 65535)+1);
IPHDR.Frag_Flags:=0;//htons($0040);
IPHDR.TTL:=128;
IPHDR.PROTO:=IPPROTO_UDP; // IPPROTO_TCP

IPHDR.Dest:=Inet_Addr(pchar(Editip.text));

IPHDR.Source:=Inet_Addr(pchar(randomip));

psd.Source:=iphdr.Source;
psd.Dest:=iphdr.Dest;
psd.ZeroByte:=0;
psd.Prot:=IPPROTO_UDP;
Psd.Len:=htons(10);

UDPHDR.SourcePort:=htons(100);
udphdr.DestPort:=htons(80);
udphdr.Len:=htons(10);
udphdr.ChkSum:=0;

//Showmessage(inttostr(sizeof(IPHDR)));

UdpHdrCheckSum(UDPHDR,psd);
IPhdrCheckSum(IPHDR,UdpHDR);
//SplitIpAddr(Editip.text,B1,B2,B3,B4);
sockAddr.sin_family:=af_inet;
sockAddr.sin_port:=htons(80);
sockAddr.sin_addr.S_addr:=Inet_Addr(pchar(Editip.text));
ZeroMemory(@FBuf,30);
CopyMemory(@FBuf[0],@IPHDR,20);
CopyMemory(@FBuf[20],@UDPHDR,8);
FBuf[28]:=79;
FBuf[29]:=75;

if (sendto(FSocket,FBuf[0],30,0,@SockAddr,Sizeof(SockAddr))=SOCKET_ERROR) then
showmessage('Send Error:'+inttostr(getlasterror));




end;
Procedure UdpHdrCheckSum(var Hdr:Udp_HDR;var PsdHdr:PsdHdr);
var Section,chkSum:Word;
CheckSum:longword;
Buf:array[0..21]of byte;
i:integer;
Begin
CheckSum:=0;
Hdr.ChkSum:=0;
PsdHdr.Len:=hdr.Len;
Copymemory(@Buf[0],@PsdHdr,12);
CopyMemory(@Buf[12],@Hdr,8);
Buf[20]:=79;
Buf[21]:=75;

for i:=0 to 3 do
begin
CopyMemory(@Section,@Buf[i*2],2);
CheckSum:=CheckSum+Section;

end;


CheckSum:=(CheckSum shr 16)+ (CheckSum and $FFFF);
CheckSum:=CheckSum +(CheckSum shr 16);
//CheckSum:= CheckSum xor $FFFF;
ChkSum:=CheckSum and $FFFF;
ChkSum:=not ChkSum ;
hdr.ChkSum:=ChkSum;

End;
Function RandomIP:string;
var ip:string;
Begin
ip:=inttostr(trunc(Random * 255));
ip:=ip+'.'+inttostr(trunc(Random * 255));
ip:=ip+'.'+inttostr(trunc(Random * 255));
ip:=ip+'.'+inttostr(trunc(Random * 255));
Result:=ip;
End;

procedure TForm1.Button3Click(Sender: TObject);
var sck:TSOcket;
addr:SockAddr;
strSend:string;
buf:array[0..8] of byte;
begin
strSend:='**** You!';
Sck:=socket(af_inet,SOCK_DGRAM,IPPROTO_UDP);
ZeroMemory(@Buf[0],9);

//move( strSend,buf[0],9);
CopyMemory(@Buf[0],pchar(strSend),9);

addr.sin_family:=af_inet;
addr.sin_port:=htons(80);
addr.sin_addr.S_addr:=inet_addr(pchar(editip.text));
sendto(sck,Buf[0],9,0,@addr,sizeof(addr));


end;
end.

我以下一些疑问:
IP头校验和:
我看C的SYN Flood 程序,校验IP头的时候,把TCP头也传进去一起校验,是否要把UDP头传进去一起校验呢?
UDP头校验和:
UDP头校验的时候要一个伪头,UPD伪头里的那个长度是UDP头的长度还是整个UDP长度呢?
IP头里的片偏移问题:
我这个UPD传的是"OK",长度肯定不用分片的,所以把片偏移变为0,可以俺看的Syn Flood的c代码为0X40,这个应该置为多少呢?SYN Flood 就一个IP头和TCP头,应该也不用分片的,为什么设为0X40呢?
如果你把这个自己调一下可以运行了,希望你能把代码贴上来
我这个里面的Winsock2这个单元是
http://www.jointdev.com/Download.asp?id=23
下载的.
 
新年到了,做做好事,帮你顶![:)]
 
IP_HDR=packed record //后面的结构体也要这么做
春节休息,没仔细看,不号意思啊
大家过年多休息,来年各位发大财的时候不要忘给小弟点好处哦[:D]
 
后退
顶部