Class: WASP::Wasp

Inherits:
Object show all
Defined in:
lib/wasp/wasp.rb

Constant Summary collapse

LINE_LENGTH =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Wasp

Returns a new instance of Wasp.



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
# File 'lib/wasp/wasp.rb', line 17

def initialize(args)
  @num_wasps = if args[:server].nil? then WASP::Const::DEFAULT_WASPS
              else
                args[:server].to_i
              end
              
  @weapon_name = if args[:weapon].nil? then WASP::Const::DEFAULT_WASPS
            else
              args[:weapon]
            end
  
  @header = args[:header]
            
  @report  = if args[:report].nil? then false
             else 
               true
             end
             
  @keepalive = if args[:keepalive].nil? then false
               else
                 true
               end
  @without_cookie = if args[:wo_cookie].nil? then false
                    else
                      true
                    end
  @compact = if args[:compact].nil? then false
       else
         true
       end
  
  @ec2 = WASP::Aws.new(args)
  @params = []
  
end

Instance Attribute Details

#ec2Object (readonly)

Returns the value of attribute ec2.



9
10
11
# File 'lib/wasp/wasp.rb', line 9

def ec2
  @ec2
end

#num_waspsObject (readonly)

Returns the value of attribute num_wasps.



10
11
12
# File 'lib/wasp/wasp.rb', line 10

def num_wasps
  @num_wasps
end

#reportObject (readonly)

Returns the value of attribute report.



13
14
15
# File 'lib/wasp/wasp.rb', line 13

def report
  @report
end

#weaponObject (readonly)

Returns the value of attribute weapon.



12
13
14
# File 'lib/wasp/wasp.rb', line 12

def weapon
  @weapon
end

#weapon_nameObject (readonly)

Returns the value of attribute weapon_name.



11
12
13
# File 'lib/wasp/wasp.rb', line 11

def weapon_name
  @weapon_name
end

Instance Method Details

#__wave_report(stingless_wasps) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wasp/wasp.rb', line 123

def __wave_report(stingless_wasps)
  puts "Wasps report:"
  stingless_wasps.each do |wasp|
    next if wasp[:wavereport].count == 0
    
    puts "Wasp " + "#{wasp[:instance_id]}".yellow + " report:\n"
    wasp[:wavereport].each do |w|
      puts w
    end
    print "\n"
  end      
end

#_attack(wasps, num, conn, urls, time = nil) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/wasp/wasp.rb', line 415

def _attack (wasps, num, conn, urls, time=nil)
  coop = conn / wasps.count
  num = num / wasps.count if not num.nil?
  
  print "Get communication channel for wasps..\n"
  threads = []
  wasps.each do |b|
    threads << Thread.new(b) { |wasp|
      begin
        wasp[:ssh] = Net::SSH.start(wasp[:instance_name], wasp[:login], 
                     :keys => ENV['HOME'] + '/.ssh/' + wasp[:key_name] + '.pem')
        print "Wasp " + "#{wasp[:instance_id]}".yellow + " is joining the wasp.\n"                       
      rescue => ex
        puts "[Error] ".red + "#{ex.message}"
        puts "Cannot reach to " + "#{wasp[:instance_id]}".yellow
      end
    }
  end      
  threads.each { |aThread| aThread.join }
  
  threads = []
  i = 0
  wasps.each do |b|
    url = urls[i%urls.count]
    i += 1
    threads << Thread.new(b) { |wasp|
      begin
      weapon = get_weapon
      if weapon.check(wasp[:ssh]) then 
        print "Wasp " + "#{wasp[:instance_id]}".yellow + " is firing its stings. Ping ping!\n"
        
          wasp[:rawreport] = weapon.fire(num, coop, url, wasp[:instance_id], @report, @keepalive, @without_cookie, time, @header) 
          
          print "Wasp " + "#{wasp[:instance_id]}".yellow + " is out of ammo.\n"
        else
          print "Wasp " + "#{wasp[:instance_id]}".yellow + " has no weapon.\n"
        end
      rescue => ex
        print "[WARN]".yellow + " #{ex.message}.\n"
      end
    }
  end
  
  threads.each { |aThread| aThread.join }
  
  wasps.each do |wasp|
    begin
      wasp[:ssh].close
    rescue => ex
      puts "[WARN]".yellow + "#{ex.message}"
    end
  end
  
  wasps
