tcell_agent 1.1.12 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +5 -5
  2. data/bin/tcell_agent +26 -14
  3. data/lib/tcell_agent.rb +16 -10
  4. data/lib/tcell_agent/agent.rb +78 -97
  5. data/lib/tcell_agent/agent/route_manager.rb +0 -16
  6. data/lib/tcell_agent/agent/static_agent.rb +9 -30
  7. data/lib/tcell_agent/authlogic.rb +3 -6
  8. data/lib/tcell_agent/config/unknown_options.rb +4 -8
  9. data/lib/tcell_agent/configuration.rb +38 -119
  10. data/lib/tcell_agent/devise.rb +25 -27
  11. data/lib/tcell_agent/hooks/login_fraud.rb +30 -33
  12. data/lib/tcell_agent/instrument_servers.rb +25 -0
  13. data/lib/tcell_agent/instrumentation.rb +12 -10
  14. data/lib/tcell_agent/instrumentation/cmdi.rb +19 -15
  15. data/lib/tcell_agent/instrumentation/lfi.rb +73 -0
  16. data/lib/tcell_agent/instrumentation/monkey_patches/file.rb +25 -0
  17. data/lib/tcell_agent/instrumentation/monkey_patches/io.rb +123 -0
  18. data/lib/tcell_agent/instrumentation/monkey_patches/kernel.rb +159 -0
  19. data/lib/tcell_agent/logger.rb +50 -114
  20. data/lib/tcell_agent/patches.rb +6 -7
  21. data/lib/tcell_agent/policies/appfirewall_policy.rb +26 -0
  22. data/lib/tcell_agent/policies/command_injection_policy.rb +28 -0
  23. data/lib/tcell_agent/policies/dataloss_policy.rb +44 -44
  24. data/lib/tcell_agent/policies/headers_policy.rb +25 -0
  25. data/lib/tcell_agent/policies/http_redirect_policy.rb +13 -79
  26. data/lib/tcell_agent/policies/js_agent_policy.rb +27 -0
  27. data/lib/tcell_agent/policies/local_file_access.rb +28 -0
  28. data/lib/tcell_agent/policies/login_policy.rb +43 -0
  29. data/lib/tcell_agent/policies/patches_policy.rb +27 -0
  30. data/lib/tcell_agent/policies/policies_manager.rb +68 -0
  31. data/lib/tcell_agent/policies/policy_polling.rb +58 -0
  32. data/lib/tcell_agent/policies/policy_types.rb +14 -0
  33. data/lib/tcell_agent/policies/system_enablements.rb +27 -0
  34. data/lib/tcell_agent/rails/auth/authlogic.rb +43 -68
  35. data/lib/tcell_agent/rails/auth/devise.rb +20 -23
  36. data/lib/tcell_agent/rails/auth/doorkeeper.rb +63 -74
  37. data/lib/tcell_agent/rails/csrf_exception.rb +2 -2
  38. data/lib/tcell_agent/rails/dlp.rb +25 -15
  39. data/lib/tcell_agent/rails/dlp_handler.rb +1 -2
  40. data/lib/tcell_agent/rails/js_agent_insert.rb +12 -13
  41. data/lib/tcell_agent/rails/middleware/body_filter_middleware.rb +4 -25
  42. data/lib/tcell_agent/rails/middleware/context_middleware.rb +2 -12
  43. data/lib/tcell_agent/rails/middleware/global_middleware.rb +0 -1
  44. data/lib/tcell_agent/rails/middleware/headers_middleware.rb +14 -34
  45. data/lib/tcell_agent/rails/on_start.rb +32 -31
  46. data/lib/tcell_agent/rails/routes.rb +7 -6
  47. data/lib/tcell_agent/rails/routes/grape.rb +1 -3
  48. data/lib/tcell_agent/rails/routes/route_id.rb +3 -1
  49. data/lib/tcell_agent/rails/settings_reporter.rb +23 -36
  50. data/lib/tcell_agent/rails/start_agent_after_initializers.rb +12 -0
  51. data/lib/tcell_agent/rails/tcell_body_proxy.rb +6 -4
  52. data/lib/tcell_agent/rust/agent_config.rb +49 -0
  53. data/lib/tcell_agent/rust/{libtcellagent-alpine-1.3.2.so → libtcellagent-4.14.0.dylib} +0 -0
  54. data/lib/tcell_agent/rust/libtcellagent-4.14.0.so +0 -0
  55. data/lib/tcell_agent/rust/{libtcellagent-1.3.2.so → libtcellagent-alpine-4.14.0.so} +0 -0
  56. data/lib/tcell_agent/rust/models.rb +0 -55
  57. data/lib/tcell_agent/rust/native_agent.rb +531 -0
  58. data/lib/tcell_agent/rust/native_agent_response.rb +42 -0
  59. data/lib/tcell_agent/rust/native_library.rb +68 -0
  60. data/lib/tcell_agent/rust/tcellagent-4.14.0.dll +0 -0
  61. data/lib/tcell_agent/sensor_events/agent_setting_event.rb +12 -0
  62. data/lib/tcell_agent/sensor_events/{app_config.rb → app_config_setting_event.rb} +0 -6
  63. data/lib/tcell_agent/sensor_events/dlp.rb +2 -6
  64. data/lib/tcell_agent/sensor_events/sensor.rb +0 -62
  65. data/lib/tcell_agent/sensor_events/server_agent.rb +13 -18
  66. data/lib/tcell_agent/sensor_events/util/sanitizer_utilities.rb +0 -108
  67. data/lib/tcell_agent/sensor_events/util/utils.rb +0 -2
  68. data/lib/tcell_agent/servers/passenger.rb +1 -28
  69. data/lib/tcell_agent/servers/puma.rb +3 -21
  70. data/lib/tcell_agent/servers/rails_server.rb +1 -1
  71. data/lib/tcell_agent/servers/thin.rb +2 -2
  72. data/lib/tcell_agent/servers/unicorn.rb +19 -80
  73. data/lib/tcell_agent/servers/webrick.rb +1 -1
  74. data/lib/tcell_agent/settings_reporter.rb +24 -24
  75. data/lib/tcell_agent/sinatra.rb +14 -16
  76. data/lib/tcell_agent/tcell_context.rb +40 -14
  77. data/lib/tcell_agent/utils/headers.rb +14 -0
  78. data/lib/tcell_agent/version.rb +1 -1
  79. data/spec/lib/tcell_agent/cmdi_spec.rb +0 -585
  80. data/spec/lib/tcell_agent/config/unknown_options_spec.rb +0 -18
  81. data/spec/lib/tcell_agent/configuration_spec.rb +4 -140
  82. data/spec/lib/tcell_agent/hooks/login_fraud_spec.rb +46 -173
  83. data/spec/lib/tcell_agent/instrumentation/cmdi/io_cmdi_spec.rb +504 -0
  84. data/spec/lib/tcell_agent/instrumentation/cmdi/kernel_cmdi_spec.rb +435 -0
  85. data/spec/lib/tcell_agent/instrumentation/lfi/file_lfi_spec.rb +326 -0
  86. data/spec/lib/tcell_agent/instrumentation/lfi/io_lfi_spec.rb +556 -0
  87. data/spec/lib/tcell_agent/instrumentation/lfi/kernel_lfi_spec.rb +249 -0
  88. data/spec/lib/tcell_agent/instrumentation/lfi_spec.rb +105 -0
  89. data/spec/lib/tcell_agent/patches_spec.rb +25 -43
  90. data/spec/lib/tcell_agent/policies/appfirewall_policy_spec.rb +183 -0
  91. data/spec/lib/tcell_agent/policies/clickjacking_policy_spec.rb +57 -0
  92. data/spec/lib/tcell_agent/policies/command_injection_policy_spec.rb +84 -773
  93. data/spec/lib/tcell_agent/policies/content_security_policy_spec.rb +161 -0
  94. data/spec/lib/tcell_agent/policies/dataloss_policy_spec.rb +9 -9
  95. data/spec/lib/tcell_agent/policies/http_redirect_policy_spec.rb +243 -198
  96. data/spec/lib/tcell_agent/policies/js_agent_policy_spec.rb +75 -0
  97. data/spec/lib/tcell_agent/policies/login_policy_spec.rb +165 -33
  98. data/spec/lib/tcell_agent/policies/patches_policy_spec.rb +84 -277
  99. data/spec/lib/tcell_agent/policies/policies_manager_spec.rb +104 -0
  100. data/spec/lib/tcell_agent/policies/policy_polling_spec.rb +6 -0
  101. data/spec/lib/tcell_agent/policies/secure_headers_policy_spec.rb +56 -0
  102. data/spec/lib/tcell_agent/rails/csrf_exception_spec.rb +9 -18
  103. data/spec/lib/tcell_agent/rails/js_agent_insert_spec.rb +13 -30
  104. data/spec/lib/tcell_agent/rails/logger_spec.rb +27 -7
  105. data/spec/lib/tcell_agent/rails/middleware/tcell_body_proxy_spec.rb +17 -12
  106. data/spec/lib/tcell_agent/rails/routes/routes_spec.rb +14 -14
  107. data/spec/lib/tcell_agent/sensor_events/util/sanitizer_utilities_spec.rb +0 -35
  108. data/spec/lib/tcell_agent/settings_reporter_spec.rb +127 -153
  109. data/spec/spec_helper.rb +1 -1
  110. data/spec/support/builders.rb +104 -0
  111. data/spec/support/force_logger_mocking.rb +38 -0
  112. data/spec/support/resources/lfi_sample_file.txt +2 -0
  113. data/spec/support/static_agent_overrides.rb +0 -15
  114. metadata +63 -74
  115. data/lib/tcell_agent/agent/event_processor.rb +0 -326
  116. data/lib/tcell_agent/agent/fork_pipe_manager.rb +0 -113
  117. data/lib/tcell_agent/agent/policy_manager.rb +0 -219
  118. data/lib/tcell_agent/agent/policy_types.rb +0 -30
  119. data/lib/tcell_agent/api.rb +0 -91
  120. data/lib/tcell_agent/appsensor/injections_reporter.rb +0 -24
  121. data/lib/tcell_agent/config/child_process_events.rb +0 -8
  122. data/lib/tcell_agent/instrumentation/cmdi/backtick.rb +0 -10
  123. data/lib/tcell_agent/instrumentation/cmdi/exec.rb +0 -14
  124. data/lib/tcell_agent/instrumentation/cmdi/popen.rb +0 -28
  125. data/lib/tcell_agent/instrumentation/cmdi/spawn.rb +0 -11
  126. data/lib/tcell_agent/instrumentation/cmdi/system.rb +0 -11
  127. data/lib/tcell_agent/policies/http_tx_policy.rb +0 -60
  128. data/lib/tcell_agent/policies/login_fraud_policy.rb +0 -45
  129. data/lib/tcell_agent/policies/rust_policies.rb +0 -110
  130. data/lib/tcell_agent/rails.rb +0 -40
  131. data/lib/tcell_agent/rust/libtcellagent-1.3.2.dylib +0 -0
  132. data/lib/tcell_agent/rust/tcellagent-1.3.2.dll +0 -0
  133. data/lib/tcell_agent/rust/whisperer.rb +0 -308
  134. data/lib/tcell_agent/sensor_events/appsensor_event.rb +0 -52
  135. data/lib/tcell_agent/sensor_events/appsensor_meta_event.rb +0 -45
  136. data/lib/tcell_agent/sensor_events/command_injection.rb +0 -75
  137. data/lib/tcell_agent/sensor_events/honeytokens.rb +0 -16
  138. data/lib/tcell_agent/sensor_events/login_fraud.rb +0 -60
  139. data/lib/tcell_agent/sensor_events/metrics.rb +0 -123
  140. data/lib/tcell_agent/sensor_events/patches.rb +0 -21
  141. data/lib/tcell_agent/start_background_thread.rb +0 -55
  142. data/lib/tcell_agent/system_info.rb +0 -11
  143. data/lib/tcell_agent/utils/io.rb +0 -38
  144. data/lib/tcell_agent/utils/passwords.rb +0 -28
  145. data/lib/tcell_agent/utils/queue_with_timeout.rb +0 -142
  146. data/spec/lib/tcell_agent/agent/fork_pipe_manager_spec.rb +0 -100
  147. data/spec/lib/tcell_agent/agent/policy_manager_spec.rb +0 -535
  148. data/spec/lib/tcell_agent/agent/static_agent_spec.rb +0 -133
  149. data/spec/lib/tcell_agent/api/api_spec.rb +0 -39
  150. data/spec/lib/tcell_agent/appsensor/injections_reporter_spec.rb +0 -187
  151. data/spec/lib/tcell_agent/instrumentation_spec.rb +0 -225
  152. data/spec/lib/tcell_agent/policies/appsensor_policy_spec.rb +0 -517
  153. data/spec/lib/tcell_agent/policies/http_tx_policy_spec.rb +0 -22
  154. data/spec/lib/tcell_agent/rails/middleware/appsensor_middleware_spec.rb +0 -293
  155. data/spec/lib/tcell_agent/rails/middleware/dlp_middleware_spec.rb +0 -198
  156. data/spec/lib/tcell_agent/rails/middleware/global_middleware_spec.rb +0 -180
  157. data/spec/lib/tcell_agent/rails/middleware/redirect_middleware_spec.rb +0 -116
  158. data/spec/lib/tcell_agent/rust/models_spec.rb +0 -120
  159. data/spec/lib/tcell_agent/rust/whisperer_spec.rb +0 -704
  160. data/spec/lib/tcell_agent/sensor_events/appsensor_meta_event_spec.rb +0 -45
  161. data/spec/lib/tcell_agent/sensor_events/sessions_metric_spec.rb +0 -272
  162. data/spec/lib/tcell_agent/utils/bounded_queue_spec.rb +0 -52
  163. data/spec/lib/tcell_agent/utils/passwords_spec.rb +0 -143
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module TCellAgent
4
- module Policies
5
- describe HttpTxPolicy do
6
- http_tx_policy_json = {
7
- 'policy_id' => '01a1',
8
- 'types' => {
9
- 'firehose' => { 'enabled' => true }
10
- }
11
- }
12
- http_tx_from_json = HttpTxPolicy.from_json(http_tx_policy_json)
13
- context 'initialized with 3 items' do
14
- it 'returns true' do
15
- expect(http_tx_from_json.policy_id).to eq('01a1')
16
- expect(http_tx_from_json.firehose['enabled']).to eq(true)
17
- expect(http_tx_from_json.firehose['lite']).to eq(false)
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,293 +0,0 @@
1
- require 'spec_helper'
2
- require 'rack/test'
3
- require 'rack'
4
-
5
- module TCellAgent
6
- module Instrumentation
7
- module Rails
8
- module Middleware
9
- regex_policy = {
10
- 'data' => {
11
- 'patterns' => [
12
- {
13
- 'id' => 'tc-xss-1',
14
- 'pattern' => '(?:<(script|iframe|embed|frame|frameset|' \
15
- 'object|img|applet|body|html|style|layer|link|ilayer|meta|bgsound))',
16
- 'sensor' => 'xss',
17
- 'title' => 'Basic Injection'
18
- },
19
-
20
- {
21
- 'id' => 'tc-sqli-1',
22
- 'pattern' => "(?:(?:\\d[\"'`\u00b4\u2019\u2018]\\s+" \
23
- "[\"'`\u00b4\u2019\u2018]\\s+\\d)|(?:^admin" \
24
- "\\s*?[\"'`\u00b4\u2019\u2018]|(/\\*)+[\"'`" \
25
- "\u00b4\u2019\u2018]+\\s?(?:--|#|/\\*|\\{)?)|" \
26
- "(?:[\"'`\u00b4\u2019\u2018]\\s*?\\b(x?or|div|" \
27
- 'like|between|and)\\b\\s*?[+<>=(),-]\\s*?[\\d"' \
28
- "'`\u00b4\u2019\u2018])|(?:[\"'`\u00b4\u2019\u2018]" \
29
- "\\s*?[^\\w\\s]?=\\s*?[\"'`\u00b4\u2019\u2018])|(?:[\"'`" \
30
- "\u00b4\u2019\u2018]\\W*?[+=]+\\W*?[\"'`\u00b4\u2019\u2018])" \
31
- "|(?:[\"'`\u00b4\u2019\u2018]\\s*?[!=|][\\d\\s!=+-]+.*?[\"'`" \
32
- "\u00b4\u2019\u2018(].*?$)|(?:[\"'`\u00b4\u2019\u2018]\\s*?[!=|]" \
33
- "[\\d\\s!=]+.*?\\d+$)|(?:[\"'`\u00b4\u2019\u2018]\\s*?" \
34
- "like\\W+[\\w\"'`\u00b4\u2019\u2018(])|(?:\\sis\\s*?0\\W)" \
35
- "|(?:where\\s[\\s\\w\\.,-]+\\s=)|(?:[\"'`\u00b4\u2019\u2018" \
36
- "][<>~]+[\"'`\u00b4\u2019\u2018]))",
37
- 'sensor' => 'sqli',
38
- 'title' => 'Basic Injection'
39
- },
40
-
41
- {
42
- 'id' => 'tc-fpt-2',
43
- 'pattern' => '(?:%c0%ae/)|(?:(?:/|\\\\)(conf|usr|etc|proc|opt|s?bin' \
44
- '|local|dev|tmp|kern|[br]oot|sys|system|windows|winnt|program|' \
45
- '%[a-z_-]{3,}%)(?:/|\\\\))|(?:(?:/|\\\\)inetpub|localstart\\.asp|' \
46
- 'boot\\.ini)',
47
- 'sensor' => 'fpt',
48
- 'title' => 'Common System Probing'
49
- }
50
- ],
51
- 'version' => 1_518_546_622_571
52
- },
53
- 'policy_id' => 'f3a313b0-10eb-11e8-8080-808080808080',
54
- 'version' => 1
55
- }
56
-
57
- class MockAppsensorRackApp
58
- attr_reader :request_body
59
-
60
- def initialize(route_id = nil)
61
- @route_id = route_id
62
- @request_headers = {}
63
- end
64
-
65
- def call(env)
66
- @env = env
67
- Rack::Request.new(env)
68
- response_headers = { 'Content-Type' => 'text/html' }
69
- env['tcell.request_data'].transaction_id = 'a-b-c-d-e-f'
70
- env['tcell.request_data'].route_id = @route_id
71
- [200, response_headers, ['OK']]
72
- end
73
-
74
- def [](key)
75
- @env[key]
76
- end
77
- end
78
-
79
- describe HeadersMiddleware do
80
- let(:app) { MockAppsensorRackApp.new }
81
- let(:app2) { MockAppsensorRackApp.new('myrouteid') }
82
-
83
- subject { with_tcell_middleware(app) }
84
-
85
- context 'Appsensor Middleware' do
86
- before(:each) do
87
- TCellAgent.configuration = TCellAgent::Configuration.new
88
- TCellAgent.configuration.read_config_from_file(get_test_resource_path('normal_config.json'))
89
- end
90
- let(:request) { Rack::MockRequest.new(subject) }
91
- let(:request2) { Rack::MockRequest.new(with_tcell_middleware(app2)) }
92
- let(:agent) { ::TCellAgent::Agent.new }
93
-
94
- context 'XSS' do
95
- context 'with allow_payloads = false' do
96
- before(:each) do
97
- old_uap = TCellAgent.configuration.allow_payloads
98
- TCellAgent.configuration.allow_payloads = true
99
- TCellAgent.thread_agent.process_policy_json(
100
- {
101
- 'regex' => regex_policy,
102
- 'appsensor' => {
103
- 'policy_id' => '153ed270',
104
- 'version' => 2,
105
- 'data' => {
106
- 'options' => {
107
- 'payloads' => {
108
- 'send_payloads' => true,
109
- 'log_payloads' => false
110
- }
111
- },
112
- 'sensors' => {
113
- 'xss' => {
114
- 'dynamic_patterns' => ['tc-xss-1'],
115
- 'patterns' => ['1']
116
- }
117
- }
118
- }
119
- }
120
- },
121
- false
122
- )
123
-
124
- TCellAgent.empty_event_queue
125
- TCellAgent.configuration.allow_payloads = old_uap
126
- end
127
-
128
- it 'alerts on get xss payload' do
129
- request.get('/foo?xyz=%3CSCRIPT%3Ealert(1)%3C%2Fscript%3E', 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6')
130
- expected_as = {
131
- 'event_type' => 'as',
132
- 'dp' => 'xss',
133
- 'param' => 'xyz',
134
- 'remote_addr' => '1.3.3.4',
135
- 'm' => 'GET',
136
- 'pattern' => 'tc-xss-1',
137
- 'uri' => 'http://example.org/foo?xyz=',
138
- 'meta' => {
139
- 'l' => 'query',
140
- 'h' => [],
141
- 'num_headers' => 1,
142
- 'summary' => [{ 'n' => 'content-length', 's' => 1 }]
143
- },
144
- 'payload' => '<SCRIPT>alert(1)</script>'
145
- }
146
- expect(TCellAgent.event_queue).to include(expected_as)
147
- end
148
-
149
- it 'alerts on post xss payload' do
150
- request.post('/foo', :input => 'x=<SCRIPT>alert(1)</SCRIPT>', 'REMOTE_ADDR' => '1.2.3.4,3.4.5.6')
151
- expected_as = {
152
- 'event_type' => 'as',
153
- 'dp' => 'xss',
154
- 'param' => 'x',
155
- 'remote_addr' => '1.2.3.4',
156
- 'm' => 'POST',
157
- 'pattern' => 'tc-xss-1',
158
- 'uri' => 'http://example.org/foo',
159
- 'meta' => {
160
- 'l' => 'body',
161
- 'h' => [],
162
- 'num_headers' => 1,
163
- 'summary' => [{ 'n' => 'content-length', 's' => 2 }]
164
- },
165
- 'payload' => '<SCRIPT>alert(1)</SCRIPT>'
166
- }
167
- expect(TCellAgent.event_queue).to include(expected_as)
168
- end
169
-
170
- it 'alerts on get xss payload with route_id' do
171
- request2.get('/foo?xyz=%3Cscript%3Ealert(1)%3C%2Fscript%3E')
172
- expected_as = {
173
- 'event_type' => 'as',
174
- 'dp' => 'xss',
175
- 'param' => 'xyz',
176
- 'rid' => 'myrouteid',
177
- 'm' => 'GET',
178
- 'pattern' => 'tc-xss-1',
179
- 'uri' => 'http://example.org/foo?xyz=',
180
- 'meta' => {
181
- 'l' => 'query',
182
- 'h' => [],
183
- 'num_headers' => 1,
184
- 'summary' => [{ 'n' => 'content-length', 's' => 1 }]
185
- },
186
- 'payload' => '<script>alert(1)</script>'
187
- }
188
- expect(TCellAgent.event_queue).to include(expected_as)
189
- end
190
- end
191
- end
192
-
193
- context 'SQL Injection' do
194
- before(:each) do
195
- TCellAgent.thread_agent.process_policy_json(
196
- {
197
- 'regex' => regex_policy,
198
- 'appsensor' => {
199
- 'policy_id' => '153ed270',
200
- 'version' => 2,
201
- 'data' => {
202
- 'sensors' => {
203
- 'sqli' => {
204
- 'dynamic_patterns' => ['tc-sqli-1'],
205
- 'patterns' => ['1']
206
- }
207
- }
208
- }
209
- }
210
- },
211
- false
212
- )
213
- TCellAgent.empty_event_queue
214
- end
215
-
216
- it 'alerts on get sqli payload' do
217
- old_uap = TCellAgent.configuration.allow_payloads
218
- TCellAgent.configuration.allow_payloads = false
219
- # ' OR '3'='3
220
- request.get('/foo?xyz=abds&def=%27%20OR%20%273%27%3D%273', 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6')
221
- TCellAgent.configuration.allow_payloads = old_uap
222
- expected_as = {
223
- 'event_type' => 'as',
224
- 'dp' => 'sqli',
225
- 'param' => 'def',
226
- 'remote_addr' => '1.3.3.4',
227
- 'm' => 'GET',
228
- 'pattern' => 'tc-sqli-1',
229
- 'uri' => 'http://example.org/foo?xyz=&def=',
230
- 'meta' => {
231
- 'l' => 'query',
232
- 'h' => [],
233
- 'num_headers' => 1,
234
- 'summary' => [{ 'n' => 'content-length', 's' => 1 }]
235
- }
236
- }
237
- expect(TCellAgent.event_queue).to include(expected_as)
238
- end
239
- end
240
-
241
- context 'File Path Traversal' do
242
- it 'alerts on most obvious payload' do
243
- TCellAgent.thread_agent.process_policy_json(
244
- {
245
- 'regex' => regex_policy,
246
- 'appsensor' => {
247
- 'policy_id' => '153ed270',
248
- 'version' => 2,
249
- 'data' => {
250
- 'options' => {
251
- 'payloads' => {
252
- 'send_payloads' => false,
253
- 'log_payloads' => false
254
- }
255
- },
256
- 'sensors' => {
257
- 'fpt' => {
258
- 'dynamic_patterns' => ['tc-fpt-2'],
259
- 'patterns' => ['2']
260
- }
261
- }
262
- }
263
- }
264
- },
265
- false
266
- )
267
- TCellAgent.empty_event_queue
268
-
269
- request.get('/foo?xyz=/ETC/PASSWD', 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6')
270
- expected_as = {
271
- 'event_type' => 'as',
272
- 'dp' => 'fpt',
273
- 'param' => 'xyz',
274
- 'remote_addr' => '1.3.3.4',
275
- 'm' => 'GET',
276
- 'pattern' => 'tc-fpt-2',
277
- 'uri' => 'http://example.org/foo?xyz=',
278
- 'meta' => {
279
- 'l' => 'query',
280
- 'h' => [],
281
- 'num_headers' => 1,
282
- 'summary' => [{ 'n' => 'content-length', 's' => 1 }]
283
- }
284
- }
285
- expect(TCellAgent.event_queue).to include(expected_as)
286
- end
287
- end
288
- end
289
- end
290
- end
291
- end
292
- end
293
- end
@@ -1,198 +0,0 @@
1
- require 'spec_helper'
2
- require 'rack/test'
3
- require 'rack'
4
-
5
- require 'tcell_agent/rails/dlp/process_request'
6
-
7
- module TCellAgent
8
- module Instrumentation
9
- module Rails
10
- module Middleware
11
- class MockDLPRackApp
12
- attr_reader :request_body
13
-
14
- def initialize(body = 'OK', route_id = nil, session_id = nil)
15
- @route_id = route_id
16
- @session_id = session_id
17
- @request_headers = {}
18
- @body = body
19
- end
20
-
21
- def loop_params_hash(method, param_hash, _prefix, &block)
22
- param_hash.each do |param_name, param_value|
23
- if param_value && param_value.is_a?(Hash)
24
- loop_params_hash(method, param_value, 'hash', &block)
25
- elsif !param_value || !param_value.instance_of?(String) || param_value == ''
26
- next
27
- else
28
- yield(method, param_name, param_value)
29
- end
30
- end
31
- end
32
-
33
- def for_params(request, &block)
34
- get_params = request.GET
35
- loop_params_hash('get', get_params, nil, &block) if get_params
36
- post_params = request.POST
37
- loop_params_hash('post', post_params, nil, &block) if post_params
38
- end
39
-
40
- def call(env)
41
- @env = env
42
- rack_request = Rack::Request.new(env)
43
- response_headers = { 'Content-Type' => 'text/html' }
44
- env['tcell.request_data'].transaction_id = 'a-b-c-d-e-f'
45
- env['tcell.request_data'].session_id = @session_id
46
- env['tcell.request_data'].route_id = @route_id
47
- tcell_context = env['tcell.request_data']
48
- dlp_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS)
49
- if dlp_policy
50
- action_objs = dlp_policy.get_actions_for_table('*', '*', 'tablex', 'columnb', tcell_context.route_id)
51
- if action_objs
52
- action_objs.each do |action_obj|
53
- tcell_context.add_response_db_filter('secretvalue', action_obj, 'databx', '*', 'tablex', 'columnb')
54
- end
55
- end
56
- TCellAgent::DLP.handle_request_dlp_parameters(rack_request)
57
- # if tcell_context && dlp_policy && dlp_policy.actions_for_form_parameter?
58
- # for_params(rack_request) { |method, param_name, param_value|
59
- # actions = dlp_policy.get_actions_for_form_parameter(param_name, tcell_context.route_id)
60
- # if actions
61
- # actions.each { |action|
62
- # puts action.action_id
63
- # tcell_context.add_filter_for_request_parameter(param_value, action, param_name)
64
- # }
65
- # end
66
- # }
67
- # end
68
- end
69
- tcell_context.filter_body!(@body)
70
- [200, response_headers, [@body]]
71
- end
72
-
73
- def [](key)
74
- @env[key]
75
- end
76
- end
77
-
78
- describe HeadersMiddleware do
79
- let(:app) { MockDLPRackApp.new }
80
- let(:app2) { MockDLPRackApp.new('My secretvalue othervalue test', 'myrouteid', 'plainsessionid') }
81
-
82
- subject { with_tcell_middleware(app) }
83
-
84
- context 'DLP Middleware' do
85
- before(:each) do
86
- TCellAgent.configuration = TCellAgent::Configuration.new
87
- TCellAgent.configuration.read_config_from_file(get_test_resource_path('normal_config.json'))
88
- end
89
- let(:request) { Rack::MockRequest.new(subject) }
90
- let(:request2) { Rack::MockRequest.new(with_tcell_middleware(app2)) }
91
- let(:agent) { ::TCellAgent::Agent.new }
92
- context 'Event' do
93
- before(:each) do
94
- TCellAgent.thread_agent.process_policy_json(
95
- {
96
- 'dlp' => {
97
- 'policy_id' => 'x1a1',
98
- 'data' => {
99
- 'db_protections' => [
100
- {
101
- 'scope' => 'route',
102
- 'route_ids' => ['myrouteid'],
103
- 'databases' => ['*'],
104
- 'schemas' => ['*'],
105
- 'tables' => ['tablex'],
106
- 'fields' => ['columnb'],
107
- 'id' => '323213',
108
- 'actions' => {
109
- 'log' => ['redact'],
110
- 'body' => ['redact']
111
- }
112
- }
113
- ]
114
- }
115
- }
116
- },
117
- false
118
- )
119
- TCellAgent.empty_event_queue
120
- end
121
- it 'redacts body' do
122
- response = request2.get(
123
- '/some/path2?x=abc',
124
- 'CONTENT_TYPE' => 'text/html',
125
- 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6'
126
- )
127
- expect(response.body).to eq('My [redacted] othervalue test')
128
- # expect(response['Location']).to eq("https://www.google.com")
129
- expected_as = {
130
- 'event_type' => 'dlp',
131
- 'rid' => 'myrouteid',
132
- 'found_in' => 'body',
133
- 'rule' => '323213',
134
- 'type' => 'db',
135
- 'db' => 'databx',
136
- 'schema' => '*',
137
- 'table' => 'tablex',
138
- 'field' => 'columnb',
139
- 'uri' => 'http://example.org/some/path2?x='
140
- }
141
- expect(TCellAgent.event_queue).to include(expected_as)
142
- end
143
- end
144
-
145
- context 'Event for request dlp' do
146
- before(:each) do
147
- TCellAgent.thread_agent.process_policy_json(
148
- {
149
- 'dlp' => {
150
- 'policy_id' => 'x1a1',
151
- 'data' => {
152
- 'request_protections' => [
153
- {
154
- 'variable_context' => 'form',
155
- 'scope' => 'route',
156
- 'route_ids' => ['myrouteid'],
157
- 'variables' => ['test333'],
158
- 'id' => '08080808',
159
- 'actions' => {
160
- 'log' => ['redact'],
161
- 'body' => ['event']
162
- }
163
- }
164
- ]
165
- }
166
- }
167
- },
168
- false
169
- )
170
- TCellAgent.empty_event_queue
171
- end
172
-
173
- it 'redacts body' do
174
- response = request2.get(
175
- '/some/path2?test333=othervalue',
176
- 'CONTENT_TYPE' => 'text/html',
177
- 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6'
178
- )
179
- expect(response.body).to eq('My secretvalue othervalue test')
180
- expected_as = {
181
- 'event_type' => 'dlp',
182
- 'rid' => 'myrouteid',
183
- 'found_in' => 'body',
184
- 'rule' => '08080808',
185
- 'type' => 'req',
186
- 'context' => 'form',
187
- 'variable' => 'test333',
188
- 'uri' => 'http://example.org/some/path2?test333='
189
- }
190
- expect(TCellAgent.event_queue).to include(expected_as)
191
- end
192
- end
193
- end
194
- end
195
- end
196
- end
197
- end
198
- end