ovas 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ovas.rb +882 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c1e122495964108d9dcdd8fdd82b5758da371f79
4
+ data.tar.gz: 462f79af97f2ce81e95b6540dd50ddb6c2437e1c
5
+ SHA512:
6
+ metadata.gz: 43c3a04c6c45173d566aed8144ae8837a30a711984b974d01f46f2978845980d2c99056ea822ba5860f8dab822fe9f52f4a0c59c9fa0ebc6bfa332c48e3af470
7
+ data.tar.gz: 9878073e2e82a39d578db93b129ec76e56ccb10071d1053bf7d3ba41e8b71f5ae2856577e18b49ae47de53d0dbc0d4a5245e6d44dd82d487dcd34b9a2d74eb71
data/lib/ovas.rb ADDED
@@ -0,0 +1,882 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+ require 'openssl'
4
+ require 'rexml/document'
5
+ require 'rexml/text'
6
+ require 'base64'
7
+ require 'byebug'
8
+
9
+ # OpenVASOMP module
10
+ #
11
+ # Usage:
12
+ #
13
+ # require 'ovas'
14
+ #
15
+
16
+ module OpenVASOMP
17
+
18
+ class OMPError < :: RuntimeError
19
+ attr_accessor :req, :reason
20
+ def initialize(req, reason = '')
21
+ self.req = req
22
+ self.reason = reason
23
+ end
24
+ def to_s
25
+ "OpenVAS OMP: #{self.reason}"
26
+ end
27
+ end
28
+
29
+ class OMPResponseError < OMPError
30
+ def initialize
31
+ self.reason = "Error in OMP request/response"
32
+ end
33
+ end
34
+
35
+ class OMPAuthError < OMPError
36
+ def initialize
37
+ self.reason = "Authentication failed"
38
+ end
39
+ end
40
+
41
+ class XMLParsingError < OMPError
42
+ def initialize
43
+ self.reason = "XML parsing failed"
44
+ end
45
+ end
46
+
47
+ # Core class for OMP communication protocol
48
+ class OpenVASOMP
49
+ # initialize object: try to connect to OpenVAS using URL, user and password
50
+ #
51
+ # Usage:
52
+ #
53
+ # ov=OpenVASOMP.new(user=>'user',password=>'pass')
54
+ # # default: host=>'localhost', port=>'9390'
55
+ #
56
+ def initialize(p={})
57
+ if p.has_key?("host")
58
+ @host=p["host"]
59
+ else
60
+ @host="localhost"
61
+ end
62
+ if p.has_key?("port")
63
+ @port=p["port"]
64
+ else
65
+ @port=9390
66
+ end
67
+ if p.has_key?("user")
68
+ @user=p["user"]
69
+ else
70
+ @user="openvas"
71
+ end
72
+ if p.has_key?("password")
73
+ @password=p["password"]
74
+ else
75
+ @password="openvas"
76
+ end
77
+ if p.has_key?("bufsize")
78
+ @bufsize=p["bufsize"]
79
+ else
80
+ @bufsize=16384
81
+ end
82
+ if p.has_key?("debug")
83
+ @debug=p["debug"]
84
+ else
85
+ @debug=0
86
+ end
87
+
88
+ if @debug>3
89
+ puts "Host: "+@host
90
+ puts "Port: "+@port.to_s()
91
+ puts "User: "+@user
92
+ end
93
+ if @debug>99
94
+ puts "Password: "+@password
95
+ end
96
+ @areq=''
97
+ @read_timeout=3
98
+ if defined? p["noautoconnect"] and not p["noautoconnect"]
99
+ connect()
100
+ if defined? p["noautologin"] and not p["noautologin"]
101
+ login()
102
+ end
103
+ end
104
+ end
105
+
106
+ # Sets debug level
107
+ #
108
+ # Usage:
109
+ #
110
+ # ov.debug(3)
111
+ #
112
+ def debug (level)
113
+ @debug=level
114
+ end
115
+
116
+ # Low level method - Connect to SSL socket
117
+ #
118
+ # Usage:
119
+ #
120
+ # ov.connect()
121
+ #
122
+ def connect
123
+ @plain_socket=TCPSocket.open(@host, @port)
124
+ ssl_context = OpenSSL::SSL::SSLContext.new()
125
+ @socket = OpenSSL::SSL::SSLSocket.new(@plain_socket, ssl_context)
126
+ @socket.sync_close = true
127
+ @socket.connect
128
+ end
129
+
130
+ # Low level method - Disconnect SSL socket
131
+ #
132
+ # Usage:
133
+ #
134
+ # ov.disconnect()
135
+ #
136
+ def disconnect
137
+ if @socket
138
+ @socket.close
139
+ end
140
+ end
141
+
142
+ # Low level method: Send request and receive response - socket
143
+ #
144
+ # Usage:
145
+ #
146
+ # ov.connect();
147
+ # puts ov.sendrecv("<get_version/>")
148
+ # ov.disconnect();
149
+ #
150
+ def sendrecv (tosend)
151
+ if not @socket
152
+ connect
153
+ end
154
+
155
+ if @debug>3 then
156
+ puts "SENDING: "+tosend
157
+ end
158
+ @socket.puts(tosend)
159
+
160
+ @rbuf=''
161
+ size=0
162
+ begin
163
+ begin
164
+ Timeout.timeout(@read_timeout) {
165
+ a = @socket.sysread(@bufsize)
166
+ size=a.length
167
+ # puts "sysread #{size} bytes"
168
+ @rbuf << a
169
+ }
170
+ rescue Timeout::Error
171
+ size=0
172
+ rescue EOFError
173
+ raise OMPResponseError
174
+ end
175
+ end while size>=@bufsize
176
+ response=@rbuf
177
+
178
+ if @debug>3 then
179
+ puts "RECEIVED: "+response
180
+ end
181
+ return response
182
+ end
183
+
184
+ # get OMP version (you don't need to be authenticated)
185
+ #
186
+ # Usage:
187
+ #
188
+ # ov.version_get()
189
+ #
190
+ def version_get
191
+ vreq="<get_version/>"
192
+ resp=sendrecv(vreq)
193
+ resp = "<X>"+resp+"</X>"
194
+ begin
195
+ docxml = REXML::Document.new(resp)
196
+ version=''
197
+ version=docxml.root.elements['get_version_response'].elements['version'].text
198
+ return version
199
+ rescue
200
+ raise XMLParsingError
201
+ end
202
+ end
203
+
204
+ # produce single XML element with attributes specified as hash
205
+ # low-level function
206
+ #
207
+ # Usage:
208
+ #
209
+ # ov.xml_attr()
210
+ #
211
+ def xml_attr(name, opts={})
212
+ xml = REXML::Element.new(name)
213
+ opts.keys.each do |k|
214
+ xml.attributes[k] = opts[k]
215
+ end
216
+ return xml
217
+ end
218
+
219
+ # produce multiple XML elements with text specified as hash
220
+ # low-level function
221
+ #
222
+ # Usage:
223
+ #
224
+ # ov.xml_ele()
225
+ #
226
+ def xml_ele(name, child={})
227
+ xml = REXML::Element.new(name)
228
+ child.keys.each do |k|
229
+ xml.add_element(k)
230
+ xml.elements[k].text = child[k]
231
+ end
232
+ return xml
233
+ end
234
+
235
+ # produce multiple XML elements with text specified as hash
236
+ # also produce multiple XML elements with attributes
237
+ # low-level function
238
+ #
239
+ # Usage:
240
+ #
241
+ # ov.xml_mix()
242
+ #
243
+ def xml_mix(name, child, attr, elem)
244
+ xml = REXML::Element.new(name)
245
+ child.keys.each do |k|
246
+ xml.add_element(k)
247
+ xml.elements[k].text = child[k]
248
+ end
249
+ elem.keys.each do |k|
250
+ xml.add_element(k)
251
+ xml.elements[k].attributes[attr] = elem[k]
252
+ end
253
+ return xml
254
+ end
255
+
256
+ # login to OpenVAS server.
257
+ # if successful returns authentication XML for further usage
258
+ # if unsuccessful returns empty string
259
+ #
260
+ # Usage:
261
+ #
262
+ # ov.login()
263
+ #
264
+ def login
265
+ areq="<authenticate>"+xml_ele("credentials", {"username"=>@user, "password"=>@password}).to_s()+"</authenticate>"
266
+ resp=sendrecv(areq)
267
+ # wrap it inside tags, so rexml does not complain
268
+ resp = "<X>"+resp+"</X>"
269
+
270
+ begin
271
+ docxml = REXML::Document.new(resp)
272
+ status=docxml.root.elements['authenticate_response'].attributes['status'].to_i()
273
+ rescue
274
+ raise XMLParsingError
275
+ end
276
+ if status == 200
277
+ @areq=areq
278
+ else
279
+ raise OMPAuthError
280
+ end
281
+ end
282
+
283
+ # check if we're successful logged in
284
+ # if successful returns true
285
+ # if unsuccessful returns false
286
+ #
287
+ # Usage:
288
+ #
289
+ # if ov.logged_in() then
290
+ # puts "logged in"
291
+ # end
292
+ #
293
+ def logged_in
294
+ if @areq == ''
295
+ return false
296
+ else
297
+ return true
298
+ end
299
+ end
300
+
301
+ # logout from OpenVAS server.
302
+ # it actually just sets internal authentication XML to empty str
303
+ # (as in OMP you have to send username/password each time)
304
+ # (i.e. there is no session id)
305
+ #
306
+ # Usage:
307
+ #
308
+ # ov.logout()
309
+ #
310
+ def logout
311
+ disconnect()
312
+ @areq = ''
313
+ end
314
+
315
+ # OMP low level method - Send string request wrapped with
316
+ # authentication XML and return response as string
317
+ #
318
+ # Usage:
319
+ #
320
+ # ov.request_xml("<HELP/")
321
+ #
322
+ def omp_request_raw (request)
323
+ resp=sendrecv(@areq+request)
324
+ return resp
325
+ end
326
+
327
+ # OMP low level method - Send string request wrapped with
328
+ # authentication XML and return REXML parsed object
329
+ #
330
+ # Usage:
331
+ #
332
+ # rexmlobject = ov.request_xml("<HELP/")
333
+ #
334
+ def omp_request_xml (request)
335
+ resp=sendrecv(@areq+request)
336
+ resp = "<X>"+resp+"</X>"
337
+
338
+ begin
339
+ docxml = REXML::Document.new(resp)
340
+ status=docxml.root.elements['authenticate_response'].attributes['status'].to_i
341
+ if status<200 and status>299
342
+ raise OMPAuthError
343
+ end
344
+ return docxml.root
345
+ rescue
346
+ raise XMLParsingError
347
+ end
348
+ end
349
+
350
+ # OMP - Create target for scanning
351
+ #
352
+ # Usage:
353
+ #
354
+ # target_id = ov.target_create("name"=>"localhost",
355
+ # "hosts"=>"127.0.0.1","comment"=>"yes")
356
+ #
357
+ def target_create (p={})
358
+ xmlreq=xml_ele("create_target", p).to_s()
359
+
360
+ begin
361
+ xr=omp_request_xml(xmlreq)
362
+ id=xr.elements['create_target_response'].attributes['id']
363
+ rescue
364
+ raise OMPResponseError
365
+ end
366
+ return id
367
+ end
368
+
369
+ # OMP - Delete target
370
+ #
371
+ # Usage:
372
+ #
373
+ # ov.target_delete(target_id)
374
+ #
375
+ def target_delete (id)
376
+ xmlreq=xml_attr("delete_target",{"target_id" => id}).to_s()
377
+ begin
378
+ xr=omp_request_xml(xmlreq)
379
+ rescue
380
+ raise OMPResponseError
381
+ end
382
+ return xr
383
+ end
384
+
385
+ # OMP - Get target for scanning and returns rexml object
386
+ #
387
+ # Usage:
388
+ # rexmlobject = target_get_raw("target_id"=>target_id)
389
+ #
390
+ def target_get_raw (p={})
391
+ xmlreq=xml_attr("get_targets", p).to_s()
392
+
393
+ begin
394
+ xr=omp_request_xml(xmlreq)
395
+ return xr
396
+ rescue
397
+ raise OMPResponseError
398
+ end
399
+ end
400
+
401
+ # OMP - Get all targets for scanning and returns array of hashes
402
+ # with following keys: id,name,comment,hosts,max_hosts,in_use
403
+ #
404
+ # Usage:
405
+ # array_of_hashes = target_get_all()
406
+ #
407
+ def target_get_all (p={})
408
+ begin
409
+ xr=target_get_raw(p)
410
+ list=Array.new
411
+ xr.elements.each('//get_targets_response/target') do |target|
412
+ td=Hash.new
413
+ td["id"]=target.attributes["id"]
414
+ td["name"]=target.elements["name"].text
415
+ td["comment"]=target.elements["comment"].text
416
+ td["hosts"]=target.elements["hosts"].text
417
+ td["max_hosts"]=target.elements["max_hosts"].text
418
+ td["in_use"]=target.elements["in_use"].text
419
+ list.push td
420
+ end
421
+ return list
422
+ rescue
423
+ raise OMPResponseError
424
+ end
425
+ end
426
+
427
+ def target_get_byid (id)
428
+ begin
429
+ xr=target_get_raw("target_id"=>id)
430
+ xr.elements.each('//get_targets_response/target') do |target|
431
+ td=Hash.new
432
+ td["id"]=target.attributes["id"]
433
+ td["name"]=target.elements["name"].text
434
+ td["comment"]=target.elements["comment"].text
435
+ td["hosts"]=target.elements["hosts"].text
436
+ td["max_hosts"]=target.elements["max_hosts"].text
437
+ td["in_use"]=target.elements["in_use"].text
438
+ return td
439
+ end
440
+ return list
441
+ rescue
442
+ raise OMPResponseError
443
+ end
444
+ end
445
+
446
+ # OMP - get reports and returns raw rexml object as response
447
+ #
448
+ # Usage:
449
+ #
450
+ # rexmlobject=ov.report_get_raw("format"=>"PDF")
451
+ #
452
+ # rexmlobject=ov.report_get_raw(
453
+ # "report_id" => "",
454
+ # "format"=>"PDF")
455
+ #
456
+ def report_get_raw (p={})
457
+ xmlreq=xml_attr("get_reports",p).to_s()
458
+ begin
459
+ xr=omp_request_xml(xmlreq)
460
+ rescue
461
+ raise OMPResponseError
462
+ end
463
+ return xr
464
+ end
465
+
466
+ # OMP - get report by id and format, returns report
467
+ # (already base64 decoded if needed)
468
+ #
469
+ # format can be: HTML, NBE, PDF, ...
470
+ #
471
+ # Usage:
472
+ #
473
+ # pdf_content=ov.report_get_byid(id,"PDF")
474
+ # File.open('report.pdf', 'w') {|f| f.write(pdf_content) }
475
+ #
476
+ def report_get_byid (id,format)
477
+ decode=Array["HTML","NBE","PDF"]
478
+ xr=report_get_raw("report_id"=>id,"format"=>format)
479
+ output_file = File.open("tmp/#{xr.elements['get_reports_response'].elements['report'].attributes['id']}.xml", "w")
480
+ output_file_path = output_file.path
481
+ output_file.puts(xr)
482
+ output_file.close
483
+ return output_file_path
484
+ # resp=xr.elements['get_reports_response'].elements['report'].text
485
+ # if decode.include?(format)
486
+ # resp=Base64.decode64(resp)
487
+ # end
488
+ # return resp
489
+ end
490
+
491
+ # OMP - get report all, returns report
492
+ #
493
+ # Usage:
494
+ #
495
+ # pdf_content=ov.report_get_all()
496
+ #
497
+ def report_get_all ()
498
+ begin
499
+ xr=report_get_raw("format"=>"NBE")
500
+ list=Array.new
501
+ xr.elements.each('//get_reports_response/report') do |report|
502
+ td=Hash.new
503
+ td["id"]=target.attributes["id"]
504
+ td["name"]=target.elements["name"].text
505
+ td["comment"]=target.elements["comment"].text
506
+ td["hosts"]=target.elements["hosts"].text
507
+ td["max_hosts"]=target.elements["max_hosts"].text
508
+ td["in_use"]=target.elements["in_use"].text
509
+ list.push td
510
+ end
511
+ return list
512
+ rescue
513
+ raise OMPResponseError
514
+ end
515
+ end
516
+
517
+ # OMP - get reports and returns raw rexml object as response
518
+ #
519
+ # Usage:
520
+ #
521
+ # rexmlobject=ov.result_get_raw("notes"=>0)
522
+ #
523
+ def result_get_raw (p={})
524
+ begin
525
+ xmlreq=xml_attr("get_results",p).to_s()
526
+ xr=omp_request_xml(xmlreq)
527
+ rescue
528
+ raise OMPResponseError
529
+ end
530
+ return xr
531
+ end
532
+
533
+ # OMP - get configs and returns rexml object as response
534
+ #
535
+ # Usage:
536
+ #
537
+ # rexmldocument=ov.config_get_raw()
538
+ #
539
+ def config_get_raw (p={})
540
+ xmlreq=xml_attr("get_configs",p).to_s()
541
+ begin
542
+ xr=omp_request_xml(xmlreq)
543
+ return xr
544
+ rescue
545
+ raise OMPResponseError
546
+ end
547
+ return false
548
+ end
549
+
550
+ # OMP - get configs and returns hash as response
551
+ # hash[config_id]=config_name
552
+ #
553
+ # Usage:
554
+ #
555
+ # array_of_hashes=ov.config_get_all()
556
+ #
557
+ def config_get_all (p={})
558
+ begin
559
+ xr=config_get_raw(p)
560
+ tc=Array.new
561
+ xr.elements.each('//get_configs_response/config') do |config|
562
+ c=Hash.new
563
+ c["id"]=config.attributes["id"]
564
+ c["name"]=config.elements["name"].text
565
+ c["comment"]=config.elements["comment"].text
566
+ tc.push c
567
+ end
568
+ return tc
569
+ rescue
570
+ raise OMPResponseError
571
+ end
572
+ return false
573
+ end
574
+
575
+ # OMP - get configs and returns hash as response
576
+ # hash[config_id]=config_name
577
+ #
578
+ # Usage:
579
+ #
580
+ # all_configs_hash=ov.config.get()
581
+ #
582
+ # config_id=ov.config_get().index("Full and fast")
583
+ #
584
+ def config_get (p={})
585
+ begin
586
+ xr=config_get_raw(p)
587
+ list=Hash.new
588
+ xr.elements.each('//get_configs_response/config') do |config|
589
+ id=config.attributes["id"]
590
+ name=config.elements["name"].text
591
+ list[id]=name
592
+ end
593
+ return list
594
+ rescue
595
+ raise OMPResponseError
596
+ end
597
+ return false
598
+ end
599
+
600
+ # OMP - copy config with new name and returns new id
601
+ #
602
+ # Usage:
603
+ #
604
+ # new_config_id=config_copy(config_id,"new_name");
605
+ #
606
+ def config_copy (config_id,name)
607
+ xmlreq=xml_attr("create_config",
608
+ {"copy"=>config_id,"name"=>name}).to_s()
609
+ begin
610
+ xr=omp_request_xml(xmlreq)
611
+ id=xr.elements['create_config_response'].attributes['id']
612
+ return id
613
+ rescue
614
+ raise OMPResponseError
615
+ end
616
+ end
617
+
618
+ # OMP - create config with specified RC file and returns new id
619
+ # name = name of new config
620
+ # rcfile = base64 encoded OpenVAS rcfile
621
+ #
622
+ # Usage:
623
+ #
624
+ # config_id=config_create("name",rcfile);
625
+ #
626
+ def config_create (name,rcfile)
627
+ xmlreq=xml_attr("create_config",
628
+ {"name"=>name,"rcfile"=>rcfile}).to_s()
629
+ begin
630
+ xr=omp_request_xml(xmlreq)
631
+ id=xr.elements['create_config_response'].attributes['id']
632
+ return id
633
+ rescue
634
+ raise OMPResponseError
635
+ end
636
+ end
637
+
638
+ # OMP - creates task and returns id of created task
639
+ #
640
+ # Parameters which usually fit in p hash and i hash:
641
+ # p = name,comment,rcfile
642
+ # i = config,target,escalator,schedule
643
+ #
644
+ # Usage:
645
+ #
646
+ # task_id=ov.task_create_raw()
647
+ #
648
+ def task_create_raw (p={}, i={})
649
+ xmlreq=xml_mix("create_task",p,"id",i).to_s()
650
+ begin
651
+ xr=omp_request_xml(xmlreq)
652
+ id=xr.elements['create_task_response'].attributes['id']
653
+ return id
654
+ rescue
655
+ raise OMPResponseError
656
+ end
657
+ end
658
+
659
+ # OMP - creates task and returns id of created task
660
+ #
661
+ # parameters = name,comment,rcfile,config,target,escalator,
662
+ # schedule
663
+ #
664
+ # Usage:
665
+ #
666
+ # config_id=o.config_get().index("Full and fast")
667
+ # target_id=o.target_create(
668
+ # {"name"=>"localtarget", "hosts"=>"127.0.0.1", "comment"=>"t"})
669
+ # task_id=ov.task_create(
670
+ # {"name"=>"testlocal","comment"=>"test", "target"=>target_id,
671
+ # "config"=>config_id}
672
+ #
673
+ def task_create (p={})
674
+ specials=Array["config","target","escalator","schedule"]
675
+ ids = Hash.new
676
+ specials.each do |spec|
677
+ if p.has_key?(spec)
678
+ ids[spec]=p[spec]
679
+ p.delete(spec)
680
+ end
681
+ end
682
+ return task_create_raw(p,ids)
683
+ end
684
+
685
+ # OMP - deletes task specified by task_id
686
+ #
687
+ # Usage:
688
+ #
689
+ # ov.task_delete(task_id)
690
+ #
691
+ def task_delete (task_id)
692
+ xmlreq=xml_attr("delete_task",{"task_id" => task_id}).to_s()
693
+ begin
694
+ xr=omp_request_xml(xmlreq)
695
+ rescue
696
+ raise OMPResponseError
697
+ end
698
+ return xr
699
+ end
700
+
701
+ # OMP - get task and returns raw rexml object as response
702
+ #
703
+ # Usage:
704
+ #
705
+ # rexmlobject=ov.task_get_raw("details"=>"0")
706
+ #
707
+ def task_get_raw (p={})
708
+ xmlreq=xml_attr("get_tasks",p).to_s()
709
+ begin
710
+ xr=omp_request_xml(xmlreq)
711
+ return xr
712
+ rescue
713
+ raise OMPResponseError
714
+ end
715
+ end
716
+
717
+ # OMP - get all tasks and returns array with hashes with
718
+ # following content:
719
+ # id,name,comment,status,progress,first_report,last_report
720
+ #
721
+ # Usage:
722
+ #
723
+ # array_of_hashes=ov.task_get_all()
724
+ #
725
+ def task_get_all (p={})
726
+ xr=task_get_raw(p)
727
+ t=Array.new
728
+ xr.elements.each('//get_tasks_response/task') do |task|
729
+ td=Hash.new
730
+ td["id"]=task.attributes["id"]
731
+ td["name"]=task.elements["name"].text
732
+ td["comment"]=task.elements["comment"].text
733
+ td["status"]=task.elements["status"].text
734
+ td["progress"]=task.elements["progress"].text
735
+ if defined? task.elements["first_report"].elements["report"].attributes["id"] then
736
+ td["firstreport"]=task.elements["first_report"].elements["report"].attributes["id"]
737
+ else
738
+ td["firstreport"]=nil
739
+ end
740
+ if defined? task.elements["last_report"].elements["report"].attributes["id"] then
741
+ td["lastreport"]=task.elements["last_report"].elements["report"].attributes["id"]
742
+ else
743
+ td["lastreport"]=nil
744
+ end
745
+ t.push td
746
+ end
747
+ return t
748
+ end
749
+
750
+ # OMP - get task specified by task_id and returns hash with
751
+ # following content:
752
+ # id,name,comment,status,progress,first_report,last_report
753
+ #
754
+ # Usage:
755
+ #
756
+ # hash=ov.task_get_byid(task_id)
757
+ #
758
+ def task_get_byid (id)
759
+ xr=task_get_raw("task_id"=>id,"details"=>0)
760
+ xr.elements.each('//get_tasks_response/task') do |task|
761
+ td=Hash.new
762
+ td["id"]=task.attributes["id"]
763
+ td["name"]=task.elements["name"].text
764
+ td["comment"]=task.elements["comment"].text
765
+ td["status"]=task.elements["status"].text
766
+ td["progress"]=task.elements["progress"].text
767
+ if defined? task.elements["first_report"].elements["report"].attributes["id"] then
768
+ td["firstreport"]=task.elements["first_report"].elements["report"].attributes["id"]
769
+ else
770
+ td["firstreport"]=nil
771
+ end
772
+ if defined? task.elements["last_report"].elements["report"].attributes["id"] then
773
+ td["lastreport"]=task.elements["last_report"].elements["report"].attributes["id"]
774
+ else
775
+ td["lastreport"]=nil
776
+ end
777
+ return (td)
778
+ end
779
+ end
780
+
781
+ # OMP - check if task specified by task_id is finished
782
+ # (it checks if task status is "Done" in OMP)
783
+ #
784
+ # Usage:
785
+ #
786
+ # if ov.task_finished(task_id)
787
+ # puts "Task finished"
788
+ # end
789
+ #
790
+ def task_finished (id)
791
+ xr=task_get_raw("task_id"=>id,"details"=>0)
792
+ xr.elements.each('//get_tasks_response/task') do |task|
793
+ if status=task.elements["status"].text == "Done"
794
+ return true
795
+ else
796
+ return false
797
+ end
798
+ end
799
+ end
800
+
801
+ # OMP - check progress of task specified by task_id
802
+ # (OMP returns -1 if task is finished, not started, etc)
803
+ #
804
+ # Usage:
805
+ #
806
+ # print "Progress: "
807
+ # puts ov.task_progress(task_id)
808
+ #
809
+ def task_progress (id)
810
+ xr=task_get_raw("task_id"=>id,"details"=>0)
811
+ xr.elements.each('//get_tasks_response/task') do |task|
812
+ return task.elements["progress"].text.to_i()
813
+ end
814
+ end
815
+
816
+ # OMP - starts task specified by task_id
817
+ #
818
+ # Usage:
819
+ #
820
+ # ov.task_start(task_id)
821
+ #
822
+ def task_start (task_id)
823
+ xmlreq=xml_attr("start_task",{"task_id" => task_id}).to_s()
824
+ begin
825
+ xr=omp_request_xml(xmlreq)
826
+ rescue
827
+ raise OMPResponseError
828
+ end
829
+ return xr
830
+ end
831
+
832
+ # OMP - stops task specified by task_id
833
+ #
834
+ # Usage:
835
+ #
836
+ # ov.task_stop(task_id)
837
+ #
838
+ def task_stop (task_id)
839
+ xmlreq=xml_attr("stop_task",{"task_id" => task_id}).to_s()
840
+ begin
841
+ xr=omp_request_xml(xmlreq)
842
+ rescue
843
+ raise OMPResponseError
844
+ end
845
+ return xr
846
+ end
847
+
848
+ # OMP - pauses task specified by task_id
849
+ #
850
+ # Usage:
851
+ #
852
+ # ov.task_pause(task_id)
853
+ #
854
+ def task_pause (task_id)
855
+ xmlreq=xml_attr("pause_task",{"task_id" => task_id}).to_s()
856
+ begin
857
+ xr=omp_request_xml(xmlreq)
858
+ rescue
859
+ raise OMPResponseError
860
+ end
861
+ return xr
862
+ end
863
+
864
+ # OMP - resumes (or starts) task specified by task_id
865
+ #
866
+ # Usage:
867
+ #
868
+ # ov.task_resume_or_start(task_id)
869
+ #
870
+ def task_resume_or_start (task_id)
871
+ xmlreq=xml_attr("resume_or_start_task",{"task_id" => task_id}).to_s()
872
+ begin
873
+ xr=omp_request_xml(xmlreq)
874
+ rescue
875
+ raise OMPResponseError
876
+ end
877
+ return xr
878
+ end
879
+
880
+ end # end of Class
881
+
882
+ end # of Module
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ovas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - heimrych
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ovas.rb
20
+ homepage:
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.6.13
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Gem to integrate OpenVAS API to Rails app
43
+ test_files: []