end

#_rangeattack(wasps, to, time, urls, keep = false) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/wasp/wasp.rb', line 321

def _rangeattack(wasps, to, time, urls, keep=false)
  count = time / WASP::Const::DEFAULT_WAVE_TIME
  awave = to / count
  wasp_count = wasps.count
  increase = awave / wasp_count
  
  coopwasps = []
  sum = increase
  
  count.times do 
    coopwasps.push(sum)
    sum += increase
  end
     
  print "Get communication channel for wasps..\n"
  threads = []
  wasps.each do |b|
    threads << Thread.new(b) { |wasp|
      begin
        wasp[:ssh] = Net::SSH.start(wasp[:instance_name], wasp[:login], 
                     :keys => ENV['HOME'] + '/.ssh/' + wasp[:key_name] + '.pem')
        print "Wasp " + "#{wasp[:instance_id]}".yellow + " is joining the wasp.\n"                       
      rescue => ex
        puts "[Error] ".red + "#{ex.message}"
        puts "Cannot reach to " + "#{wasp[:instance_id]}".yellow
      end
    }
  end      
  threads.each { |aThread| aThread.join }

  wave = 1
  if keep then
    per_wave = to
  else
    per_wave = awave
  end
  print "Total " + "#{count}".green + " waves(per *#{per_wave}/#{WASP::Const::DEFAULT_WAVE_TIME}secs) are coming..\n"
  while wave <= count do
    if keep then
      swarm = to
    else
      swarm = awave * wave
    end
    puts "#{wave}".yellow + " wave of swarm is coming (" + "#{swarm}".yellow + " wasps).."
    threads = []
    i = 0
    wasps.each do |b|
      url = urls[i%urls.count]
      i += 1
      #puts "Attack the target : #{url}"
      threads << Thread.new(b) { |wasp|
        begin                                    
	      weapon = get_weapon
        if weapon.check(wasp[:ssh]) then     
          if keep then
            coop = to / wasp_count
          else
            coop = coopwasps[wave-1]
           end
            wasp[:wavereport] = weapon.wavefire(coop, url, wasp[:instance_id], wave, @keepalive, @without_cookie, @header)
          else
            puts "Wasp " + "#{wasp[:instance_id]}".yellow + " has no weapon."
          end
        rescue => ex
          print "[WARN]".yellow + " #{ex.message}.\n"
        end
      }
    end
    threads.each { |aThread| aThread.join }

    partial_report(wasps, wave)

    wave += 1
    if wave <= count then
      print "Waiting " + "#{WASP::Const::DEFAULT_COOLDOWN}".yellow + " seconds for cooling down\n"
      sleep WASP::Const::DEFAULT_COOLDOWN
    else
      cool = WASP::Const::DEFAULT_COOLDOWN / 5
      print "Waiting " + "#{cool}".yellow + " seconds for cooling down\n"
      sleep cool
    end
  end
  
  wasps.each do |wasp|
    begin
      wasp[:ssh].close
    rescue => ex
      puts "[WARN]".yellow + "#{ex.message}"
    end
  end
  
  return wasps, count
end

#airfieldObject



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/wasp/wasp.rb', line 277

def airfield
  air = @ec2.get_regions
  print "Available airfield:\n"
  if not air.empty? then
    air.each do |a|
      print "#{a}".yellow  + "\n"
    end
  else
    print "No airfield is available."
  end
end

#assemble_waspsObject



117
118
119
120
121
# File 'lib/wasp/wasp.rb', line 117

def assemble_wasps
  puts "Assemling wasps."
  wasps = @ec2.get_wasps
  wasps
end

#attack(num, conn, url, time = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wasp/wasp.rb', line 70

def attack(num, conn, url, time=nil)
  wasps = assemble_wasps
  maximum = wasps.count * WASP::Const::DEFAULT_MAXIMUM_STINGS  
  if conn > maximum then
    print "[WARN]".yellow + " concurrent attack exceeded maximum number of wasps.\n" 
    print "Currently maximum number of concurrent attack is " + "#{maximum}".green + ".\n"
    print "Please breed more wasps.\n"
    exit false
  end
  
  urls = url.split(';')
  
  puts "Organizing the wasp."
  
  stingless_wasps = _attack(wasps, num, conn, urls, time)
  
  print "Offensive complete.\n\n"
  
  retrive_report(stingless_wasps) if @report
  
  mission_report(stingless_wasps)
  
  puts "The wasp is awaiting new orders."
