400 分求助,winsocket发送修改源IP后的UDP包不成功!(300分)

  • 主题发起人 主题发起人 zhb0601
  • 开始时间 开始时间
Z

zhb0601

Unregistered / Unconfirmed
GUEST, unregistred user!
在本地机器用抓包工具能抓到包,但是在目标机器用抓包工具发现没有收到包~,也就是没发送出去,估计是在构建修改源IP后的包时错误,我在网上看到文章说代码中计算效验和部分是错误的,谁能帮帮我呢,愿意拿出我的全部家当奉赠!---------
----------我的QQ :190135565 ,同时我会送他很多QQ币做酬谢~
下面是我的代码,是抄的《delphi隐藏IP方法》

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, Registry,shellapi;

Const
Max_Message = 1024;
Max_Packet = 1024;

type
TPacketBuffer = Array[0..Max_Packet-1] of byte;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure SendIt;
end;
// IP 头
type
T_IP_Header = record
ip_verlen : Byte;
ip_tos : Byte;
ip_totallength : Word;
ip_id : Word;
ip_offset : Word;
ip_ttl : Byte;
ip_protocol : Byte;
ip_checksum : Word;
ip_srcaddr : LongWord;
ip_destaddr : LongWord;
end;
// UDP 头
Type
T_UDP_Header = record
src_portno : Word;
dst_portno : Word;
udp_length : Word;
udp_checksum : Word;
end;
// 一些 Winsock 2 的类型声明
u_char = Char;
u_short = Word;
u_int = Integer;
u_long = Longint;
SunB = packed record
s_b1, s_b2, s_b3, s_b4: u_char;
end;
SunW = packed record
s_w1, s_w2: u_short;
end;
in_addr = record
case integer of
0: (S_un_b: SunB);
1: (S_un_w: SunW);
2: (S_addr: u_long);
end;
TInAddr = in_addr;
Sockaddr_in = record
case Integer of
0: (sin_family: u_short;
sin_port: u_short;
sin_addr: TInAddr;
sin_zero: array[0..7] of Char);
1: (sa_family: u_short;
sa_data: array[0..13] of Char)
end;
TSockAddr = Sockaddr_in;
TSocket = u_int;
const
WSADESCRIPTION_LEN = 256;
WSASYS_STATUS_LEN = 128;
type
PWSAData = ^TWSAData;
WSAData = record // WSDATA
wVersion: Word;
wHighVersion: Word;
szDescription: array[0..WSADESCRIPTION_LEN] of Char;
szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
iMaxSockets: Word;
iMaxUdpDg: Word;
lpVendorInfo: PChar;
end;
TWSAData = WSAData;
//定义一些 winsock 2 函数
function closesocket(s: TSocket): Integer; stdcall;
function socket(af, Struct, protocol: Integer): TSocket; stdcall;
function sendto(s: TSocket; var Buf; len, flags: Integer; var addrto: TSockAddr;
tolen: Integer): Integer; stdcall;{}
function setsockopt(s: TSocket; level, optname: Integer; optval: PChar;
optlen: Integer): Integer; stdcall;
function inet_addr(cp: PChar): u_long; stdcall; {PInAddr;} { TInAddr }
function htons(hostshort: u_short): u_short; stdcall;
function WSAGetLastError: Integer; stdcall;
function WSAStartup(wVersionRequired: word; var WSData: TWSAData): Integer; stdcall;
function WSACleanup: Integer; stdcall;
const
AF_INET = 2; // internetwork: UDP, TCP, etc.
IP_HDRINCL = 2; // IP Header Include
SOCK_RAW = 3; // raw-protocol interface
IPPROTO_IP = 0; // dummy for IP
IPPROTO_TCP = 6; // tcp
IPPROTO_UDP = 17; // user datagram protocol
IPPROTO_RAW = 255; // raw IP packet
INVALID_SOCKET = TSocket(NOT(0));
SOCKET_ERROR = -1;
var
Form1: TForm1;
implementation

// Import Winsock 2 functions
const WinSocket = 'WS2_32.DLL';
function closesocket; external winsocket name 'closesocket';
function socket; external winsocket name 'socket';
function sendto; external winsocket name 'sendto';
function setsockopt; external winsocket name 'setsockopt';
function inet_addr; external winsocket name 'inet_addr';
function htons; external winsocket name 'htons';
function WSAGetLastError; external winsocket name 'WSAGetLastError';
function WSAStartup; external winsocket name 'WSAStartup';
function WSACleanup; external winsocket name 'WSACleanup';

