security_guard 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.
- data/.gitignore +18 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +57 -0
- data/Rakefile +11 -0
- data/bin/sguard +40 -0
- data/data/GeoIP.dat +0 -0
- data/lib/security_guard.rb +5 -0
- data/lib/security_guard/country_ips.rb +20 -0
- data/lib/security_guard/utils/accepts_from_file.rb +20 -0
- data/lib/security_guard/utils/files.rb +13 -0
- data/lib/security_guard/utils/geo_ips.rb +22 -0
- data/lib/security_guard/version.rb +3 -0
- data/security_guard.gemspec +22 -0
- data/specs/country_ips_spec.rb +17 -0
- data/specs/fixtures/countries.txt +3 -0
- data/specs/fixtures/ip_addresses.txt +3 -0
- data/specs/spec_helper.rb +15 -0
- data/specs/utils/accepts_from_file_spec.rb +22 -0
- data/specs/utils/files_spec.rb +15 -0
- data/specs/utils/geo_ips_spec.rb +45 -0
- metadata +112 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Fred Wu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# SecurityGuard
|
2
|
+
|
3
|
+
This gem is a collection of useful tools for auditing data and performing security checks.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'security_guard'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install security_guard
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Executable
|
22
|
+
|
23
|
+
There is an `sguard` command if you intend to use security_guard as a command line tool. Please refer to the help option for its usage.
|
24
|
+
|
25
|
+
sguard -h
|
26
|
+
|
27
|
+
### Country IPs
|
28
|
+
|
29
|
+
Returns a list of the IPs from given country and IP dictionaries. Useful for auditing IPs from higher risk nations.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
country_ips = SecurityGuard::CountryIps.new
|
33
|
+
country_ips.countries = ['Australia', 'United States']
|
34
|
+
country_ips.ips = ['4.4.4.4', '8.8.8.8', '203.206.0.1']
|
35
|
+
country_ips.result # => ['203.206.0.1']
|
36
|
+
```
|
37
|
+
|
38
|
+
You may also pass country and IP data as a line-delimited file by appending `_from_file` at the end of the attributes:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
country_ips.countries_from_file = '/path/to/the/file'
|
42
|
+
country_ips.ips_from_file = '/path/to/the/file'
|
43
|
+
```
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Make sure you add documentation to README.md
|
49
|
+
3. Make sure you test all your code
|
50
|
+
4. Do your magic!
|
51
|
+
5. Create a new Pull Request
|
52
|
+
|
53
|
+
## Author
|
54
|
+
|
55
|
+
- [Fred Wu](http://fredwu.me/)
|
56
|
+
|
57
|
+
Brought to you by [SitePoint](http://www.sitepoint.com/).
|
data/Rakefile
ADDED
data/bin/sguard
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'clamp'
|
4
|
+
require 'awesome_print'
|
5
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
6
|
+
require 'security_guard'
|
7
|
+
|
8
|
+
module SecurityGuard
|
9
|
+
class CountryIpsCommand < Clamp::Command
|
10
|
+
parameter 'ip_addresses_path', 'path to a line-delimited file of IP addresses'
|
11
|
+
parameter 'countries_path', 'path to a line-delimited file of countries'
|
12
|
+
parameter '[output_path]', 'output the results in a text file'
|
13
|
+
|
14
|
+
def execute
|
15
|
+
country_ips = SecurityGuard::CountryIps.new
|
16
|
+
|
17
|
+
country_ips.ips_from_file = ip_addresses_path
|
18
|
+
country_ips.countries_from_file = countries_path
|
19
|
+
|
20
|
+
if output_path
|
21
|
+
MainCommand.output_result(country_ips.result, output_path)
|
22
|
+
else
|
23
|
+
ap country_ips.result
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class MainCommand < Clamp::Command
|
29
|
+
def self.output_result(result, output_path)
|
30
|
+
`echo #{result} > #{output_path}`
|
31
|
+
ap "Results published to '#{output_path}'."
|
32
|
+
end
|
33
|
+
|
34
|
+
subcommand 'country_ips',
|
35
|
+
'Returns a list of the IPs from given country and IP dictionaries.',
|
36
|
+
SecurityGuard::CountryIpsCommand
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
SecurityGuard::MainCommand.run
|
data/data/GeoIP.dat
ADDED
Binary file
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SecurityGuard
|
2
|
+
class CountryIps
|
3
|
+
include Utils::AcceptsFromFile
|
4
|
+
|
5
|
+
accepts_from_file :countries, :ips
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@geoip ||= Utils::GeoIps.new(Utils::Files.load('GeoIP.dat'))
|
9
|
+
end
|
10
|
+
|
11
|
+
def result
|
12
|
+
country_ips = []
|
13
|
+
ips.each do |ip|
|
14
|
+
@geoip.ip_address = ip
|
15
|
+
country_ips << ip if countries.include?(@geoip.country_name)
|
16
|
+
end
|
17
|
+
country_ips.sort
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SecurityGuard
|
2
|
+
module Utils
|
3
|
+
module AcceptsFromFile
|
4
|
+
def self.included(klass)
|
5
|
+
klass.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def accepts_from_file(*attributes)
|
10
|
+
attributes.each do |attribute|
|
11
|
+
self.send :attr_accessor, attribute
|
12
|
+
self.send :define_method, "#{attribute}_from_file=", lambda { |file|
|
13
|
+
self.send "#{attribute}=", SecurityGuard::Utils::Files.to_array(file)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SecurityGuard
|
2
|
+
module Utils
|
3
|
+
class Files
|
4
|
+
def self.load(filename)
|
5
|
+
File.expand_path("../../../../data/#{filename}", __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.to_array(line_delimited_file)
|
9
|
+
File.readlines(line_delimited_file).map{ |line| line.sub("\n", '') }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'geoip'
|
2
|
+
|
3
|
+
module SecurityGuard
|
4
|
+
module Utils
|
5
|
+
class GeoIps
|
6
|
+
attr_accessor :ip_address
|
7
|
+
|
8
|
+
def initialize(geo_data_file, ip_address = nil)
|
9
|
+
@geoip ||= GeoIP.new(geo_data_file)
|
10
|
+
self.ip_address = ip_address
|
11
|
+
end
|
12
|
+
|
13
|
+
def country
|
14
|
+
@geoip.country(ip_address)
|
15
|
+
end
|
16
|
+
|
17
|
+
def country_name
|
18
|
+
country.country_name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/security_guard/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Fred Wu']
|
6
|
+
gem.email = ['fred.wu@sitepoint.com']
|
7
|
+
gem.summary = %q{A collection of useful tools for auditing data and performing security checks.}
|
8
|
+
gem.description = gem.summary
|
9
|
+
gem.homepage = 'https://github.com/sitepoint/security_guard'
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = 'security_guard'
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = SecurityGuard::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'clamp'
|
19
|
+
gem.add_dependency 'awesome_print'
|
20
|
+
gem.add_dependency 'geoip'
|
21
|
+
gem.add_development_dependency 'simplecov'
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe SecurityGuard::CountryIps do
|
4
|
+
before do
|
5
|
+
@country_ips = SecurityGuard::CountryIps.new
|
6
|
+
@country_ips.countries = ['Australia']
|
7
|
+
@country_ips.ips = ['4.4.4.4', '8.8.8.8', '203.206.0.1']
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'contains GeoIP data' do
|
11
|
+
@country_ips.instance_variable_get(:@geoip).must_be_instance_of SecurityGuard::Utils::GeoIps
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns all IPs from the given country and IP dictionaries' do
|
15
|
+
@country_ips.result.must_equal ['203.206.0.1']
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/spec'
|
6
|
+
|
7
|
+
require File.expand_path('../../lib/security_guard', __FILE__)
|
8
|
+
|
9
|
+
def data_file(filename)
|
10
|
+
SecurityGuard::Utils::Files.load(filename)
|
11
|
+
end
|
12
|
+
|
13
|
+
def fixture_file(filename)
|
14
|
+
File.expand_path("../fixtures/#{filename}", __FILE__)
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
class TestA
|
4
|
+
include SecurityGuard::Utils::AcceptsFromFile
|
5
|
+
accepts_from_file :test_attribute
|
6
|
+
end
|
7
|
+
|
8
|
+
describe SecurityGuard::Utils::AcceptsFromFile do
|
9
|
+
before do
|
10
|
+
@test = TestA.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has #test_attribute as an accessor' do
|
14
|
+
@test.test_attribute = 'test'
|
15
|
+
@test.test_attribute.must_equal 'test'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'converts data from a file into an array' do
|
19
|
+
@test.test_attribute_from_file = fixture_file('countries.txt')
|
20
|
+
@test.test_attribute.must_equal ['Australia', 'United States', 'United Kingdom']
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe SecurityGuard::Utils::Files do
|
4
|
+
describe 'SecurityGuard::Utils::Files#load' do
|
5
|
+
it 'points to the correct file' do
|
6
|
+
SecurityGuard::Utils::Files.load('GeoIP.dat').must_match 'security_guard/data/GeoIP.dat'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'SecurityGuard::Utils::Files#to_array' do
|
11
|
+
it 'returns an array of the line delimited data' do
|
12
|
+
SecurityGuard::Utils::Files.to_array(fixture_file('countries.txt')).must_equal ['Australia', 'United States', 'United Kingdom']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe SecurityGuard::Utils::GeoIps do
|
4
|
+
describe 'initialise without an ip' do
|
5
|
+
before do
|
6
|
+
@geoip = SecurityGuard::Utils::GeoIps.new(data_file('GeoIP.dat'))
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'initialises with a geo ip database' do
|
10
|
+
@geoip.must_be_instance_of SecurityGuard::Utils::GeoIps
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'sets an ip address' do
|
14
|
+
@geoip.ip_address = '8.8.8.8'
|
15
|
+
@geoip.ip_address.must_equal '8.8.8.8'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'initialise with an ip' do
|
20
|
+
before do
|
21
|
+
@geoip = SecurityGuard::Utils::GeoIps.new(data_file('GeoIP.dat'), '8.8.8.8')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'has an ip address' do
|
25
|
+
@geoip.ip_address.must_equal '8.8.8.8'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'detects the country' do
|
29
|
+
@geoip.country.must_be_kind_of Struct
|
30
|
+
@geoip.country.to_hash.must_equal({
|
31
|
+
:request => '8.8.8.8',
|
32
|
+
:ip => '8.8.8.8',
|
33
|
+
:country_code => 225,
|
34
|
+
:country_code2 => 'US',
|
35
|
+
:country_code3 => 'USA',
|
36
|
+
:country_name => 'United States',
|
37
|
+
:continent_code => 'NA'
|
38
|
+
})
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'detects the country name' do
|
42
|
+
@geoip.country_name.must_equal 'United States'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: security_guard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fred Wu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: clamp
|
16
|
+
requirement: &70177434013380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70177434013380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: awesome_print
|
27
|
+
requirement: &70177434012840 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70177434012840
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: geoip
|
38
|
+
requirement: &70177434012420 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70177434012420
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: simplecov
|
49
|
+
requirement: &70177434011980 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70177434011980
|
58
|
+
description: A collection of useful tools for auditing data and performing security
|
59
|
+
checks.
|
60
|
+
email:
|
61
|
+
- fred.wu@sitepoint.com
|
62
|
+
executables:
|
63
|
+
- sguard
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- .gitignore
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/sguard
|
73
|
+
- data/GeoIP.dat
|
74
|
+
- lib/security_guard.rb
|
75
|
+
- lib/security_guard/country_ips.rb
|
76
|
+
- lib/security_guard/utils/accepts_from_file.rb
|
77
|
+
- lib/security_guard/utils/files.rb
|
78
|
+
- lib/security_guard/utils/geo_ips.rb
|
79
|
+
- lib/security_guard/version.rb
|
80
|
+
- security_guard.gemspec
|
81
|
+
- specs/country_ips_spec.rb
|
82
|
+
- specs/fixtures/countries.txt
|
83
|
+
- specs/fixtures/ip_addresses.txt
|
84
|
+
- specs/spec_helper.rb
|
85
|
+
- specs/utils/accepts_from_file_spec.rb
|
86
|
+
- specs/utils/files_spec.rb
|
87
|
+
- specs/utils/geo_ips_spec.rb
|
88
|
+
homepage: https://github.com/sitepoint/security_guard
|
89
|
+
licenses: []
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.8.10
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: A collection of useful tools for auditing data and performing security checks.
|
112
|
+
test_files: []
|