rcs-common 9.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +49 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +1 -0
  5. data/Rakefile +27 -0
  6. data/lib/rcs-common.rb +21 -0
  7. data/lib/rcs-common/binary.rb +64 -0
  8. data/lib/rcs-common/cgi.rb +7 -0
  9. data/lib/rcs-common/component.rb +87 -0
  10. data/lib/rcs-common/crypt.rb +71 -0
  11. data/lib/rcs-common/deploy.rb +96 -0
  12. data/lib/rcs-common/diagnosticable.rb +136 -0
  13. data/lib/rcs-common/evidence.rb +261 -0
  14. data/lib/rcs-common/evidence/addressbook.rb +173 -0
  15. data/lib/rcs-common/evidence/application.rb +59 -0
  16. data/lib/rcs-common/evidence/calendar.rb +62 -0
  17. data/lib/rcs-common/evidence/call.rb +185 -0
  18. data/lib/rcs-common/evidence/camera.rb +25 -0
  19. data/lib/rcs-common/evidence/chat.rb +272 -0
  20. data/lib/rcs-common/evidence/clibpoard.rb +58 -0
  21. data/lib/rcs-common/evidence/command.rb +50 -0
  22. data/lib/rcs-common/evidence/common.rb +78 -0
  23. data/lib/rcs-common/evidence/content/camera/001.jpg +0 -0
  24. data/lib/rcs-common/evidence/content/coin/wallet_bit.dat +0 -0
  25. data/lib/rcs-common/evidence/content/coin/wallet_lite.dat +0 -0
  26. data/lib/rcs-common/evidence/content/file/Einstein.docx +0 -0
  27. data/lib/rcs-common/evidence/content/file/arabic.docx +0 -0
  28. data/lib/rcs-common/evidence/content/mouse/001.jpg +0 -0
  29. data/lib/rcs-common/evidence/content/mouse/002.jpg +0 -0
  30. data/lib/rcs-common/evidence/content/mouse/003.jpg +0 -0
  31. data/lib/rcs-common/evidence/content/mouse/004.jpg +0 -0
  32. data/lib/rcs-common/evidence/content/print/001.jpg +0 -0
  33. data/lib/rcs-common/evidence/content/screenshot/001.jpg +0 -0
  34. data/lib/rcs-common/evidence/content/screenshot/002.jpg +0 -0
  35. data/lib/rcs-common/evidence/content/screenshot/003.jpg +0 -0
  36. data/lib/rcs-common/evidence/content/url/001.jpg +0 -0
  37. data/lib/rcs-common/evidence/content/url/002.jpg +0 -0
  38. data/lib/rcs-common/evidence/content/url/003.jpg +0 -0
  39. data/lib/rcs-common/evidence/device.rb +23 -0
  40. data/lib/rcs-common/evidence/download.rb +54 -0
  41. data/lib/rcs-common/evidence/exec.rb +0 -0
  42. data/lib/rcs-common/evidence/file.rb +129 -0
  43. data/lib/rcs-common/evidence/filesystem.rb +71 -0
  44. data/lib/rcs-common/evidence/info.rb +24 -0
  45. data/lib/rcs-common/evidence/keylog.rb +84 -0
  46. data/lib/rcs-common/evidence/mail.rb +237 -0
  47. data/lib/rcs-common/evidence/mic.rb +39 -0
  48. data/lib/rcs-common/evidence/mms.rb +36 -0
  49. data/lib/rcs-common/evidence/money.rb +676 -0
  50. data/lib/rcs-common/evidence/mouse.rb +62 -0
  51. data/lib/rcs-common/evidence/password.rb +60 -0
  52. data/lib/rcs-common/evidence/photo.rb +80 -0
  53. data/lib/rcs-common/evidence/position.rb +303 -0
  54. data/lib/rcs-common/evidence/print.rb +50 -0
  55. data/lib/rcs-common/evidence/screenshot.rb +53 -0
  56. data/lib/rcs-common/evidence/sms.rb +91 -0
  57. data/lib/rcs-common/evidence/url.rb +133 -0
  58. data/lib/rcs-common/fixnum.rb +48 -0
  59. data/lib/rcs-common/gridfs.rb +294 -0
  60. data/lib/rcs-common/heartbeat.rb +96 -0
  61. data/lib/rcs-common/keywords.rb +50 -0
  62. data/lib/rcs-common/mime.rb +65 -0
  63. data/lib/rcs-common/mongoid.rb +19 -0
  64. data/lib/rcs-common/pascalize.rb +62 -0
  65. data/lib/rcs-common/path_utils.rb +67 -0
  66. data/lib/rcs-common/resolver.rb +40 -0
  67. data/lib/rcs-common/rest.rb +17 -0
  68. data/lib/rcs-common/sanitize.rb +42 -0
  69. data/lib/rcs-common/serializer.rb +404 -0
  70. data/lib/rcs-common/signature.rb +141 -0
  71. data/lib/rcs-common/stats.rb +94 -0
  72. data/lib/rcs-common/symbolize.rb +10 -0
  73. data/lib/rcs-common/systemstatus.rb +136 -0
  74. data/lib/rcs-common/temporary.rb +13 -0
  75. data/lib/rcs-common/time.rb +24 -0
  76. data/lib/rcs-common/trace.rb +138 -0
  77. data/lib/rcs-common/trace.yaml +42 -0
  78. data/lib/rcs-common/updater/client.rb +354 -0
  79. data/lib/rcs-common/updater/dsl.rb +178 -0
  80. data/lib/rcs-common/updater/payload.rb +79 -0
  81. data/lib/rcs-common/updater/server.rb +126 -0
  82. data/lib/rcs-common/updater/shared_key.rb +55 -0
  83. data/lib/rcs-common/updater/tmp_dir.rb +13 -0
  84. data/lib/rcs-common/utf16le.rb +83 -0
  85. data/lib/rcs-common/version.rb +5 -0
  86. data/lib/rcs-common/winfirewall.rb +235 -0
  87. data/rcs-common.gemspec +64 -0
  88. data/spec/gridfs_spec.rb +637 -0
  89. data/spec/mongoid.yaml +6 -0
  90. data/spec/signature_spec.rb +105 -0
  91. data/spec/spec_helper.rb +22 -0
  92. data/spec/updater_spec.rb +80 -0
  93. data/tasks/deploy.rake +21 -0
  94. data/tasks/protect.rake +90 -0
  95. data/test/helper.rb +17 -0
  96. data/test/test_binary.rb +107 -0
  97. data/test/test_cgi.rb +14 -0
  98. data/test/test_crypt.rb +125 -0
  99. data/test/test_evidence.rb +52 -0
  100. data/test/test_evidence_manager.rb +119 -0
  101. data/test/test_fixnum.rb +35 -0
  102. data/test/test_keywords.rb +137 -0
  103. data/test/test_mime.rb +49 -0
  104. data/test/test_pascalize.rb +100 -0
  105. data/test/test_path_utils.rb +24 -0
  106. data/test/test_rcs-common.rb +7 -0
  107. data/test/test_sanitize.rb +40 -0
  108. data/test/test_serialization.rb +20 -0
  109. data/test/test_stats.rb +90 -0
  110. data/test/test_symbolize.rb +20 -0
  111. data/test/test_systemstatus.rb +35 -0
  112. data/test/test_time.rb +56 -0
  113. data/test/test_trace.rb +25 -0
  114. data/test/test_utf16le.rb +71 -0
  115. data/test/test_winfirewall.rb +68 -0
  116. metadata +423 -0
