program Project1;
{$APPTYPE CONSOLE}
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Dialogs, WinSock;
type IPHDR =
record
verlen: integer;
tos: integer;
totallength: short;
id: short;
offset: short;
ttl: integer;
protocol: integer;
checksum: short;
srcaddr: integer;
dstaddr: integer;
end;
type TCPHDR =
record
srcport: short;
dstport: short;
seqno: integer;
ackno: integer;
offset: integer;
flags: integer;
window: short;
checksum: short;
urgptr: short;
end;
type FORCSUM =
record
srcaddr: integer;
dstaddr: integer;
padzero: integer;
protocol: integer;
tcplength: short;
end;
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)[i];
inc(i);
Size := Size - SizeOf(Word);
end;
if Size=1
then
ChkSum := ChkSum + Byte(TWordArray(Buffer)[i]);
ChkSum := (ChkSum
shr 16) + (ChkSum
and $FFFF);
ChkSum := ChkSum + (Chksum
shr 16);
Result := Word(ChkSum);
end;
function RawSocket(destIP, srcIP: pchar; fd, destPort, srcPort, tcpFlag: word; content:
string): integer;
var dest: sockaddr_in;
forc: FORCSUM;
tcp: TCPHDR;
ip: IPHDR;
iErr: integer;
buf:
array[0..60]
of char;
begin
dest.sin_family := AF_INET;
dest.sin_port := htons(destPort);
dest.sin_addr.S_addr := inet_addr(destIP);
ip.verlen := $45;
ip.tos := $0;
ip.totallength := 24;
ip.id := 1;
ip.offset := 0;
ip.ttl := $80;
ip.protocol := IPPROTO_TCP;
ip.checksum := 0;
ip.dstaddr := inet_addr(destIP);
ip.srcaddr := inet_addr(srcIP);
tcp.dstport := htons(destPort);
tcp.ackno := 0;
tcp.offset := 80;
tcp.flags := tcpFlag;
tcp.window := htons($4000);
tcp.urgptr := 0;
tcp.checksum := 0;
forc.dstaddr :=
ip.dstaddr;
forc.padzero := 0;
forc.protocol := IPPROTO_TCP;
forc.tcplength := htons(sizeof(
tcp));
tcp.srcport := htons(srcPort);
randomize;
tcp.seqno := htonl(random(16));
forc.srcaddr := inet_addr(srcIP);
copymemory(@buf, @forc, sizeof(forc));
copymemory(buf + sizeof(
ip), @
tcp, sizeof(
tcp));
tcp.checksum := checksum(buf, sizeof(forc)+sizeof(
tcp));
copymemory(@buf, @
ip, sizeof(
ip));
copymemory(buf+sizeof(
ip), @
tcp, sizeof(
tcp));
// memset(buf+sizeof(ip)+sizeof(tcp), 0,sizeof(buf)-sizeof(ip)-sizeof(tcp));
copymemory(buf + sizeof(
ip) + sizeof(
tcp), @content, sizeof(content));
ip.checksum := checksum(buf, sizeof(
ip) + sizeof(
tcp));
copymemory(@buf, @
ip, sizeof(
ip));
iErr := sendto(fd, buf, sizeof(
ip) + sizeof(
tcp), 0, dest, sizeof(dest));
end;
function SockAttemp: integer;
var SockVersion: word;
data: WSADATA;
iErr, opt: integer;
fd: integer;
begin
SockVersion := MAKEWORD(2,2);
WSAStartup(SockVersion, data);
fd := socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
setsockopt(fd, IPPROTO_IP, 2, @opt, sizeof(opt));
Result := fd;
end;
var sock: integer;
begin
{ TODO -oUser -cConsole Main : Hier Code einfügen }
sock := SockAttemp;
RawSocket(pchar('
127.0.0.1'), pchar('
127.0.0.1'), sock, 888, 888, 0, '
hallo');
readln;
end.