PageRenderTime 84ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/tests/tc_xrules.rb

https://github.com/ironbee/ironbee
Ruby | 405 lines | 371 code | 33 blank | 1 comment | 0 complexity | 05ff8ab5088699d33013d344f521e0d6 MD5 | raw file
  1. require 'fileutils'
  2. # Integration testing.
  3. class TestXRules < CLIPPTest::TestCase
  4. include CLIPPTest
  5. def do_clipp_test(action, flag_to_check)
  6. clipp(
  7. :input_hashes => [
  8. simple_hash("GET / HTTP/1.1\nHost: foo.bar\n\n")
  9. ],
  10. :config => '''
  11. ProtectionEngineOptions +blockingMode
  12. LoadModule ibmod_xrules.so
  13. ''',
  14. :default_site_config => <<-EOS
  15. XRuleIpv4 "0.0.0.0/0" #{action}
  16. Rule #{flag_to_check} @clipp_print #{flag_to_check} id:1 rev:1 phase:RESPONSE_HEADER
  17. EOS
  18. )
  19. end
  20. [
  21. [ "EnableBlockingMode", "FLAGS:blockingMode", 1 ],
  22. [ "DisableBlockingMode", "FLAGS:blockingMode", 0 ],
  23. [ "EnableRequestHeaderInspection", "FLAGS:inspectRequestHeader", 1 ],
  24. [ "DisableRequestHeaderInspection", "FLAGS:inspectRequestHeader", 0 ],
  25. [ "EnableRequestURIInspection", "FLAGS:inspectRequestUri", 1 ],
  26. [ "DisableRequestURIInspection", "FLAGS:inspectRequestUri", 0 ],
  27. [ "EnableRequestParamInspection", "FLAGS:inspectRequestParams", 1 ],
  28. [ "DisableRequestParamInspection", "FLAGS:inspectRequestParams", 0 ],
  29. [ "EnableRequestBodyInspection", "FLAGS:inspectRequestBody", 1 ],
  30. [ "DisableRequestBodyInspection", "FLAGS:inspectRequestBody", 0 ],
  31. [ "EnableResponseHeaderInspection", "FLAGS:inspectResponseHeader", 1 ],
  32. [ "DisableResponseHeaderInspection", "FLAGS:inspectResponseHeader", 0 ],
  33. [ "EnableResponseBodyInspection", "FLAGS:inspectResponseBody", 1 ],
  34. [ "DisableResponseBodyInspection", "FLAGS:inspectResponseBody", 0 ],
  35. ].each do |testspec|
  36. action, flag_to_check, flag_value = testspec
  37. self.class_eval(
  38. <<-EOS
  39. def test_xrules_#{action}
  40. do_clipp_test("#{action}", "#{flag_to_check}")
  41. assert_no_issues
  42. assert_log_match /\\\[#{flag_to_check}\\\]: #{flag_value}/
  43. end
  44. EOS
  45. )
  46. end
  47. def test_xruleipv4_no_subnet
  48. clipp(
  49. modules: %w{ xrules },
  50. default_site_config: <<-EOS
  51. ProtectionEngineOptions +blockingMode
  52. XRuleIpv4 "0.0.0.0" EnableBlockingMode
  53. EOS
  54. ) do
  55. transaction do |t|
  56. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  57. end
  58. end
  59. assert_no_issues
  60. end
  61. def test_xruleipv6_no_subnet
  62. clipp(
  63. modules: %w{ xrules },
  64. default_site_config: <<-EOS
  65. ProtectionEngineOptions +blockingMode
  66. XRuleIpv6 "::1" EnableBlockingMode
  67. EOS
  68. ) do
  69. transaction do |t|
  70. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  71. end
  72. end
  73. assert_no_issues
  74. end
  75. def test_setblockflag
  76. clipp(
  77. modules: %w{ xrules txdump },
  78. config: '''
  79. TxDump TxFinished stdout Basic all
  80. ProtectionEngineOptions +blockingMode
  81. ''',
  82. default_site_config: <<-EOS
  83. XRulePath "/" block
  84. EOS
  85. ) do
  86. transaction do |t|
  87. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  88. end
  89. end
  90. assert_log_match 'Blocking Mode = On'
  91. assert_log_match 'IsBlocked'
  92. end
  93. def test_event_tags
  94. clipp(
  95. modules: %w{ xrules txdump },
  96. config: '''
  97. TxDump TxFinished stdout all
  98. ProtectionEngineOptions +blockingMode
  99. ''',
  100. default_site_config: <<-EOS
  101. Rule REQUEST_METHOD @imatch get \\
  102. id:1 \\
  103. phase:REQUEST \\
  104. msg:woops \\
  105. event:alert \\
  106. tag:tag1 \\
  107. tag:tag2 \\
  108. tag:tag3
  109. XRuleEventTag a tag2 c tag3 block
  110. EOS
  111. ) do
  112. transaction do |t|
  113. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  114. end
  115. end
  116. assert_log_match 'Blocking Mode = On'
  117. assert_log_match 'IsBlocked'
  118. end
  119. def test_exception_two_tags_fail
  120. clipp(
  121. modules: %w{ xrules txdump },
  122. config: '''
  123. TxDump TxFinished stdout all
  124. ProtectionEngineOptions +blockingMode
  125. ''',
  126. default_site_config: <<-EOS
  127. Rule REQUEST_METHOD @imatch get \\
  128. id:1 \\
  129. phase:REQUEST \\
  130. msg:woops \\
  131. event:alert \\
  132. tag:tag1 \\
  133. tag:tag2 \\
  134. tag:tag3
  135. XRuleException EventTag:a EventTag:tag2 block
  136. EOS
  137. ) do
  138. transaction do |t|
  139. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  140. end
  141. end
  142. assert_log_match 'Blocking Mode = On'
  143. assert_log_no_match /IsBlocked/
  144. end
  145. def test_exception_two_tags_pass
  146. clipp(
  147. modules: %w{ xrules txdump },
  148. config: '''
  149. TxDump TxFinished stdout all
  150. ProtectionEngineOptions +blockingMode
  151. ''',
  152. default_site_config: <<-EOS
  153. Rule REQUEST_METHOD @imatch get \\
  154. id:1 \\
  155. phase:REQUEST \\
  156. msg:woops \\
  157. event:alert \\
  158. tag:tag1 \\
  159. tag:tag2 \\
  160. tag:tag3
  161. XRuleException EventTag:tag1 EventTag:tag2 block
  162. EOS
  163. ) do
  164. transaction do |t|
  165. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  166. end
  167. end
  168. assert_log_match 'Blocking Mode = On'
  169. assert_log_match 'IsBlocked'
  170. end
  171. def test_exception_two_tags_and_net_ip_pass
  172. clipp(
  173. modules: %w{ xrules txdump },
  174. config: '''
  175. TxDump TxFinished stdout all
  176. ProtectionEngineOptions +blockingMode
  177. ''',
  178. default_site_config: <<-EOS
  179. Rule REQUEST_METHOD @imatch get \\
  180. id:1 \\
  181. phase:REQUEST \\
  182. msg:woops \\
  183. event:alert \\
  184. tag:tag1 \\
  185. tag:tag2 \\
  186. tag:tag3
  187. XRuleException EventTag:tag1 EventTag:tag2 IPv4:5.6.7.0/24 block
  188. EOS
  189. ) do
  190. transaction do |t|
  191. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  192. end
  193. end
  194. assert_log_match 'Blocking Mode = On'
  195. assert_log_match 'IsBlocked'
  196. end
  197. def test_exception_two_tags_and_host_ip_pass
  198. clipp(
  199. modules: %w{ xrules txdump },
  200. config: '''
  201. TxDump TxFinished stdout all
  202. ProtectionEngineOptions +blockingMode
  203. ''',
  204. default_site_config: <<-EOS
  205. Rule REQUEST_METHOD @imatch get \\
  206. id:1 \\
  207. phase:REQUEST \\
  208. msg:woops \\
  209. event:alert \\
  210. tag:tag1 \\
  211. tag:tag2 \\
  212. tag:tag3
  213. XRuleException EventTag:tag1 EventTag:tag2 IPv4:5.6.7.8 block
  214. EOS
  215. ) do
  216. transaction do |t|
  217. t.request(raw: "GET / HTTP/1.1\nHost: foo.bar\n\n")
  218. end
  219. end
  220. assert_log_match 'Blocking Mode = On'
  221. assert_log_match 'IsBlocked'
  222. end
  223. def test_exception_ip_path_tag_two_events
  224. auditlog_base_dir = File.join(BUILDDIR, "auditlogs")
  225. auditlog_idx = File.join(auditlog_base_dir, "idx")
  226. FileUtils.rm_rf(auditlog_base_dir)
  227. FileUtils.mkdir_p(auditlog_base_dir)
  228. clipp(
  229. modules: %w{ xrules txdump },
  230. config: """
  231. TxDump TxFinished stdout all
  232. AuditLogBaseDir #{auditlog_base_dir}
  233. AuditLogIndex #{auditlog_idx}
  234. XRuleGenerateEvent on
  235. ProtectionEngineOptions +blockingMode
  236. """,
  237. default_site_config: <<-EOS
  238. Rule REQUEST_METHOD @imatch get \\
  239. id:1 \\
  240. phase:REQUEST \\
  241. msg:woops \\
  242. event:alert \\
  243. tag:tag1 \\
  244. block:advisory
  245. Rule REQUEST_METHOD @imatch get \\
  246. id:2 \\
  247. phase:REQUEST \\
  248. msg:uhoh \\
  249. event:alert \\
  250. tag:tag2 \\
  251. block:advisory
  252. XRuleException Path:/ EventTag:tag1 EventTag:tag2 IPv4:5.6.7.8 block
  253. EOS
  254. ) do
  255. transaction do |t|
  256. t.request(raw: "GET /hi HTTP/1.1\nHost: foo.bar\n\n")
  257. end
  258. end
  259. assert_log_match 'Blocking Mode = On'
  260. assert_log_match 'IsBlocked'
  261. end
  262. def test_xrule_host_block
  263. clipp(
  264. modhtp: true,
  265. modules: %w{ xrules txdump },
  266. config: '''
  267. ProtectionEngineOptions +blockingMode
  268. TxDump TxFinished stdout all
  269. ''',
  270. default_site_config: <<-EOS
  271. XRuleHostname foo.bar block
  272. EOS
  273. ) do
  274. transaction do |t|
  275. t.request(raw: "GET / HTTP/1.1", headers: { Host: 'www.foo.bar' })
  276. end
  277. end
  278. assert_log_match 'Blocking Mode = On'
  279. assert_log_match 'IsBlocked'
  280. end
  281. def test_xrule_method_block
  282. clipp(
  283. modhtp: true,
  284. modules: %w{ xrules txdump },
  285. config: '''
  286. ProtectionEngineOptions +blockingMode
  287. TxDump TxFinished stdout all
  288. ''',
  289. default_site_config: <<-EOS
  290. XRuleMethod BOB block
  291. EOS
  292. ) do
  293. transaction do |t|
  294. t.request(raw: "BOB / HTTP/1.1")
  295. end
  296. end
  297. assert_log_match 'Blocking Mode = On'
  298. assert_log_match 'IsBlocked'
  299. end
  300. def test_xrule_threat_level
  301. clipp(
  302. modhtp: true,
  303. modules: %w{ xrules },
  304. config: '''
  305. ProtectionEngineOptions +blockingMode
  306. ''',
  307. default_site_config: '''
  308. XRulePath "/local" scaleThreat=10
  309. XRuleEventTag "qa/01" scaleThreat=20
  310. Action \
  311. id:create_event_with_tag rev:1 \
  312. phase:REQUEST \
  313. "tag:qa/01" \
  314. event \
  315. "msg:creating event for XRuleEventTag check."
  316. Rule XRULES:SCALE_THREAT @clipp_print "SCALE_THREAT" id:cp rev:1 phase:LOGGING
  317. '''
  318. ) do
  319. transaction do |t|
  320. t.request(raw: "GET /local HTTP/1.1")
  321. t.response(raw: "HTTP/1.1 200 OK")
  322. end
  323. end
  324. assert_log_match /clipp_print \[SCALE_THREAT\]: 30\.0*/
  325. end
  326. def test_xrules_qa01
  327. clipp(
  328. modhtp: true,
  329. modules: %w{ xrules },
  330. config: '''
  331. ProtectionEngineOptions +blockingMode
  332. ''',
  333. default_site_config: '''
  334. XRuleTime !0,6@04:00-06:30-0230 EnableRequestBodyInspection priority=2
  335. XRuleTime !0@10:00-18:00-0800 Allow priority=1
  336. XRuleTime 0@20:00-22:00-0500 "ScaleThreat=-1" priority=1
  337. XRuleException "EventTag:qid/150011" "Path:/bodgeit/search.jsp" "Ipv4:10.100.14.109/32" "Method:GET" "Param:q" Allow priority=1
  338. XRuleException "EventTag:qid/150001" "Path:/" "Ipv4:61.19.242.155/32" "Method:GET" "Param:q" "RequestHeader:Connection" "Param:foo" "Param:acc" Block priority=1
  339. XRuleException "EventTag:qid/150022" "Time:1,5@12:30-15:00+0300" "Path:/" "Method:GET" "Param:PHPSESSID" Block priority=1
  340. XRuleException "EventTag:qid/150001" "Path:/" "Ipv4:61.19.242.155/32" "Param:account" "RequestContentType:application/xhtml+xml" Allow priority=1
  341. XRuleException "EventTag:qid/150011" "Path:/" "Ipv4:146.63.16.130/32" "Method:GET" "Param:testing" "RequestContentType:application/x-www-form-urlencoded" Block priority=1
  342. '''
  343. ) do
  344. transaction do |t|
  345. t.request(raw: "GET /local HTTP/1.1", headers: {'Content-Type'=> 'text/plain'})
  346. t.response(raw: "HTTP/1.1 200 OK")
  347. end
  348. end
  349. assert_no_issues
  350. end
  351. def test_xrules_redirect_action
  352. clipp(
  353. modhtp: true,
  354. modules: %w{ xrules },
  355. config: '''
  356. ProtectionEngineOptions +blockingMode
  357. ''',
  358. default_site_config: '''
  359. XRuleException "Method:GET" redirect=302,www.example.com priority=1
  360. '''
  361. ) do
  362. transaction do |t|
  363. t.request(raw: "GET /local HTTP/1.1", headers: {'Content-Type'=> 'text/plain'})
  364. t.response(raw: "HTTP/1.1 200 OK")
  365. end
  366. end
  367. assert_log_match 'clipp_error: 302'
  368. assert_log_match 'clipp_header: dir=response action=set hdr=Location value=www.example.com/local'
  369. end
  370. end