@@ -0,0 +1,6 @@
1
+ spec:
2
+ sessions:
3
+ default:
4
+ database: rcs-test
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+ require 'rcs-common/signature'
3
+
4
+ class TestSignature
5
+ include Mongoid::Document
6
+ include RCS::Mongoid::Signature
7
+
8
+ field :name, type: String
9
+ field :surname, type: String
10
+ field :code, type: Integer
11
+ field :address, type: String
12
+ field :complex, type: Hash
13
+
14
+ sign_options :include => [:name, :surname, :code, :complex]
15
+ end
16
+
17
+ describe RCS::Mongoid::Signature do
18
+
19
+ describe '.included' do
20
+
21
+ let(:test) do
22
+ TestSignature.new
23
+ end
24
+
25
+ let(:fields) do
26
+ TestSignature.fields
27
+ end
28
+
29
+ before do
30
+ test.run_callbacks(:create)
31
+ test.run_callbacks(:save)
32
+ end
33
+
34
+ it "adds signature to the document" do
35
+ expect(fields["signature"]).to_not be_nil
36
+ end
37
+
38
+ end
39
+
40
+ context "when the document is created" do
41
+
42
+ let(:test) do
43
+ TestSignature.create(name: 'a', surname: 'b')
44
+ end
45
+
46
+ it "runs the created callbacks" do
47
+ expect(test.signature).to_not be_nil
48
+ end
49
+
50
+ it 'validates the signature' do
51
+ expect(test.check_signature).to be_truthy
52
+ end
53
+
54
+ end
55
+
56
+ context 'when the document is updated' do
57
+
58
+ let(:test) do
59
+ TestSignature.create(name: 'a', surname: 'b', code: 123, complex: {a:1, b:2})
60
+ end
61
+
62
+ it 'validates the signature after reload' do
63
+ test.reload
64
+ expect(test.check_signature).to be_truthy
65
+ end
66
+
67
+ it 'validates the signature after save' do
68
+ test.name = 'modified'
69
+ test.save
70
+ test.reload
71
+ expect(test.check_signature).to be_truthy
72
+ end
73
+
74
+ it 'validates the signature after update_attributes' do
75
+ test.update_attributes({surname: 'modified'})
76
+ test.reload
77
+ expect(test.check_signature).to be_truthy
78
+ end
79
+
80
+ end
81
+
82
+ context 'when the document is tampered' do
83
+
84
+ let(:test) do
85
+ TestSignature.create(name: 'a', surname: 'b', code: 123, complex: {a:1, b:2})
86
+ end
87
+
88
+ it 'validate the signature when changing not included field' do
89
+ test.address = 'c'
90
+ expect(test.check_signature).to be_truthy
91
+ end
92
+
93
+ it 'invalidate the signature when changing a signed field' do
94
+ test.name = 'mod'
95
+ expect(test.check_signature).to be_falsey
96
+ end
97
+
98
+ it 'invalidate the signature when changing the signature itself' do
99
+ test.signature = {}
100
+ expect(test.check_signature).to be_falsey
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,22 @@
1
+ require 'bundler'
2
+ require 'rspec'
3
+ require 'pry'
4
+
5
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
6
+
7
+ require 'rcs-common'
8
+ require 'rcs-common/mongoid'
9
+
10
+ RSpec.configure do |config|
11
+
12
+ config.color = true
13
+
14
+ config.before(:all) do
15
+ ENV['MONGOID_ENV'] = 'spec'
16
+ Mongoid.load! File.expand_path('../mongoid.yaml', __FILE__), :spec
17
+ end
18
+
19
+ config.before(:each) do
20
+ Mongoid.purge!
21
+ end
22
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+ require 'rcs-common/updater/client'
4
+ require 'rcs-common/updater/server'
5
+
6
+ module RCS::Updater
7
+
8
+ describe 'client' do
9
+ before do
10
+ allow_any_instance_of(Client).to receive(:trace).and_return(nil)
11
+ end
12
+
13
+ let(:signature) { '2433e2d6865e4e9a15ee57f74a196477' }
14
+
15
+ let(:signature2) { '2433e2d6865e4e9a15ee57f74a196400' }
16
+
17
+ let(:client) { Client.new("localhost") }
18
+
19
+ before do
20
+ @server_process_pid = fork do
21
+ allow_any_instance_of(SharedKey).to receive(:read_key_from_file).and_return(signature)
22
+ $stdout = StringIO.new
23
+ $stderr = $stdout
24
+ Server.start
25
+ end
26
+
27
+ # Wait for the server to bind
28
+ sleep(2)
29
+
30
+ allow(client).to receive(:localhost?).and_return(false)
31
+ end
32
+
33
+ after do
34
+ Process.kill(9, @server_process_pid)
35
+ end
36
+
37
+ context 'when shared key is valid' do
38
+ before do
39
+ allow_any_instance_of(SharedKey).to receive(:read_key_from_file).and_return(signature)
40
+ end
41
+
42
+ it 'communicates' do
43
+ expect(client.connected?).to be_truthy
44
+ end
45
+ end
46
+
47
+ context 'when shared key is not valid' do
48
+ before do
49
+ allow_any_instance_of(SharedKey).to receive(:read_key_from_file).and_return(signature2)
50
+ end
51
+
52
+ it 'does not get a reply' do
53
+ expect(client.connected?).to be_falsey
54
+ end
55
+ end
56
+
57
+ context 'when requesting to execute an invalid command' do
58
+ before do
59
+ allow_any_instance_of(SharedKey).to receive(:read_key_from_file).and_return(signature)
60
+ end
61
+
62
+ it 'raises an error' do
63
+ client.max_retries = 0
64
+ expect { client.request("xpas123Mnq1", exec: 1) }.to raise_error
65
+ end
66
+ end
67
+
68
+ context 'when requesting to execute the hostname command' do
69
+ before do
70
+ allow_any_instance_of(SharedKey).to receive(:read_key_from_file).and_return(signature)
71
+ end
72
+
73
+ it 'gets a valid response' do
74
+ resp = client.request("hostname", exec: 1)
75
+ expect(resp[:return_code]).to eq(0)
76
+ expect(resp[:output]).to eq(`hostname`)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,21 @@
1
+ require 'rcs-common/deploy'
2
+
3
+ desc "Deploy this project"
4
+ task :deploy do
5
+ user = ENV['DEPLOY_USER'] || 'Administrator'
6
+ address = ENV['DEPLOY_ADDRESS'] || '192.168.100.100'
7
+ deploy = RCS::Deploy.new(user: user, address: address)
8
+ $target = deploy.target
9
+ $me = deploy.me
10
+
11
+ if ENV['SKIP_CONFIRM'] != 'yes' and $me.pending_changes?
12
+ exit unless $me.ask('You have pending changes, continue?')
13
+ end
14
+
15
+ $me.run('rm -f pkg/*.gem')
16
+ $me.run('rake build')
17
+ $target.run("cd ./rcs-common && del *.gem")
18
+ $target.mirror!("pkg", "./rcs-common")
19
+ $target.run("cd ./rcs-common; \"C:/RCS/Ruby/bin/gem\" install --conservative rcs*.gem; \"C:/RCS/Ruby/bin/gem\" clean rcs-common")
20
+ $target.restart_service('RCSWorker')
21
+ end
@@ -0,0 +1,90 @@
1
+ require 'fileutils'
2
+
3
+ namespace :protect do
4
+
5
+ def verbose?
6
+ Rake.verbose == true
7
+ end
8
+
9
+ def report(message)
10
+ print message + '...'
11
+ STDOUT.flush
12
+ if block_given?
13
+ yield
14
+ end
15
+ puts ' ok'
16
+ end
17
+
18
+ def exec_rubyencoder(cmd)
19
+ if verbose?
20
+ system(cmd) || raise("Econding failed.")
21
+ else
22
+ raise("Econding failed.") if `#{cmd}` !~ /processed, 0 errors/
23
+ end
24
+ end
25
+
26
+ def windows?
27
+ RbConfig::CONFIG['host_os'] =~ /mingw/
28
+ end
29
+
30
+ if windows?
31
+ RUBYENCPATH = 'C:/Program Files (x86)/RubyEncoder'
32
+ RUBYENC = "\"C:\\Program Files (x86)\\RubyEncoder\\rgencoder.exe\""
33
+ else
34
+ paths = ['/Applications/Development/RubyEncoder.app/Contents/MacOS', '/Applications/RubyEncoder.app/Contents/MacOS']
35
+ RUBYENCPATH = File.exists?(paths.first) ? paths.first : paths.last
36
+ RUBYENC = "#{RUBYENCPATH}/rgencoder"
37
+ end
38
+
39
+ RUBYENC_VERSION = '2.0.0'
40
+
41
+ LIB_PATH = File.expand_path('../../lib', __FILE__)
42
+
43
+ raise("Invalid lib path") unless File.exists?("#{LIB_PATH}/rcs-common.rb")
44
+
45
+ desc "Build an encrypted version of rcs-common gem into the pkg directory"
46
+ task :build do
47
+ begin
48
+ FileUtils.cp_r(LIB_PATH, "#{LIB_PATH}_src")
49
+
50
+ # Encoding files
51
+ report("Encoding scripts (use --trace to see RubyEncoder output)") do
52
+ exec_rubyencoder("#{RUBYENC} --stop-on-error --encoding UTF-8 -b- -r --ruby #{RUBYENC_VERSION} \"#{LIB_PATH}/*.rb\"")
53
+ end
54
+
55
+
56
+ # Copy rgloader to lib folder
57
+
58
+ rgpath = "#{LIB_PATH}/rgloader"
59
+ FileUtils.rm_rf(rgpath)
60
+ FileUtils.mkdir(rgpath)
61
+
62
+ files = Dir["#{RUBYENCPATH}/Loaders/**/**"]
63
+ # keep only the interesting files (2.0.x windows, macos)
64
+ files.delete_if {|v| v.match(/bsd/i) or v.match(/linux/i)}
65
+ files.keep_if {|v| v.match(/#{RUBYENC_VERSION.gsub('.','')[0..1]}/) or v.match(/loader.rb/) }
66
+
67
+ files.each { |f| FileUtils.cp(f, rgpath) }
68
+
69
+
70
+ # Building the gem
71
+
72
+ export_protected = windows? ? "set PROTECTED=1 &&" : "export PROTECTED=1 ;"
73
+ system "#{export_protected} rake build"
74
+ ensure
75
+ # Restore the lib folder
76
+ if Dir.exists?("#{LIB_PATH}_src")
77
+ FileUtils.rm_rf(LIB_PATH) if Dir.exists?(LIB_PATH)
78
+ FileUtils.mv("#{LIB_PATH}_src", LIB_PATH)
79
+ end
80
+ end
81
+ end
82
+
83
+ desc "Build and install an encrypted version of rcs-common into system gems"
84
+ task :install do
85
+ FileUtils.rm_rf("#{LIB_PATH}/../pkg")
86
+ Rake::Task['protect:build'].invoke
87
+ gemfile = Dir["#{LIB_PATH}/../pkg/*.gem"].first
88
+ system("gem install --conservative #{gemfile}")
89
+ end
90
+ end
@@ -0,0 +1,17 @@
1
+ require 'bundler'
2
+
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'rcs-common'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,107 @@
1
+ require "test/unit"
2
+ require 'securerandom'
3
+
4
+ class BinaryPatchTest < Test::Unit::TestCase
5
+
6
+ def test_string
7
+ input = "string to be modified"
8
+ output = "string modified"
9
+
10
+ input.binary_patch "to be ", ""
11
+
12
+ assert_equal output, input
13
+ end
14
+
15
+ def test_binary
16
+ input = SecureRandom.random_bytes(16)
17
+ search = input.slice(0..3)
18
+ output = "1234" + input[4..-1]
19
+
20
+ input.binary_patch search, "1234"
21
+
22
+ assert_equal output, input
23
+ end
24
+
25
+ def test_binary_with_zero
26
+ input = "this\x00and\x00that"
27
+ search = "and"
28
+ replace = ",\x00this\x00,"
29
+ output = "this\x00,\x00this\x00,\x00that"
30
+
31
+ input.binary_patch search, replace
32
+
33
+ assert_equal output, input
34
+ end
35
+
36
+ def test_binary_with_regex
37
+ input = SecureRandom.random_bytes(16)
38
+ search = input.slice(0..3)
39
+ output = '\&$1' + input[4..-1]
40
+
41
+ input.binary_patch search, '\&$1'
42
+
43
+ assert_equal output, input
44
+ end
45
+
46
+ def test_not_found
47
+ input = "ciao"
48
+
49
+ assert_raise MatchNotFound do
50
+ input.binary_patch "miao", "bau"
51
+ end
52
+ end
53
+
54
+ def test_with_offset
55
+ input = "ciao miao bau"
56
+ offset = 5
57
+ string = "test"
58
+ output = "ciao test bau"
59
+
60
+ input.binary_patch_at_offset offset, string
61
+
62
+ assert_equal output, input
63
+ end
64
+
65
+ def test_with_offset_out_of_bound
66
+ input = "ciao bau"
67
+ offset = 15
68
+ string = "test"
69
+
70
+ assert_raise OutOfBounds do
71
+ input.binary_patch_at_offset offset, string
72
+ end
73
+ end
74
+
75
+ def test_with_offset_too_long
76
+ input = "ciao bau"
77
+ offset = 5
78
+ string = "test"
79
+
80
+ assert_raise OutOfBoundsString do
81
+ input.binary_patch_at_offset offset, string
82
+ end
83
+ end
84
+
85
+ def test_add_at_offset
86
+ input = "\x00\x00\x00\x00ciao miao bau"
87
+ offset = 0
88
+ value = 16
89
+ output = "\x10\x00\x00\x00ciao miao bau"
90
+
91
+ input.binary_add_at_offset offset, value
92
+
93
+ assert_equal output, input
94
+ end
95
+
96
+ def test_add_at_offset_not_zero
97
+ input = "ciao \x10\x00\x00\x00 miao bau"
98
+ offset = 5
99
+ value = 16
100
+ output = "ciao \x20\x00\x00\x00 miao bau"
101
+
102
+ input.binary_add_at_offset offset, value
103
+
104
+ assert_equal output, input
105
+ end
106
+
107
+ end