Class: NessusAPI::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/nessus_api/session.rb

Constant Summary collapse

@@current =

Keep that in mind when I start extending the class.

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = ENV['NESSUS_HOST'], user = ENV['NESSUS_USER'], pw = ENV['NESSUS_PASS'], port = ENV['NESSUS_PORT']) ⇒ Session

Returns a new instance of Session.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nessus_api/session.rb', line 16

def initialize(host=ENV['NESSUS_HOST'], user=ENV['NESSUS_USER'],
               pw=ENV['NESSUS_PASS'], port=ENV['NESSUS_PORT'])
  # Attempts to connect with the given instance
  # of Nessus. Returns errors when it cannot reach
  # an installation, or if there are bad credentials
  # given. Returns a token otherwise.
  @host = host
  @port = port
  @token = self.get('login', {'login' => user, 'password' => pw},
               nil).css("token").text
  @@current = self
end

Class Method Details

.currentObject



69
70
71
# File 'lib/nessus_api/session.rb', line 69

def self.current
  @@current
end

Instance Method Details

#closeObject



59
60
61
62
63
64
65
66
67
# File 'lib/nessus_api/session.rb', line 59

def close
  # Logs out of Nessus installation
  # Returns a true, if it works.
  if self.get('logout').css('contents').text == 'OK'
    return true
  else
    return false
  end
end

#currentObject



73
74
75
# File 'lib/nessus_api/session.rb', line 73

def current
  @@current
end

#get(path, args = {}, token = @token) ⇒ Object



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
# File 'lib/nessus_api/session.rb', line 29

def get(path, args={}, token=@token)
  # Performs an API call using the path and arguments given.
  # Returns a token if there is not already a token.
  # Otherwise, it returns the response from the server.
  args['token'] = @token
  args['seq'] = Random.new.rand(9999).to_s
  url = URI('https://' + @host + ':' + @port + '/' + path)
  request = Net::HTTP::Post.new(url.path)
  request.set_form_data(args)
  conn = Net::HTTP.new(url.host, url.port)
  conn.use_ssl = true
  conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
  begin
    response = conn.request(request)
    if response.is_a?(Net::HTTPSuccess)
      response_xml = Nokogiri::XML(response.body)
      if response_xml.at_css("seq").text != args['seq']
        raise StandardError, "Secret token did not match!"
      elsif response_xml.at_css("status").text != 'OK'
        raise AuthenticationError, "Credentials are not valid!"
      end
      return response_xml
    else
      raise ConnectionError, "Could not connect properly!"
    end
  rescue => e
    raise e
  end
end

#policiesObject



81
82
83
84
85
86
87
88
# File 'lib/nessus_api/session.rb', line 81

def policies
  results = []
  @doc = get('policy/list', {})
    (0..@doc.css("policies policyName").length-1).each do |i|
      results << [@doc.css("policies policyName")[i].text, @doc.css("policies policyID")[i].text]
    end
  return results
end

#scanListObject



77
78
79
# File 'lib/nessus_api/session.rb', line 77

def scanList
  get('scan/list', {}).at_css('scanList')
end