{$R *.DFM}

function CheckSum(Var Buffer; Size : integer) : Word;
type
TWordArray = Array[0..1] of Word;
var
ChkSum : LongWord;
i : Integer;
begin
ChkSum := 0;
i := 0;
While Size > 1 do begin
ChkSum := ChkSum + TWordArray(Buffer);
inc(i);
Size := Size - SizeOf(Word);
end;
if Size=1 then ChkSum := ChkSum + Byte(TWordArray(Buffer));
ChkSum := (ChkSum shr 16) + (ChkSum and $FFFF);
ChkSum := ChkSum + (Chksum shr 16);
Result := Word(ChkSum);
end;

procedure BuildHeaders(FromIP : String;iFromPort : Word;ToIP : String;iToPort : Word;StrMessage : String;Var Buf : TPacketBuffer;Var remote : TSockAddr;Var iTotalSize : Word);
Var
dwFromIP : LongWord;
dwToIP : LongWord;
iIPVersion : Word;
iIPSize : Word;
ipHdr : T_IP_Header;
udpHdr : T_UDP_Header;
iUdpSize : Word;
iUdpChecksumSize : Word;
cksum : Word;
Ptr : ^Byte;
procedure IncPtr(value : Integer);
begin
ptr := pointer(integer(ptr) + value);
end;
begin
// Convert ip address'ss
dwFromIP := inet_Addr(PChar(FromIP));
dwToIP := inet_Addr(PChar(ToIP));
// 初始化 IP 头
//
iTotalSize := sizeof(ipHdr) + sizeof(udpHdr) + length(strMessage);
iIPVersion := 4;
iIPSize := sizeof(ipHdr) div sizeof(LongWord);
ipHdr.ip_verlen := (iIPVersion shl 4) or iIPSize;
ipHdr.ip_tos := 0; // IP type of service
ipHdr.ip_totallength := htons(iTotalSize); // Total packet len
ipHdr.ip_id := 0; // Unique identifier: set to 0
ipHdr.ip_offset := 0; // Fragment offset field
ipHdr.ip_ttl := 128; // Time to live
ipHdr.ip_protocol := $11; // Protocol(UDP)
ipHdr.ip_checksum := 0 ;// IP checksum
ipHdr.ip_srcaddr := dwFromIP; // Source address
ipHdr.ip_destaddr := dwToIP; // Destination address
//
// 初始化 UDP 头
//
iUdpSize := sizeof(udpHdr) + length(strMessage);
udpHdr.src_portno := htons(iFromPort);
udpHdr.dst_portno := htons(iToPort);
udpHdr.udp_length := htons(iUdpSize);
udpHdr.udp_checksum := 0;
iUdpChecksumSize := 0;
ptr := @buf[0];
FillChar(Buf, SizeOf(Buf), 0);
Move(ipHdr.ip_srcaddr, ptr^, SizeOf(ipHdr.ip_srcaddr));
IncPtr(SizeOf(ipHdr.ip_srcaddr));
iUdpChecksumSize := iUdpChecksumSize + sizeof(ipHdr.ip_srcaddr);
Move(ipHdr.ip_destaddr, ptr^, SizeOf(ipHdr.ip_destaddr));
IncPtr(SizeOf(ipHdr.ip_destaddr));
iUdpChecksumSize := iUdpChecksumSize + sizeof(ipHdr.ip_destaddr);
IncPtr(1);
Inc(iUdpChecksumSize);
Move(ipHdr.ip_protocol, ptr^, sizeof(ipHdr.ip_protocol));
IncPtr(sizeof(ipHdr.ip_protocol));
iUdpChecksumSize := iUdpChecksumSize + sizeof(ipHdr.ip_protocol);
Move(udpHdr.udp_length, ptr^, sizeof(udpHdr.udp_length));
IncPtr(sizeof(udpHdr.udp_length));
iUdpChecksumSize := iUdpChecksumSize + sizeof(udpHdr.udp_length);
move(udpHdr, ptr^, sizeof(udpHdr));
IncPtr(sizeof(udpHdr));
iUdpChecksumSize := iUdpCheckSumSize + sizeof(udpHdr);
Move(StrMessage[1], ptr^, Length(strMessage));
IncPtr(Length(StrMessage));
iUdpChecksumSize := iUdpChecksumSize + length(strMessage);
cksum := checksum(buf, iUdpChecksumSize);
udpHdr.udp_checksum := cksum;
//
// 现在 IP 和 UDP 头OK了,我们可以把它发送出去。
//
FillChar(Buf, SizeOf(Buf), 0);
Ptr := @Buf[0];
Move(ipHdr, ptr^, SizeOf(ipHdr)); IncPtr(SizeOf(ipHdr));
Move(udpHdr, ptr^, SizeOf(udpHdr)); IncPtr(SizeOf(udpHdr));
Move(StrMessage[1], ptr^, length(StrMessage));