end

#breedObject



58
59
60
61
62
63
# File 'lib/wasp/wasp.rb', line 58

def breed
  if @ec2.create == false then
    puts "[Error]".red + " Breeding wasps failed"
    exit false
  end
end

#downObject



65
66
67
68
# File 'lib/wasp/wasp.rb', line 65

def down
  puts "Calling off the wasp."
  @ec2.terminate
end

#equipObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/wasp/wasp.rb', line 298

def equip
  wasps = assemble_wasps
  threads = []
  print "Wasps are checking its weapon..(This will just take a minute) "
  wasps.each do |b|
    threads << Thread.new(b) { |wasp|          
      begin
        Net::SSH.start(wasp[:instance_name], wasp[:login], 
                       :keys => ENV['HOME'] + '/.ssh/' + wasp[:key_name] + '.pem', 
                       :verbose => :fatal) do |ssh|
          weapon = get_weapon
          weapon.reload if not weapon.check(ssh)
        end
      rescue => ex
        print "[WARN]".yellow + " #{ex.message}\n"
      end    
    }
  end
  
  threads.each { |aThread| aThread.join }
  puts "OK".green
end

#get_weaponObject



289
290
291
292
293
294
295
296
# File 'lib/wasp/wasp.rb', line 289

def get_weapon
  case
  when @weapon_name == 'ab' then
    WASP::WeaponAB.new
  else
    WASP::WeaponAB.new
  end
end

#mission_report(stingless_wasps) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/wasp/wasp.rb', line 222

def mission_report(stingless_wasps)
  report = []
  @weapon = get_weapon

  puts "Wasps report:"
  stingless_wasps.each do |wasp|
    next if wasp[:rawreport].nil?
    
    case
    when wasp[:rawreport].match(/currently not installed/) then
      print "[WARN]".yellow + " Some of wasps has no weapon to attack.\n"
    
    when wasp[:rawreport].match(/Unknown error/) then
      print "[WARN]".yellow + " Some of wasps lost sight of the target.\n"
      
    when wasp[:rawreport].match(/Requests per second/) then
      report.push(@weapon.points(wasp[:rawreport]))        
      
    else 
      print "[WARN]".yellow + " Some of wasps lost completely.\n"
    end
  end
  
  if report.empty? then
    print "Nothing to report.\n\n"
  else
    @weapon.summary(report)
  end
end

#partial_report(stingless_wasps, wave) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/wasp/wasp.rb', line 136

def partial_report(stingless_wasps, wave)
  @weapon = get_weapon
  wavereport = []

  print "#{wave} ".yellow + "wave report:\n"
  stingless_wasps.each do |wasp|
    report = wasp[:wavereport]
    next if report.nil?
    case
    when report.match(/currently not installed/) then
      print "[WARN]".yellow + " Some of wasps has no weapon to attack.\n"

    when report.match(/Unknown error/) then
      print "[WARN]".yellow + " Some of wasps lost sight of the target.\n"

    when report.match(/Requests per second/) then
      wavereport.push @weapon.points(report)

    else 
      print "[WARN]".yellow + " In wave " + "#{wave}".yellow + " Some of wasps lost completely.\n"
      print "[LOG]\n"
      print "#{report}\n"
    end        
  end

  if wavereport.count < 1 then
      print "This wave has no report. Target is " + "unavailable".red + ".\n\n"
  else
    if @compact then
      @weapon.compact_summary(wavereport)
    else
      @weapon.summary(wavereport)
    end
  end

end

#rangeattack(to, time, url, keep = false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wasp/wasp.rb', line 95

def rangeattack(to, time, url, keep=false)
  wasps = assemble_wasps
  maximum = wasps.count * WASP::Const::DEFAULT_MAXIMUM_STINGS  
  if to > maximum then
    print "[WARN]".yellow + " concurrent attack exceeded maximum number of wasps.\n" 
    print "Currently maximum number of attack is " + "#{maximum}".green + ".\n"
    print "Please breed more wasps.\n"
    exit false
  end
  
  urls = url.split(';')
  
  stingless_wasps, waves = _rangeattack(wasps, to, time, urls, keep)
  print "Waves are finished.\n\n"
  
  #retrive_report(stingless_wasps, true)
  
  #wave_report(stingless_wasps, waves)
  
  puts "The wasp is awaiting new orders."
