FlowControl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * Flow Control tests.
  3. *
  4. * @author tlwr [toby@toby.codes]
  5. *
  6. * @copyright Crown Copyright 2017
  7. * @license Apache-2.0
  8. */
  9. import TestRegister from "../../TestRegister.js";
  10. const ALL_BYTES = [
  11. "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
  12. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
  13. "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f",
  14. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f",
  15. "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
  16. "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f",
  17. "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f",
  18. "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f",
  19. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f",
  20. "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f",
  21. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
  22. "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
  23. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf",
  24. "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf",
  25. "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef",
  26. "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
  27. ].join("");
  28. TestRegister.addTests([
  29. {
  30. name: "Fork: nothing",
  31. input: "",
  32. expectedOutput: "",
  33. recipeConfig: [
  34. {
  35. op: "Fork",
  36. args: ["\n", "\n", false],
  37. },
  38. ],
  39. },
  40. {
  41. name: "Fork, Merge: nothing",
  42. input: "",
  43. expectedOutput: "",
  44. recipeConfig: [
  45. {
  46. op: "Fork",
  47. args: ["\n", "\n", false],
  48. },
  49. {
  50. op: "Merge",
  51. args: [],
  52. },
  53. ],
  54. },
  55. {
  56. name: "Fork, (expect) Error, Merge",
  57. input: "1.1\n2.5\na\n3.4",
  58. expectedError: true,
  59. recipeConfig: [
  60. {
  61. op: "Fork",
  62. args: ["\n", "\n", false],
  63. },
  64. {
  65. op: "Object Identifier to Hex",
  66. args: [],
  67. },
  68. {
  69. op: "Merge",
  70. args: [],
  71. },
  72. ],
  73. },
  74. {
  75. name: "Fork, Conditional Jump, Encodings",
  76. input: "Some data with a 1 in it\nSome data with a 2 in it",
  77. expectedOutput: "U29tZSBkYXRhIHdpdGggYSAxIGluIGl0\n53 6f 6d 65 20 64 61 74 61 20 77 69 74 68 20 61 20 32 20 69 6e 20 69 74\n",
  78. recipeConfig: [
  79. {"op": "Fork", "args": ["\\n", "\\n", false]},
  80. {"op": "Conditional Jump", "args": ["1", false, "skipReturn", "10"]},
  81. {"op": "To Hex", "args": ["Space"]},
  82. {"op": "Return", "args": []},
  83. {"op": "Label", "args": ["skipReturn"]},
  84. {"op": "To Base64", "args": ["A-Za-z0-9+/="]}
  85. ]
  86. },
  87. {
  88. name: "Jump: Empty Label",
  89. input: [
  90. "should be changed",
  91. ].join("\n"),
  92. expectedOutput: [
  93. "should be changed was changed",
  94. ].join("\n"),
  95. recipeConfig: [
  96. {
  97. op: "Jump",
  98. args: ["", 10],
  99. },
  100. {
  101. op: "Find / Replace",
  102. args: [
  103. {
  104. "option": "Regex",
  105. "string": "should be changed"
  106. },
  107. "should be changed was changed",
  108. true,
  109. true,
  110. true,
  111. ],
  112. },
  113. ],
  114. },
  115. {
  116. name: "Jump: skips 1",
  117. input: [
  118. "shouldnt be changed",
  119. ].join("\n"),
  120. expectedOutput: [
  121. "shouldnt be changed",
  122. ].join("\n"),
  123. recipeConfig: [
  124. {
  125. op: "Jump",
  126. args: ["skipReplace", 10],
  127. },
  128. {
  129. op: "Find / Replace",
  130. args: [
  131. {
  132. "option": "Regex",
  133. "string": "shouldnt be changed"
  134. },
  135. "shouldnt be changed was changed",
  136. true,
  137. true,
  138. true,
  139. ],
  140. },
  141. {
  142. op: "Label",
  143. args: ["skipReplace"]
  144. },
  145. ],
  146. },
  147. {
  148. name: "Conditional Jump: Skips 0",
  149. input: [
  150. "match",
  151. "should be changed 1",
  152. "should be changed 2",
  153. ].join("\n"),
  154. expectedOutput: [
  155. "match",
  156. "should be changed 1 was changed",
  157. "should be changed 2 was changed"
  158. ].join("\n"),
  159. recipeConfig: [
  160. {
  161. op: "Conditional Jump",
  162. args: ["match", false, "", 0],
  163. },
  164. {
  165. op: "Find / Replace",
  166. args: [
  167. {
  168. "option": "Regex",
  169. "string": "should be changed 1"
  170. },
  171. "should be changed 1 was changed",
  172. true,
  173. true,
  174. true,
  175. ],
  176. },
  177. {
  178. op: "Find / Replace",
  179. args: [
  180. {
  181. "option": "Regex",
  182. "string": "should be changed 2"
  183. },
  184. "should be changed 2 was changed",
  185. true,
  186. true,
  187. true,
  188. ],
  189. },
  190. ],
  191. },
  192. {
  193. name: "Comment: nothing",
  194. input: "",
  195. expectedOutput: "",
  196. recipeConfig: [
  197. {
  198. "op": "Comment",
  199. "args": [""]
  200. }
  201. ]
  202. },
  203. {
  204. name: "Fork, Comment, Base64",
  205. input: "cat\nsat\nmat",
  206. expectedOutput: "Y2F0\nc2F0\nbWF0\n",
  207. recipeConfig: [
  208. {
  209. "op": "Fork",
  210. "args": ["\\n", "\\n", false]
  211. },
  212. {
  213. "op": "Comment",
  214. "args": ["Testing 123"]
  215. },
  216. {
  217. "op": "To Base64",
  218. "args": ["A-Za-z0-9+/="]
  219. }
  220. ]
  221. },
  222. {
  223. name: "Conditional Jump: Skips 1",
  224. input: [
  225. "match",
  226. "should not be changed",
  227. "should be changed",
  228. ].join("\n"),
  229. expectedOutput: [
  230. "match",
  231. "should not be changed",
  232. "should be changed was changed"
  233. ].join("\n"),
  234. recipeConfig: [
  235. {
  236. op: "Conditional Jump",
  237. args: ["match", false, "skip match", 10],
  238. },
  239. {
  240. op: "Find / Replace",
  241. args: [
  242. {
  243. "option": "Regex",
  244. "string": "should not be changed"
  245. },
  246. "should not be changed was changed",
  247. true,
  248. true,
  249. true,
  250. ],
  251. },
  252. {
  253. op: "Label", args: ["skip match"],
  254. },
  255. {
  256. op: "Find / Replace",
  257. args: [
  258. {
  259. "option": "Regex",
  260. "string": "should be changed"
  261. },
  262. "should be changed was changed",
  263. true,
  264. true,
  265. true,
  266. ],
  267. },
  268. ],
  269. },
  270. {
  271. name: "Conditional Jump: Skips backwards",
  272. input: [
  273. "match",
  274. ].join("\n"),
  275. expectedOutput: [
  276. "replaced",
  277. ].join("\n"),
  278. recipeConfig: [
  279. {
  280. op: "Label",
  281. args: ["back to the beginning"],
  282. },
  283. {
  284. op: "Jump",
  285. args: ["skip replace"],
  286. },
  287. {
  288. op: "Find / Replace",
  289. args: [
  290. {
  291. "option": "Regex",
  292. "string": "match"
  293. },
  294. "replaced",
  295. true,
  296. true,
  297. true,
  298. ],
  299. },
  300. {
  301. op: "Label",
  302. args: ["skip replace"],
  303. },
  304. {
  305. op: "Conditional Jump",
  306. args: ["match", false, "back to the beginning", 10],
  307. },
  308. ],
  309. },
  310. {
  311. name: "Register: RC4 key",
  312. input: "http://malwarez.biz/beacon.php?key=0e932a5c&data=8db7d5ebe38663a54ecbb334e3db11",
  313. expectedOutput: "All the secrets",
  314. recipeConfig: [
  315. {
  316. op: "Register",
  317. args: ["key=([\\da-f]*)", true, false]
  318. },
  319. {
  320. op: "Find / Replace",
  321. args: [
  322. {
  323. "option": "Regex",
  324. "string": ".*data=(.*)"
  325. }, "$1", true, false, true
  326. ]
  327. },
  328. {
  329. op: "RC4",
  330. args: [
  331. {
  332. "option": "Hex",
  333. "string": "$R0"
  334. }, "Hex", "Latin1"
  335. ]
  336. }
  337. ]
  338. },
  339. {
  340. name: "Register: AES key",
  341. input: "51e201d463698ef5f717f71f5b4712af20be674b3bff53d38546396ee61daac4908e319ca3fcf7089bfb6b38ea99e781d26e577ba9dd6f311a39420b8978e93014b042d44726caedf5436eaf652429c0df94b521676c7c2ce812097c277273c7c72cd89aec8d9fb4a27586ccf6aa0aee224c34ba3bfdf7aeb1ddd477622b91e72c9e709ab60f8daf731ec0cc85ce0f746ff1554a5a3ec291ca40f9e629a872592d988fdd834534aba79c1ad1676769a7c010bf04739ecdb65d95302371d629d9e37e7b4a361da468f1ed5358922d2ea752dd11c366f3017b14aa011d2af03c44f95579098a15e3cf9b4486f8ffe9c239f34de7151f6ca6500fe4b850c3f1c02e801caf3a24464614e42801615b8ffaa07ac8251493ffda7de5ddf3368880c2b95b030f41f8f15066add071a66cf60e5f46f3a230d397b652963a21a53f",
  342. expectedOutput: `"You know," said Arthur, "it's at times like this, when I'm trapped in a Vogon airlock with a man from Betelgeuse, and about to die of asphyxiation in deep space that I really wish I'd listened to what my mother told me when I was young."
  343. "Why, what did she tell you?"
  344. "I don't know, I didn't listen."`,
  345. recipeConfig: [
  346. {
  347. op: "Register",
  348. args: ["(.{32})", true, false]
  349. },
  350. {
  351. op: "Drop bytes",
  352. args: [0, 32, false]
  353. },
  354. {
  355. op: "AES Decrypt",
  356. args: [
  357. {
  358. "option": "Hex",
  359. "string": "1748e7179bd56570d51fa4ba287cc3e5"
  360. },
  361. {
  362. "option": "Hex",
  363. "string": "$R0"
  364. },
  365. "CTR", "Hex", "Raw",
  366. {
  367. "option": "Hex",
  368. "string": ""
  369. }
  370. ]
  371. }
  372. ]
  373. },
  374. {
  375. name: "Label, Comment: Complex content",
  376. input: ALL_BYTES,
  377. expectedOutput: ALL_BYTES,
  378. recipeConfig: [
  379. {
  380. op: "Label",
  381. args: [""]
  382. },
  383. {
  384. op: "Comment",
  385. args: [""]
  386. }
  387. ]
  388. },
  389. ]);