Class: Nessus::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nessus/xmlrpc.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, username = nil, password = nil, ssl_option = nil) {|@connection| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (@connection)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nessus/xmlrpc.rb', line 17

def initialize(host, username = nil, password = nil, ssl_option = nil)
  uri = URI.parse(host)
  @connection = Net::HTTP.new(uri.host, uri.port)
  @connection.use_ssl = true
  if ssl_option == "ssl_verify"
    @connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
  else
    @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
    
  yield @connection if block_given?
    authenticate(username, password) if username && password
end

Instance Method Details

#authenticate(username, password) ⇒ Object Also known as: login



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nessus/xmlrpc.rb', line 31

def authenticate(username, password)
  payload = {
    :username => username, 
    :password => password, 
    :json => 1
  }
  res = http_post(:uri=>"/session", :data=>payload)
  if res['token']
    @token = "token=#{res['token']}"
    return true
  else
    false
  end
end

#authenticatedObject



52
53
54
55
56
57
58
# File 'lib/nessus/xmlrpc.rb', line 52

def authenticated
  if (@token && @token.include?('token='))
    return true
  else
    return false
  end
end

#get_server_propertiesObject



60
61
62
# File 'lib/nessus/xmlrpc.rb', line 60

def get_server_properties
  http_get(:uri=>"/server/properties", :fields=>x_cookie)
end

#host_detail(scan_id, host_id) ⇒ Object



204
205
206
# File 'lib/nessus/xmlrpc.rb', line 204

def host_detail(scan_id, host_id)
  res = http_get(:uri=>"/scans/#{scan_id}/hosts/#{host_id}", :fields=>x_cookie)
end

#is_adminObject



126
127
128
129
130
131
132
133
# File 'lib/nessus/xmlrpc.rb', line 126

def is_admin
  res = http_get(:uri=>"/session", :fields=>x_cookie)
  if res['permissions'] == 128
    return true
  else
    return false
  end
end

#list_familiesObject



110
111
112
# File 'lib/nessus/xmlrpc.rb', line 110

def list_families
  http_get(:uri=>"/plugins/families", :fields=>x_cookie)
end

#list_foldersObject



102
103
104
# File 'lib/nessus/xmlrpc.rb', line 102

def list_folders
  http_get(:uri=>"/folders", :fields=>x_cookie)
end

#list_plugins(family_id) ⇒ Object



114
115
116
# File 'lib/nessus/xmlrpc.rb', line 114

def list_plugins(family_id)
  http_get(:uri=>"/plugins/families/#{family_id}", :fields=>x_cookie)
end

#list_policiesObject



94
95
96
# File 'lib/nessus/xmlrpc.rb', line 94

def list_policies
  http_get(:uri=>"/policies", :fields=>x_cookie)
end

#list_scannersObject



106
107
108
# File 'lib/nessus/xmlrpc.rb', line 106

def list_scanners
  http_get(:uri=>"/scanners", :fields=>x_cookie)
end

#list_template(type) ⇒ Object



118
119
120
# File 'lib/nessus/xmlrpc.rb', line 118

def list_template(type)
  res = http_get(:uri=>"/editor/#{type}/templates", :fields=>x_cookie)
end

#list_usersObject



98
99
100
# File 'lib/nessus/xmlrpc.rb', line 98

def list_users
  http_get(:uri=>"/users", :fields=>x_cookie)
end

#plugin_details(plugin_id) ⇒ Object



122
123
124
# File 'lib/nessus/xmlrpc.rb', line 122

def plugin_details(plugin_id)
  http_get(:uri=>"/plugins/plugin/#{plugin_id}", :fields=>x_cookie)
end

#policy_delete(policy_id) ⇒ Object



199
200
201
202
# File 'lib/nessus/xmlrpc.rb', line 199

def policy_delete(policy_id)
  res = http_delete(:uri=>"/policies/#{policy_id}", :fields=>x_cookie)
  return res.code
end

#report_download(scan_id, file_id) ⇒ Object



208
209
210
# File 'lib/nessus/xmlrpc.rb', line 208

def report_download(scan_id, file_id)
  res = http_get(:uri=>"/scans/#{scan_id}/export/#{file_id}/download", :raw_content=> true, :fields=>x_cookie)
end

#scan_create(uuid, name, description, targets) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nessus/xmlrpc.rb', line 139

def scan_create(uuid, name, description, targets)
  payload = {
    :uuid => uuid, 
    :settings => {
      :name => name, 
      :description => description, 
      :text_targets => targets
      },
    :json => 1
  }.to_json
  http_post(:uri=>"/scans", :body=>payload, :fields=>x_cookie, :ctype=>'application/json')
end

#scan_details(scan_id) ⇒ Object



164
165
166
# File 'lib/nessus/xmlrpc.rb', line 164

def scan_details(scan_id)
  http_get(:uri=>"/scans/#{scan_id}", :fields=>x_cookie)
end

#scan_export(scan_id, format) ⇒ Object



180
181
182
183
184
185
# File 'lib/nessus/xmlrpc.rb', line 180

def scan_export(scan_id, format)
  payload = {
    :format => format
  }.to_json
  http_post(:uri=>"/scans/#{scan_id}/export", :body=>payload, :ctype=>'application/json', :fields=>x_cookie)
end

#scan_export_status(scan_id, file_id) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/nessus/xmlrpc.rb', line 187

def scan_export_status(scan_id, file_id)
  request = Net::HTTP::Get.new("/scans/#{scan_id}/export/#{file_id}/status")
  request.add_field("X-Cookie", @token)
  res = @connection.request(request)
  if res.code == "200"
    return "ready"
  else
    res = JSON.parse(res.body)
    return res
  end
end

#scan_launch(scan_id) ⇒ Object



152
153
154
# File 'lib/nessus/xmlrpc.rb', line 152

def scan_launch(scan_id)
  http_post(:uri=>"/scans/#{scan_id}/launch", :fields=>x_cookie)
end

#scan_listObject



160
161
162
# File 'lib/nessus/xmlrpc.rb', line 160

def scan_list
  http_get(:uri=>"/scans", :fields=>x_cookie)
end

#scan_pause(scan_id) ⇒ Object



168
169
170
# File 'lib/nessus/xmlrpc.rb', line 168

def scan_pause(scan_id)
  http_post(:uri=>"/scans/#{scan_id}/pause", :fields=>x_cookie)
end

#scan_resume(scan_id) ⇒ Object



172
173
174
# File 'lib/nessus/xmlrpc.rb', line 172

def scan_resume(scan_id)
  http_post(:uri=>"/scans/#{scan_id}/resume", :fields=>x_cookie)
end

#scan_stop(scan_id) ⇒ Object



176
177
178
# File 'lib/nessus/xmlrpc.rb', line 176

def scan_stop(scan_id)
  http_post(:uri=>"/scans/#{scan_id}/stop", :fields=>x_cookie)
end

#server_propertiesObject



135
136
137
# File 'lib/nessus/xmlrpc.rb', line 135

def server_properties
  http_get(:uri=>"/server/properties", :fields=>x_cookie)
end

#server_statusObject



156
157
158
# File 'lib/nessus/xmlrpc.rb', line 156

def server_status
  http_get(:uri=>"/server/status", :fields=>x_cookie)
end

#user_add(username, password, permissions, type) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/nessus/xmlrpc.rb', line 64

def user_add(username, password, permissions, type)
  payload = {
    :username => username, 
    :password => password, 
    :permissions => permissions, 
    :type => type, 
    :json => 1
  }
  http_post(:uri=>"/users", :fields=>x_cookie, :data=>payload)
end

#user_chpasswd(user_id, password) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/nessus/xmlrpc.rb', line 80

def user_chpasswd(user_id, password)
  payload = {
    :password => password, 
    :json => 1
  }
  res = http_put(:uri=>"/users/#{user_id}/chpasswd", :data=>payload, :fields=>x_cookie)
  return res.code
end

#user_delete(user_id) ⇒ Object



75
76
77
78
# File 'lib/nessus/xmlrpc.rb', line 75

def user_delete(user_id)
  res = http_delete(:uri=>"/users/#{user_id}", :fields=>x_cookie)
  return res.code
end

#user_logoutObject



89
90
91
92
# File 'lib/nessus/xmlrpc.rb', line 89

def user_logout
  res = http_delete(:uri=>"/session", :fields=>x_cookie)
  return res.code
end


46
47
48
# File 'lib/nessus/xmlrpc.rb', line 46

def x_cookie
  {'X-Cookie'=>@token}
end