-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathowasp_zap.rb
135 lines (117 loc) · 3.85 KB
/
owasp_zap.rb
1
2
3
4
5
6
7
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
require "json"
require "rest_client"
require "addressable/uri"
require "cgi"
require "logger"
require_relative "owasp_zap/version"
require_relative "owasp_zap/error"
require_relative "owasp_zap/string_extension"
require_relative "owasp_zap/spider"
require_relative "owasp_zap/attack"
require_relative "owasp_zap/alert"
require_relative "owasp_zap/auth"
require_relative "owasp_zap/scanner"
require_relative "owasp_zap/policy"
module OwaspZap
class ZapException < Exception;end
class Zap
attr_accessor :target,:base, :zap_bin
attr_reader :api_key
def initialize(params = {})
#TODO
# handle params
@base = params[:base] || "http://127.0.0.1:8080"
@target = params[:target]
@api_key = params[:api_key]
@zap_bin = params [:zap] || "#{ENV['HOME']}/ZAP/zap.sh"
@output = params[:output] || $stdout #default we log everything to the stdout
end
def status_for(component)
case component
when :ascan
Zap::Attack.new(:base=>@base,:target=>@target).status
when :spider
Zap::Spider.new(:base=>@base,:target=>@target).status
when :scan
Zap::Scan.new(:base=>@base,:target=>@target).status
else
{:status=>"unknown component"}.to_json
end
end
def ok?(json_data)
json_data.is_a?(Hash) and json_data[0] == "OK"
end
def running?
begin
response = RestClient::get "#{@base}"
rescue Errno::ECONNREFUSED
return false
end
response.code == 200
end
def policy
Zap::Policy.new(:base=>@base)
end
def alerts
Zap::Alert.new(:base=>@base,:target=>@target)
end
def scanner
Zap::Scanner.new(:base=>@base)
end
#attack
def ascan
Zap::Attack.new(:base=>@base,:target=>@target)
end
def spider
Zap::Spider.new(:base=>@base,:target=>@target)
end
def auth
Zap::Auth.new(:base=>@base)
end
# TODO
# DOCUMENT the step necessary: install ZAP under $home/ZAP or should be passed to new as :zap parameter
def start(params = {})
# default we are disabling api key
params = {api_key:false}.merge(params)
cmd_line = "#{@zap_bin}"
case
when params.key?(:daemon)
cmd_line += " -daemon"
when params.key?(:api_key)
cmd_line += if params[:api_key] == true
" -config api.key=#{@api_key}"
else
" -config api.disablekey=true"
end
end
if params.key?(:host)
cmd_line += " -host #{params[:host]}"
end
if params.key?(:port)
cmd_line += " -port #{params[:port]}"
end
fork do
# if you passed :output=>"file.txt" to the constructor, then it will send the forked process output
# to this file (that means, ZAP stdout)
unless @output == $stdout
STDOUT.reopen(File.open(@output, 'w+'))
STDOUT.sync = true
end
print "Running the following command: #{cmd_line} \n"
exec cmd_line
end
end
#shutdown zap
def shutdown
RestClient::get "#{@base}/JSON/core/action/shutdown/"
end
#xml report
#maybe it should be refactored to alert.
def xml_report
RestClient::get "#{@base}/OTHER/core/other/xmlreport/"
end
def html_report
RestClient::get "#{@base}/OTHER/core/other/htmlreport/"
end
end
end