remote.sin_family := AF_INET;
remote.sin_port := htons(iToPort);
remote.sin_addr.s_addr := dwToIP;
end;

procedure TForm1.SendIt;
Var
sh : TSocket;
bOpt : Integer;
ret : Integer;
Buf,buf2,buf3,buf4,buf5,buf6: TPacketBuffer;
Remote : TSockAddr;
Local : TSockAddr;
iTotalSize : Word;
wsdata : TWSAdata;
begin
// Startup Winsock 2
ret := WSAStartup($0002, wsdata);
try
// Create socket
sh := Socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
bOpt := 1;
ret := SetSockOpt(sh, IPPROTO_IP, IP_HDRINCL, @bOpt, SizeOf(bOpt));

// Build the packet
BuildHeaders('192.168.0.10', 1234,'192.168.1.100', 7008,'这里是消息内容',Buf, Remote, iTotalSize );
ret := SendTo(sh, buf, iTotalSize, 0, Remote, SizeOf(Remote));
// Close socket
CloseSocket(sh);
finally
// Close Winsock 2
WSACleanup;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SendIt;
end;
end.
 
有人说我这代码中“计算效验和”部分是错误的,同时他给了我正确的计算效验和代码
function CheckSum(addr: PWord; len:Integer):Word;//计算校验和
var
nleft,sum:Integer;
w:PWord;
begin
nleft:=len; //完全可以不用中间变量nleft而直接使用len
sum:=0;
w:=addr;
Result:=0; //answer=0

while nleft>1 do
begin
Inc(sum,w^); //求和
Inc(w); //指针后移
Dec(nleft,2); //长度减少( Word = 2 Byte )
end;
if nleft=1 then //长度为奇数
begin
PByte(@Result)^:=PByte(w)^;
Inc(sum,Result); //这两句可以合并为 Inc(sum,PByte(w)^);
end;

sum:=sum shr 16+sum and $FFFF;
Inc(sum,sum shr 16);
Result:=not sum;
end;

但怎么加到我的代码里去呢,他的是
function CheckSum(addr: PWord; len:Integer):Word;//
而我原来的是
function CheckSum(Var Buffer; Size : integer) : Word;
( ) 中的不一样,不知道怎么修改,帮帮我呀~~
 
看了一下,不明白那里错了。
估计校验错误的可能性大一点。
我做的校验都是头校验,不把包体包含进去的。
老兄的好像把包体也包含进去校验了。

function cksum(addr: PWord; len: integer): Cardinal;
var
nleft: integer;
w: PWord;
answer: cardinal;
sum: integer;
begin
nleft := len;
w := addr;
sum := 0;

while (nleft > 1) do
begin
sum := sum + w^;
//Integer(w) := Integer(w) + SizeOf(Word);
inc(w);
nleft := nleft - 2;
end;

if (nleft = 1) then
begin
sum := sum + Word(w^);
end;
sum := sum shr 16 + sum and $FFFF;
sum := sum + sum shr 16;
answer := not sum;
result := answer;
end;
这是校验函数,使用方法是
例如ip头ipheard;
ip.chsum:=word(@ipheard,sizeof(ipheard));
在icmp协议中使用通过的。
lz看看
//================
有人发了,hoho,看来大家见解一致。
哦,这个人还是楼主。晕
 
楼上的老师,你的QQ多少啊,我想具体向你请教一下,我在修改过程中有些问题不明白~,谢谢你啊~,我的QQ是 190135565 ,到时候给你冲很多QQB
 
晕,你这样说我都晕了,
我做这个时间也不长,我们可以讨论一下。
qq没有,公司用msn办公。
可以用邮件联系:gohoo115@sohu.com
 
隐藏IP地址发送啊!成功可以当黑客了
 
楼上的言重了,我要做的修改源IP发送UDP包,是正常需要,无恶意
现在UDP能发出去,也能够做到自定义源IP,但是计算效验合是错的,导致对方收不到,郁闷,谁帮帮我,真心感谢你
 
接受答案了.
 
后退
顶部