ssh_scan_api 0.0.1.pre2 → 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.
- checksums.yaml +5 -5
- data/.gitignore +7 -2
- data/.travis.yml +57 -10
- data/Gemfile +1 -2
- data/README.md +14 -16
- data/Rakefile +7 -1
- data/bin/ssh_scan_api +2 -12
- data/lib/ssh_scan_api.rb +6 -7
- data/lib/ssh_scan_api/api.rb +251 -186
- data/lib/ssh_scan_api/authenticator.rb +25 -19
- data/lib/ssh_scan_api/constants.rb +58 -0
- data/lib/ssh_scan_api/models/scan.rb +7 -0
- data/lib/ssh_scan_api/target_validator.rb +50 -0
- data/lib/ssh_scan_api/version.rb +3 -1
- data/ssh_scan_api.gemspec +8 -8
- metadata +38 -26
- data/lib/ssh_scan_api/database.rb +0 -61
- data/lib/ssh_scan_api/database/mongo.rb +0 -67
- data/lib/ssh_scan_api/database/sqlite.rb +0 -91
- data/lib/ssh_scan_api/job_queue.rb +0 -24
- data/lib/ssh_scan_api/stats.rb +0 -52
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'mongo'
|
2
|
-
|
3
|
-
Mongo::Logger.logger.level = ::Logger::FATAL
|
4
|
-
|
5
|
-
module SSHScan
|
6
|
-
module DB
|
7
|
-
class MongoDb
|
8
|
-
attr_reader :collection
|
9
|
-
|
10
|
-
def initialize(client)
|
11
|
-
@client = client
|
12
|
-
@collection = @client[:ssh_scan]
|
13
|
-
end
|
14
|
-
|
15
|
-
# Helps us create a SSHScan::DB::MongoDB object with a hash
|
16
|
-
def self.from_hash(opts)
|
17
|
-
name = opts["name"]
|
18
|
-
server = opts["server"]
|
19
|
-
port = opts["port"]
|
20
|
-
socket = server + ":" + port.to_s
|
21
|
-
|
22
|
-
client = Mongo::Client.new([socket], :database => name)
|
23
|
-
return SSHScan::DB::MongoDb.new(client)
|
24
|
-
end
|
25
|
-
|
26
|
-
# @param [String] worker_id
|
27
|
-
# @param [String] uuid
|
28
|
-
# @param [Hash] result
|
29
|
-
def add_scan(worker_id, uuid, result, socket)
|
30
|
-
@collection.insert_one("uuid" => uuid,
|
31
|
-
"target" => socket[:target],
|
32
|
-
"port" => socket[:port],
|
33
|
-
"scan" => result,
|
34
|
-
"worker_id" => worker_id)
|
35
|
-
end
|
36
|
-
|
37
|
-
def delete_scan(uuid)
|
38
|
-
@collection.delete_one(:uuid => uuid)
|
39
|
-
end
|
40
|
-
|
41
|
-
def delete_all
|
42
|
-
@collection.delete_many({})
|
43
|
-
end
|
44
|
-
|
45
|
-
# LEFT OFF HERE: the results of this method should be the exact same format as with SQLite
|
46
|
-
def find_scan_result(uuid)
|
47
|
-
@collection.find(:uuid => uuid).each do |doc|
|
48
|
-
return doc[:scan].to_hash
|
49
|
-
end
|
50
|
-
|
51
|
-
return nil
|
52
|
-
end
|
53
|
-
|
54
|
-
def fetch_cached_result(socket)
|
55
|
-
results = @collection.find(:target => socket[:target], :port => socket[:port])
|
56
|
-
results = results.skip(results.count() - 1)
|
57
|
-
return nil if results.count.zero?
|
58
|
-
result = {}
|
59
|
-
results.each do |result|
|
60
|
-
result[:uuid] = result[:uuid]
|
61
|
-
result[:start_time] = result[:scan][:start_time]
|
62
|
-
return result
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'sqlite3'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module SSHScan
|
5
|
-
module DB
|
6
|
-
class SQLite
|
7
|
-
attr_reader :database
|
8
|
-
|
9
|
-
def initialize(database)
|
10
|
-
@database = database # the SQLite database object
|
11
|
-
end
|
12
|
-
|
13
|
-
# Helps us create a SSHScan::DB::SQLite object with a hash
|
14
|
-
def self.from_hash(opts)
|
15
|
-
file_name = opts["file"]
|
16
|
-
|
17
|
-
if File.exist?(file_name)
|
18
|
-
db = ::SQLite3::Database.open(file_name)
|
19
|
-
else
|
20
|
-
db = ::SQLite3::Database.new(file_name)
|
21
|
-
end
|
22
|
-
|
23
|
-
#Check to see if the schema is setup or not
|
24
|
-
result = db.execute <<-SQL
|
25
|
-
SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'ssh_scan';
|
26
|
-
SQL
|
27
|
-
|
28
|
-
# If not, create it
|
29
|
-
if result == [[0]]
|
30
|
-
# Create the schema for the database
|
31
|
-
db.execute <<-SQL
|
32
|
-
create table ssh_scan (
|
33
|
-
uuid varchar(100),
|
34
|
-
target varchar(100),
|
35
|
-
port varchar(100),
|
36
|
-
result json,
|
37
|
-
worker_id varchar(100)
|
38
|
-
);
|
39
|
-
SQL
|
40
|
-
end
|
41
|
-
|
42
|
-
return SSHScan::DB::SQLite.new(db)
|
43
|
-
end
|
44
|
-
|
45
|
-
def size
|
46
|
-
count = @database.execute("select count() from ssh_scan")
|
47
|
-
return count
|
48
|
-
end
|
49
|
-
|
50
|
-
def add_scan(worker_id, uuid, result, socket)
|
51
|
-
@database.execute "insert into ssh_scan values ( ? , ? , ? , ? , ? )",
|
52
|
-
[uuid, socket[:target], socket[:port],
|
53
|
-
result.to_json, worker_id]
|
54
|
-
end
|
55
|
-
|
56
|
-
def delete_scan(uuid)
|
57
|
-
@database.execute(
|
58
|
-
"delete from ssh_scan where uuid = ?",
|
59
|
-
uuid
|
60
|
-
)
|
61
|
-
end
|
62
|
-
|
63
|
-
def delete_all
|
64
|
-
@database.execute("delete from ssh_scan")
|
65
|
-
end
|
66
|
-
|
67
|
-
def find_scan_result(uuid)
|
68
|
-
@database.execute(
|
69
|
-
"select * from ssh_scan where uuid like ( ? )",
|
70
|
-
uuid
|
71
|
-
) do |row|
|
72
|
-
return JSON.parse(row[3])
|
73
|
-
end
|
74
|
-
return nil
|
75
|
-
end
|
76
|
-
|
77
|
-
def fetch_cached_result(socket)
|
78
|
-
result = {}
|
79
|
-
results = @database.execute(
|
80
|
-
"select uuid, result from ssh_scan
|
81
|
-
where target like ( ? ) and port like ( ? )",
|
82
|
-
[socket[:target], socket[:port]]
|
83
|
-
)
|
84
|
-
return nil if results == []
|
85
|
-
result[:uuid] = results[result.length()-1][0]
|
86
|
-
result[:start_time] = JSON.parse(results[result.length()-1][1])["start_time"]
|
87
|
-
return result
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module SSHScan
|
2
|
-
class JobQueue
|
3
|
-
def initialize
|
4
|
-
@queue = Queue.new
|
5
|
-
end
|
6
|
-
|
7
|
-
# @param [String] a socket we want to scan (Example: "192.168.1.1:22")
|
8
|
-
# @return [nil]
|
9
|
-
def add(socket)
|
10
|
-
@queue.push(socket)
|
11
|
-
end
|
12
|
-
|
13
|
-
# @return [String] a socket we want to scan (Example: "192.168.1.1:22")
|
14
|
-
def next
|
15
|
-
return nil if @queue.empty?
|
16
|
-
@queue.pop
|
17
|
-
end
|
18
|
-
|
19
|
-
# @return [FixNum] the number of jobs in the JobQueue
|
20
|
-
def size
|
21
|
-
@queue.size
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/ssh_scan_api/stats.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
module SSHScan
|
2
|
-
class Stats
|
3
|
-
def initialize
|
4
|
-
@requests = []
|
5
|
-
end
|
6
|
-
|
7
|
-
def new_scan_request
|
8
|
-
@requests << Time.now
|
9
|
-
# Purges the request queue of old requests
|
10
|
-
purge_old_requests
|
11
|
-
end
|
12
|
-
|
13
|
-
def get_stats(queue_size)
|
14
|
-
{
|
15
|
-
:items_queued => queue_size,
|
16
|
-
:avg_requests_per_min => requests_avg_per,
|
17
|
-
:requests_per_min => requests_per
|
18
|
-
}.to_json
|
19
|
-
end
|
20
|
-
|
21
|
-
def size
|
22
|
-
@requests.size
|
23
|
-
end
|
24
|
-
|
25
|
-
# Purges the request queue of old requests, so we don't run the API out of memory
|
26
|
-
# @param [Fixnum] seconds
|
27
|
-
def purge_old_requests(seconds = 60)
|
28
|
-
@requests.delete_if {|request_time| request_time < Time.now - seconds}
|
29
|
-
end
|
30
|
-
|
31
|
-
# Determines the number of requests in a second-based
|
32
|
-
# time period (up to 60 seconds)
|
33
|
-
# @param [Fixnum] seconds
|
34
|
-
# @return [Fixnum] request per time period
|
35
|
-
def requests_per(seconds = 60)
|
36
|
-
requests_per = 0
|
37
|
-
past_time = Time.now - seconds
|
38
|
-
|
39
|
-
@requests.each do |request_time|
|
40
|
-
requests_per += 1 if request_time >= past_time
|
41
|
-
end
|
42
|
-
|
43
|
-
return requests_per
|
44
|
-
end
|
45
|
-
|
46
|
-
# Determines average requests per time period
|
47
|
-
def requests_avg_per(seconds = 60)
|
48
|
-
requests_per = requests_per(seconds)
|
49
|
-
requests_per / seconds.to_f
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|