1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| import std.stdio;
import std.string;
import std.bitmanip;
import std.socket;
import std.c.windows.winsock;
struct tcphdr
{
ushort port_source;
ushort port_dest;
uint seq;
uint ack_seq;
version(LittleEndian)
{
mixin(bitfields!(uint, "res", 6, uint, "off", 4, uint, "fin", 1, uint, "syn", 1, uint, "rst", 1, uint, "psh", 1, uint, "ack", 1, uint, "urg", 1));
}
else version(BigEndian)
{
mixin(bitfields!(uint, "off", 4, uint, "res", 6, uint, "urg", 1, uint, "ack", 1, uint, "psh", 1, uint, "rst", 1, uint, "syn", 1, uint, "fin", 1));
}
ushort window;
ushort checksum;
ushort urg_ptr;
}
struct iphdr
{
mixin(bitfields!(uint, "ihl", 4, uint, "vers", 4));
ubyte tos;
ushort totLength;
ushort id;
mixin(bitfields!(uint, "flags", 3, uint, "posFrag", 13));
ubyte protocole;
ubyte ttl;
ushort checksum;
uint ip_source;
uint ip_dest;
}
ushort check_sum(ushort *buf, uint words) // Repris d'un site
{
uint sum = 0;
for(sum = 0; words > 0; --words)
{
sum += *buf++;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return cast(ushort)(~sum);
}
void main()
{
char[4096] paquet;
iphdr *ip = cast(iphdr*) paquet;
tcphdr *tcp = cast(tcphdr*)(cast(ubyte*)ip + cast(uint)(5 * uint.sizeof));
Socket socket = new Socket(AddressFamily.INET, SocketType.RAW, ProtocolType.RAW);
assert(socket !is null);
ip.ihl = 5;
ip.vers = 4;
ip.tos = 0;
ip.totLength = cast(ushort)(tcphdr.sizeof + iphdr.sizeof);
ip.id = htons(5431);
ip.flags = 0;
ip.posFrag = 5;
ip.protocole = cast(ubyte)6; // TCP
ip.ttl = cast(ubyte)255; // max ttl
ip.ip_source = inet_addr(ipsrc);
ip.ip_dest = inet_addr(ipdest);
ip.checksum = check_sum(cast(ushort*) paquet, ip.totLength >> 1);
tcp.port_source = htons(5887);
tcp.port_dest = htons(80);
tcp.seq = 0;
tcp.ack_seq = 0;
tcp.res = 0;
tcp.off = 0;
tcp.fin = 0;
tcp.syn = 1;
tcp.rst = 0;
tcp.psh = 0;
tcp.ack = 0;
tcp.urg = 0;
tcp.window = htons(5000);
tcp.urg_ptr = 0;
InternetAddress addr = new InternetAddress(ipdest, 80);
for(int i = 0; i < 5; ++i)
{
if(socket.sendTo(paquet, addr) == Socket.ERROR)
writefln(lastSocketError);
}
} |
Partager