end

#readyObject



53
54
55
56
# File 'lib/wasp/wasp.rb', line 53

def ready
  @ec2.get_keypair
  @ec2.get_security_group
end

#retrive_report(wasps, iswave = false) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/wasp/wasp.rb', line 471

def retrive_report (wasps, iswave=false)
  threads = []
  
  print "Retrieve report from wasps.."
  wasps.each do |b|
    threads << Thread.new(b) { |wasp|
      if not iswave then
        remotepath = "#{wasp[:instance_id]}.zip"
        localpath = File.dirname(__FILE__) + "/../../report/#{wasp[:instance_id]}.zip"
      else
        remotepath = "#{wasp[:instance_id]}-wave.tar.gz"
        localpath = File.dirname(__FILE__) + "/../../report/#{wasp[:instance_id]}-wave.tar.gz"
      end
      
      begin
        Net::SSH.start(wasp[:instance_name], wasp[:login], 
                       :keys => ENV['HOME'] + '/.ssh/' + wasp[:key_name] + '.pem', 
                       :verbose => :fatal) do |ssh|
          ssh.scp.download! remotepath, localpath
        end
      rescue => ex
        print "[WARN]".yellow + " #{ex.message}.\n"
      end
    }
  end
  
  threads.each { |aThread| aThread.join }
  begin
    if not iswave then
      report_files = File.dirname(__FILE__) + "/../../report/*.zip"          
    else
      report_files = File.dirname(__FILE__) + "/../../report/*.tar.gz"
    end
    now = DateTime.now.to_s
    now_dir = File.dirname(__FILE__) + "/../../report/" + now
    Dir.mkdir(now_dir)
    FileUtils.mv(Dir.glob(report_files), now_dir)
  rescue => ex
    print "[WARN]".yellow + " #{ex.message}\n"
  end
  puts "OK".green
end

#statusObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/wasp/wasp.rb', line 252

def status
  wasps = @ec2.status
   
  count = 0
  if wasps.nil? == false then     
    wasps.each do |zone, info| 
      puts "Zone " + "#{zone}".green + ":"     
      threads = []
      info[:instances].each do |ins|
        threads << Thread.new(ins) { |i|
          if @ec2.get_wasp_status(zone, i) == :running then
            dns = @ec2.get_wasp_domain(zone, i)
            print "Wasp " + "#{i}".yellow + " is ready to serve. (#{dns})\n"
            count += 1
          else
            print "Wasp " + "#{i}".red + " is hanging around somewhere.\n"
          end
        }
      end
      threads.each { |aThread| aThread.join }
    end
    print "\n#{count}".green + " Wasps are ready to serve.\n"
  end    
end

#wave_report(stingless_wasps, waves) ⇒ Object



173
174
175
176
177
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
216
217
218
219
220
# File 'lib/wasp/wasp.rb', line 173

def wave_report(stingless_wasps, waves)
  wavereport = {}
  i = 1
  while i <= waves do
    wavereport[i.to_s] = []
    i += 1
  end
  
  @weapon = get_weapon
  
  puts "Wave report:"
  
  stingless_wasps.each do |wasp|
    next if wasp[:wavereport].nil?
    wave = 1
    wasp[:wavereport].each do |report|
      case
      when report.match(/currently not installed/) then
        print "[WARN]".yellow + " Some of wasps has no weapon to attack.\n"

      when report.match(/Unknown error/) then
        print "[WARN]".yellow + " Some of wasps lost sight of the target.\n"

      when report.match(/Requests per second/) then
        wavereport[wave.to_s].push(@weapon.points(report))        

      else 
        print "[WARN]".yellow + " In wave " + "#{wave}".yellow + " Some of wasps lost completely.\n"
        print "[LOG]\n"
        print "#{report}\n"
      end
      wave += 1
    end
  end        
  
  wavereport.keys.each do |report|
    print "#{report} ".yellow + "wave report:\n"
    if wavereport[report].count < 1 then
      print "This wave has no report. Target is " + "unavailable".red + ".\n\n"
    else
      if @compact then
        @weapon.compact_summary(wavereport[report])
      else
        @weapon.summary(wavereport[report])
      end
    end
  end
end