You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

archives.c 52KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "message.h"
  18. #include "task.h"
  19. #include "archives.h"
  20. #include "libmime/mime_encoding.h"
  21. #include <unicode/uchar.h>
  22. #include <unicode/utf8.h>
  23. #include <unicode/utf16.h>
  24. #include <unicode/ucnv.h>
  25. #include <archive.h>
  26. #include <archive_entry.h>
  27. #define msg_debug_archive(...) rspamd_conditional_debug_fast(NULL, NULL, \
  28. rspamd_archive_log_id, "archive", task->task_pool->tag.uid, \
  29. G_STRFUNC, \
  30. __VA_ARGS__)
  31. INIT_LOG_MODULE(archive)
  32. static void
  33. rspamd_archive_dtor(gpointer p)
  34. {
  35. struct rspamd_archive *arch = p;
  36. struct rspamd_archive_file *f;
  37. unsigned int i;
  38. for (i = 0; i < arch->files->len; i++) {
  39. f = g_ptr_array_index(arch->files, i);
  40. if (f->fname) {
  41. g_string_free(f->fname, TRUE);
  42. }
  43. g_free(f);
  44. }
  45. g_ptr_array_free(arch->files, TRUE);
  46. }
  47. static bool
  48. rspamd_archive_file_try_utf(struct rspamd_task *task,
  49. struct rspamd_archive *arch,
  50. struct rspamd_archive_file *fentry,
  51. const char *in, gsize inlen)
  52. {
  53. const char *charset = NULL, *p, *end;
  54. GString *res;
  55. charset = rspamd_mime_charset_find_by_content(in, inlen, TRUE);
  56. if (charset) {
  57. UChar *tmp;
  58. UErrorCode uc_err = U_ZERO_ERROR;
  59. int32_t r, clen, dlen;
  60. struct rspamd_charset_converter *conv;
  61. UConverter *utf8_converter;
  62. conv = rspamd_mime_get_converter_cached(charset, task->task_pool,
  63. TRUE, &uc_err);
  64. utf8_converter = rspamd_get_utf8_converter();
  65. if (conv == NULL) {
  66. msg_info_task("cannot open converter for %s: %s",
  67. charset, u_errorName(uc_err));
  68. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  69. fentry->fname = g_string_new_len(in, inlen);
  70. return false;
  71. }
  72. tmp = g_malloc(sizeof(*tmp) * (inlen + 1));
  73. r = rspamd_converter_to_uchars(conv, tmp, inlen + 1,
  74. in, inlen, &uc_err);
  75. if (!U_SUCCESS(uc_err)) {
  76. msg_info_task("cannot convert data to unicode from %s: %s",
  77. charset, u_errorName(uc_err));
  78. g_free(tmp);
  79. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  80. fentry->fname = g_string_new_len(in, inlen);
  81. return NULL;
  82. }
  83. int i = 0;
  84. while (i < r) {
  85. UChar32 uc;
  86. U16_NEXT(tmp, i, r, uc);
  87. if (IS_ZERO_WIDTH_SPACE(uc) || u_iscntrl(uc)) {
  88. msg_info_task("control character in archive file name found: 0x%02xd "
  89. "(filename=%T)",
  90. uc, arch->archive_name);
  91. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  92. break;
  93. }
  94. }
  95. clen = ucnv_getMaxCharSize(utf8_converter);
  96. dlen = UCNV_GET_MAX_BYTES_FOR_STRING(r, clen);
  97. res = g_string_sized_new(dlen);
  98. r = ucnv_fromUChars(utf8_converter, res->str, dlen, tmp, r, &uc_err);
  99. if (!U_SUCCESS(uc_err)) {
  100. msg_info_task("cannot convert data from unicode from %s: %s",
  101. charset, u_errorName(uc_err));
  102. g_free(tmp);
  103. g_string_free(res, TRUE);
  104. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  105. fentry->fname = g_string_new_len(in, inlen);
  106. return NULL;
  107. }
  108. g_free(tmp);
  109. res->len = r;
  110. msg_debug_archive("converted from %s to UTF-8 inlen: %z, outlen: %d",
  111. charset, inlen, r);
  112. fentry->fname = res;
  113. }
  114. else {
  115. /* Convert unsafe characters to '?' */
  116. res = g_string_sized_new(inlen);
  117. p = in;
  118. end = in + inlen;
  119. while (p < end) {
  120. if (g_ascii_isgraph(*p)) {
  121. g_string_append_c(res, *p);
  122. }
  123. else {
  124. g_string_append_c(res, '?');
  125. if (*p < 0x7f && (g_ascii_iscntrl(*p) || *p == '\0')) {
  126. if (!(fentry->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED)) {
  127. msg_info_task("suspicious character in archive file name found: 0x%02xd "
  128. "(filename=%T)",
  129. (int) *p, arch->archive_name);
  130. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  131. }
  132. }
  133. }
  134. p++;
  135. }
  136. fentry->fname = res;
  137. }
  138. return true;
  139. }
  140. static void
  141. rspamd_archive_process_zip(struct rspamd_task *task,
  142. struct rspamd_mime_part *part)
  143. {
  144. const unsigned char *p, *start, *end, *eocd = NULL, *cd;
  145. const uint32_t eocd_magic = 0x06054b50, cd_basic_len = 46;
  146. const unsigned char cd_magic[] = {0x50, 0x4b, 0x01, 0x02};
  147. const unsigned int max_processed = 1024;
  148. uint32_t cd_offset, cd_size, comp_size, uncomp_size, processed = 0;
  149. uint16_t extra_len, fname_len, comment_len;
  150. struct rspamd_archive *arch;
  151. struct rspamd_archive_file *f = NULL;
  152. /* Zip files have interesting data at the end of archive */
  153. p = part->parsed_data.begin + part->parsed_data.len - 1;
  154. start = part->parsed_data.begin;
  155. end = p;
  156. /* Search for EOCD:
  157. * 22 bytes is a typical size of eocd without a comment and
  158. * end points one byte after the last character
  159. */
  160. p -= 21;
  161. while (p > start + sizeof(uint32_t)) {
  162. uint32_t t;
  163. if (processed > max_processed) {
  164. break;
  165. }
  166. /* XXX: not an efficient approach */
  167. memcpy(&t, p, sizeof(t));
  168. if (GUINT32_FROM_LE(t) == eocd_magic) {
  169. eocd = p;
  170. break;
  171. }
  172. p--;
  173. processed++;
  174. }
  175. if (eocd == NULL) {
  176. /* Not a zip file */
  177. msg_info_task("zip archive is invalid (no EOCD)");
  178. return;
  179. }
  180. if (end - eocd < 21) {
  181. msg_info_task("zip archive is invalid (short EOCD)");
  182. return;
  183. }
  184. memcpy(&cd_size, eocd + 12, sizeof(cd_size));
  185. cd_size = GUINT32_FROM_LE(cd_size);
  186. memcpy(&cd_offset, eocd + 16, sizeof(cd_offset));
  187. cd_offset = GUINT32_FROM_LE(cd_offset);
  188. /* We need to check sanity as well */
  189. if (cd_offset + cd_size > (unsigned int) (eocd - start)) {
  190. msg_info_task("zip archive is invalid (bad size/offset for CD)");
  191. return;
  192. }
  193. cd = start + cd_offset;
  194. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  195. arch->files = g_ptr_array_new();
  196. arch->type = RSPAMD_ARCHIVE_ZIP;
  197. if (part->cd) {
  198. arch->archive_name = &part->cd->filename;
  199. }
  200. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  201. arch);
  202. while (cd < start + cd_offset + cd_size) {
  203. uint16_t flags;
  204. /* Read central directory record */
  205. if (eocd - cd < cd_basic_len ||
  206. memcmp(cd, cd_magic, sizeof(cd_magic)) != 0) {
  207. msg_info_task("zip archive is invalid (bad cd record)");
  208. return;
  209. }
  210. memcpy(&flags, cd + 8, sizeof(uint16_t));
  211. flags = GUINT16_FROM_LE(flags);
  212. memcpy(&comp_size, cd + 20, sizeof(uint32_t));
  213. comp_size = GUINT32_FROM_LE(comp_size);
  214. memcpy(&uncomp_size, cd + 24, sizeof(uint32_t));
  215. uncomp_size = GUINT32_FROM_LE(uncomp_size);
  216. memcpy(&fname_len, cd + 28, sizeof(fname_len));
  217. fname_len = GUINT16_FROM_LE(fname_len);
  218. memcpy(&extra_len, cd + 30, sizeof(extra_len));
  219. extra_len = GUINT16_FROM_LE(extra_len);
  220. memcpy(&comment_len, cd + 32, sizeof(comment_len));
  221. comment_len = GUINT16_FROM_LE(comment_len);
  222. if (cd + fname_len + comment_len + extra_len + cd_basic_len > eocd) {
  223. msg_info_task("zip archive is invalid (too large cd record)");
  224. return;
  225. }
  226. f = g_malloc0(sizeof(*f));
  227. rspamd_archive_file_try_utf(task, arch, f, cd + cd_basic_len, fname_len);
  228. f->compressed_size = comp_size;
  229. f->uncompressed_size = uncomp_size;
  230. if (flags & 0x41u) {
  231. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  232. }
  233. if (f->fname) {
  234. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  235. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  236. }
  237. g_ptr_array_add(arch->files, f);
  238. msg_debug_archive("found file in zip archive: %v", f->fname);
  239. }
  240. else {
  241. g_free(f);
  242. return;
  243. }
  244. /* Process extra fields */
  245. const unsigned char *extra = cd + fname_len + cd_basic_len;
  246. p = extra;
  247. while (p + sizeof(uint16_t) * 2 < extra + extra_len) {
  248. uint16_t hid, hlen;
  249. memcpy(&hid, p, sizeof(uint16_t));
  250. hid = GUINT16_FROM_LE(hid);
  251. memcpy(&hlen, p + sizeof(uint16_t), sizeof(uint16_t));
  252. hlen = GUINT16_FROM_LE(hlen);
  253. if (hid == 0x0017) {
  254. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  255. }
  256. p += hlen + sizeof(uint16_t) * 2;
  257. }
  258. cd += fname_len + comment_len + extra_len + cd_basic_len;
  259. }
  260. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  261. part->specific.arch = arch;
  262. arch->size = part->parsed_data.len;
  263. }
  264. static inline int
  265. rspamd_archive_rar_read_vint(const unsigned char *start, gsize remain, uint64_t *res)
  266. {
  267. /*
  268. * From http://www.rarlab.com/technote.htm:
  269. * Variable length integer. Can include one or more bytes, where
  270. * lower 7 bits of every byte contain integer data and highest bit
  271. * in every byte is the continuation flag.
  272. * If highest bit is 0, this is the last byte in sequence.
  273. * So first byte contains 7 least significant bits of integer and
  274. * continuation flag. Second byte, if present, contains next 7 bits and so on.
  275. */
  276. uint64_t t = 0;
  277. unsigned int shift = 0;
  278. const unsigned char *p = start;
  279. while (remain > 0 && shift <= 57) {
  280. if (*p & 0x80) {
  281. t |= ((uint64_t) (*p & 0x7f)) << shift;
  282. }
  283. else {
  284. t |= ((uint64_t) (*p & 0x7f)) << shift;
  285. p++;
  286. break;
  287. }
  288. shift += 7;
  289. p++;
  290. remain--;
  291. }
  292. if (remain == 0 || shift > 64) {
  293. return -1;
  294. }
  295. *res = GUINT64_FROM_LE(t);
  296. return p - start;
  297. }
  298. #define RAR_SKIP_BYTES(n) \
  299. do { \
  300. if ((n) <= 0) { \
  301. msg_debug_archive("rar archive is invalid (bad skip value)"); \
  302. return; \
  303. } \
  304. if ((gsize) (end - p) < (n)) { \
  305. msg_debug_archive("rar archive is invalid (truncated)"); \
  306. return; \
  307. } \
  308. p += (n); \
  309. } while (0)
  310. #define RAR_READ_VINT() \
  311. do { \
  312. r = rspamd_archive_rar_read_vint(p, end - p, &vint); \
  313. if (r == -1) { \
  314. msg_debug_archive("rar archive is invalid (bad vint)"); \
  315. return; \
  316. } \
  317. else if (r == 0) { \
  318. msg_debug_archive("rar archive is invalid (BAD vint offset)"); \
  319. return; \
  320. } \
  321. } while (0)
  322. #define RAR_READ_VINT_SKIP() \
  323. do { \
  324. r = rspamd_archive_rar_read_vint(p, end - p, &vint); \
  325. if (r == -1) { \
  326. msg_debug_archive("rar archive is invalid (bad vint)"); \
  327. return; \
  328. } \
  329. p += r; \
  330. } while (0)
  331. #define RAR_READ_UINT16(n) \
  332. do { \
  333. if (end - p < (glong) sizeof(uint16_t)) { \
  334. msg_debug_archive("rar archive is invalid (bad int16)"); \
  335. return; \
  336. } \
  337. n = p[0] + (p[1] << 8); \
  338. p += sizeof(uint16_t); \
  339. } while (0)
  340. #define RAR_READ_UINT32(n) \
  341. do { \
  342. if (end - p < (glong) sizeof(uint32_t)) { \
  343. msg_debug_archive("rar archive is invalid (bad int32)"); \
  344. return; \
  345. } \
  346. n = (unsigned int) p[0] + ((unsigned int) p[1] << 8) + ((unsigned int) p[2] << 16) + ((unsigned int) p[3] << 24); \
  347. p += sizeof(uint32_t); \
  348. } while (0)
  349. static void
  350. rspamd_archive_process_rar_v4(struct rspamd_task *task, const unsigned char *start,
  351. const unsigned char *end, struct rspamd_mime_part *part)
  352. {
  353. const unsigned char *p = start, *start_section;
  354. uint8_t type;
  355. unsigned int flags;
  356. uint64_t sz, comp_sz = 0, uncomp_sz = 0;
  357. struct rspamd_archive *arch;
  358. struct rspamd_archive_file *f;
  359. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  360. arch->files = g_ptr_array_new();
  361. arch->type = RSPAMD_ARCHIVE_RAR;
  362. if (part->cd) {
  363. arch->archive_name = &part->cd->filename;
  364. }
  365. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  366. arch);
  367. while (p < end) {
  368. /* Crc16 */
  369. start_section = p;
  370. RAR_SKIP_BYTES(sizeof(uint16_t));
  371. type = *p;
  372. p++;
  373. RAR_READ_UINT16(flags);
  374. if (type == 0x73) {
  375. /* Main header, check for encryption */
  376. if (flags & 0x80) {
  377. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  378. goto end;
  379. }
  380. }
  381. RAR_READ_UINT16(sz);
  382. if (flags & 0x8000) {
  383. /* We also need to read ADD_SIZE element */
  384. uint32_t tmp;
  385. RAR_READ_UINT32(tmp);
  386. sz += tmp;
  387. /* This is also used as PACK_SIZE */
  388. comp_sz = tmp;
  389. }
  390. if (sz == 0) {
  391. /* Zero sized block - error */
  392. msg_debug_archive("rar archive is invalid (zero size block)");
  393. return;
  394. }
  395. if (type == 0x74) {
  396. unsigned int fname_len;
  397. /* File header */
  398. /* Uncompressed size */
  399. RAR_READ_UINT32(uncomp_sz);
  400. /* Skip to NAME_SIZE element */
  401. RAR_SKIP_BYTES(11);
  402. RAR_READ_UINT16(fname_len);
  403. if (fname_len == 0 || fname_len > (gsize) (end - p)) {
  404. msg_debug_archive("rar archive is invalid (bad filename size: %d)",
  405. fname_len);
  406. return;
  407. }
  408. /* Attrs */
  409. RAR_SKIP_BYTES(4);
  410. if (flags & 0x100) {
  411. /* We also need to read HIGH_PACK_SIZE */
  412. uint32_t tmp;
  413. RAR_READ_UINT32(tmp);
  414. sz += tmp;
  415. comp_sz += tmp;
  416. /* HIGH_UNP_SIZE */
  417. RAR_READ_UINT32(tmp);
  418. uncomp_sz += tmp;
  419. }
  420. f = g_malloc0(sizeof(*f));
  421. if (flags & 0x200) {
  422. /* We have unicode + normal version */
  423. unsigned char *tmp;
  424. tmp = memchr(p, '\0', fname_len);
  425. if (tmp != NULL) {
  426. /* Just use ASCII version */
  427. rspamd_archive_file_try_utf(task, arch, f, p, tmp - p);
  428. msg_debug_archive("found ascii filename in rarv4 archive: %v",
  429. f->fname);
  430. }
  431. else {
  432. /* We have UTF8 filename, use it as is */
  433. rspamd_archive_file_try_utf(task, arch, f, p, fname_len);
  434. msg_debug_archive("found utf filename in rarv4 archive: %v",
  435. f->fname);
  436. }
  437. }
  438. else {
  439. rspamd_archive_file_try_utf(task, arch, f, p, fname_len);
  440. msg_debug_archive("found ascii (old) filename in rarv4 archive: %v",
  441. f->fname);
  442. }
  443. f->compressed_size = comp_sz;
  444. f->uncompressed_size = uncomp_sz;
  445. if (flags & 0x4) {
  446. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  447. }
  448. if (f->fname) {
  449. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  450. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  451. }
  452. g_ptr_array_add(arch->files, f);
  453. }
  454. else {
  455. g_free(f);
  456. }
  457. }
  458. p = start_section;
  459. RAR_SKIP_BYTES(sz);
  460. }
  461. end:
  462. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  463. part->specific.arch = arch;
  464. arch->size = part->parsed_data.len;
  465. }
  466. static void
  467. rspamd_archive_process_rar(struct rspamd_task *task,
  468. struct rspamd_mime_part *part)
  469. {
  470. const unsigned char *p, *end, *section_start;
  471. const unsigned char rar_v5_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x01, 0x00},
  472. rar_v4_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00};
  473. const unsigned int rar_encrypted_header = 4, rar_main_header = 1,
  474. rar_file_header = 2;
  475. uint64_t vint, sz, comp_sz = 0, uncomp_sz = 0, flags = 0, type = 0,
  476. extra_sz = 0;
  477. struct rspamd_archive *arch;
  478. struct rspamd_archive_file *f;
  479. int r;
  480. p = part->parsed_data.begin;
  481. end = p + part->parsed_data.len;
  482. if ((gsize) (end - p) <= sizeof(rar_v5_magic)) {
  483. msg_debug_archive("rar archive is invalid (too small)");
  484. return;
  485. }
  486. if (memcmp(p, rar_v5_magic, sizeof(rar_v5_magic)) == 0) {
  487. p += sizeof(rar_v5_magic);
  488. }
  489. else if (memcmp(p, rar_v4_magic, sizeof(rar_v4_magic)) == 0) {
  490. p += sizeof(rar_v4_magic);
  491. rspamd_archive_process_rar_v4(task, p, end, part);
  492. return;
  493. }
  494. else {
  495. msg_debug_archive("rar archive is invalid (no rar magic)");
  496. return;
  497. }
  498. /* Rar v5 format */
  499. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  500. arch->files = g_ptr_array_new();
  501. arch->type = RSPAMD_ARCHIVE_RAR;
  502. if (part->cd) {
  503. arch->archive_name = &part->cd->filename;
  504. }
  505. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  506. arch);
  507. /* Now we can have either encryption header or archive header */
  508. /* Crc 32 */
  509. RAR_SKIP_BYTES(sizeof(uint32_t));
  510. /* Size */
  511. RAR_READ_VINT_SKIP();
  512. sz = vint;
  513. /* Type */
  514. section_start = p;
  515. RAR_READ_VINT_SKIP();
  516. type = vint;
  517. /* Header flags */
  518. RAR_READ_VINT_SKIP();
  519. flags = vint;
  520. if (flags & 0x1) {
  521. /* Have extra zone */
  522. RAR_READ_VINT_SKIP();
  523. }
  524. if (flags & 0x2) {
  525. /* Data zone is presented */
  526. RAR_READ_VINT_SKIP();
  527. sz += vint;
  528. }
  529. if (type == rar_encrypted_header) {
  530. /* We can't read any further information as archive is encrypted */
  531. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  532. goto end;
  533. }
  534. else if (type != rar_main_header) {
  535. msg_debug_archive("rar archive is invalid (bad main header)");
  536. return;
  537. }
  538. /* Nothing useful in main header */
  539. p = section_start;
  540. RAR_SKIP_BYTES(sz);
  541. while (p < end) {
  542. gboolean has_extra = FALSE;
  543. /* Read the next header */
  544. /* Crc 32 */
  545. RAR_SKIP_BYTES(sizeof(uint32_t));
  546. /* Size */
  547. RAR_READ_VINT_SKIP();
  548. sz = vint;
  549. if (sz == 0) {
  550. /* Zero sized block - error */
  551. msg_debug_archive("rar archive is invalid (zero size block)");
  552. return;
  553. }
  554. section_start = p;
  555. /* Type */
  556. RAR_READ_VINT_SKIP();
  557. type = vint;
  558. /* Header flags */
  559. RAR_READ_VINT_SKIP();
  560. flags = vint;
  561. if (flags & 0x1) {
  562. /* Have extra zone */
  563. RAR_READ_VINT_SKIP();
  564. extra_sz = vint;
  565. has_extra = TRUE;
  566. }
  567. if (flags & 0x2) {
  568. /* Data zone is presented */
  569. RAR_READ_VINT_SKIP();
  570. sz += vint;
  571. comp_sz = vint;
  572. }
  573. if (type != rar_file_header) {
  574. p = section_start;
  575. RAR_SKIP_BYTES(sz);
  576. }
  577. else {
  578. /* We have a file header, go forward */
  579. uint64_t fname_len;
  580. bool is_directory = false;
  581. /* File header specific flags */
  582. RAR_READ_VINT_SKIP();
  583. flags = vint;
  584. /* Unpacked size */
  585. RAR_READ_VINT_SKIP();
  586. uncomp_sz = vint;
  587. /* Attributes */
  588. RAR_READ_VINT_SKIP();
  589. if (flags & 0x2) {
  590. /* Unix mtime */
  591. RAR_SKIP_BYTES(sizeof(uint32_t));
  592. }
  593. if (flags & 0x4) {
  594. /* Crc32 */
  595. RAR_SKIP_BYTES(sizeof(uint32_t));
  596. }
  597. if (flags & 0x1) {
  598. /* Ignore directories for sanity purposes */
  599. is_directory = true;
  600. msg_debug_archive("skip directory record in a rar archive");
  601. }
  602. if (!is_directory) {
  603. /* Compression */
  604. RAR_READ_VINT_SKIP();
  605. /* Host OS */
  606. RAR_READ_VINT_SKIP();
  607. /* Filename length (finally!) */
  608. RAR_READ_VINT_SKIP();
  609. fname_len = vint;
  610. if (fname_len == 0 || fname_len > (gsize) (end - p)) {
  611. msg_debug_archive("rar archive is invalid (bad filename size)");
  612. return;
  613. }
  614. f = g_malloc0(sizeof(*f));
  615. f->uncompressed_size = uncomp_sz;
  616. f->compressed_size = comp_sz;
  617. rspamd_archive_file_try_utf(task, arch, f, p, fname_len);
  618. if (f->fname) {
  619. msg_debug_archive("added rarv5 file: %v", f->fname);
  620. g_ptr_array_add(arch->files, f);
  621. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  622. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  623. }
  624. }
  625. else {
  626. g_free(f);
  627. f = NULL;
  628. }
  629. if (f && has_extra && extra_sz > 0 &&
  630. p + fname_len + extra_sz < end) {
  631. /* Try to find encryption record in extra field */
  632. const unsigned char *ex = p + fname_len;
  633. while (ex < p + extra_sz) {
  634. const unsigned char *t;
  635. int64_t cur_sz = 0, sec_type = 0;
  636. r = rspamd_archive_rar_read_vint(ex, extra_sz, &cur_sz);
  637. if (r == -1) {
  638. msg_debug_archive("rar archive is invalid (bad vint)");
  639. return;
  640. }
  641. t = ex + r;
  642. r = rspamd_archive_rar_read_vint(t, extra_sz - r, &sec_type);
  643. if (r == -1) {
  644. msg_debug_archive("rar archive is invalid (bad vint)");
  645. return;
  646. }
  647. if (sec_type == 0x01) {
  648. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  649. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  650. break;
  651. }
  652. ex += cur_sz;
  653. }
  654. }
  655. }
  656. /* Restore p to the beginning of the header */
  657. p = section_start;
  658. RAR_SKIP_BYTES(sz);
  659. }
  660. }
  661. end:
  662. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  663. part->specific.arch = arch;
  664. arch->size = part->parsed_data.len;
  665. }
  666. static inline int
  667. rspamd_archive_7zip_read_vint(const unsigned char *start, gsize remain, uint64_t *res)
  668. {
  669. /*
  670. * REAL_UINT64 means real UINT64.
  671. * UINT64 means real UINT64 encoded with the following scheme:
  672. *
  673. * Size of encoding sequence depends from first byte:
  674. * First_Byte Extra_Bytes Value
  675. * (binary)
  676. * 0xxxxxxx : ( xxxxxxx )
  677. * 10xxxxxx BYTE y[1] : ( xxxxxx << (8 * 1)) + y
  678. * 110xxxxx BYTE y[2] : ( xxxxx << (8 * 2)) + y
  679. * ...
  680. * 1111110x BYTE y[6] : ( x << (8 * 6)) + y
  681. * 11111110 BYTE y[7] : y
  682. * 11111111 BYTE y[8] : y
  683. */
  684. unsigned char t;
  685. if (remain == 0) {
  686. return -1;
  687. }
  688. t = *start;
  689. if (!isset(&t, 7)) {
  690. /* Trivial case */
  691. *res = t;
  692. return 1;
  693. }
  694. else if (t == 0xFF) {
  695. if (remain >= sizeof(uint64_t) + 1) {
  696. memcpy(res, start + 1, sizeof(uint64_t));
  697. *res = GUINT64_FROM_LE(*res);
  698. return sizeof(uint64_t) + 1;
  699. }
  700. }
  701. else {
  702. int cur_bit = 6, intlen = 1;
  703. const unsigned char bmask = 0xFF;
  704. uint64_t tgt;
  705. while (cur_bit > 0) {
  706. if (!isset(&t, cur_bit)) {
  707. if (remain >= intlen + 1) {
  708. memcpy(&tgt, start + 1, intlen);
  709. tgt = GUINT64_FROM_LE(tgt);
  710. /* Shift back */
  711. tgt >>= sizeof(tgt) - NBBY * intlen;
  712. /* Add masked value */
  713. tgt += (uint64_t) (t & (bmask >> (NBBY - cur_bit)))
  714. << (NBBY * intlen);
  715. *res = tgt;
  716. return intlen + 1;
  717. }
  718. }
  719. cur_bit--;
  720. intlen++;
  721. }
  722. }
  723. return -1;
  724. }
  725. #define SZ_READ_VINT_SKIP() \
  726. do { \
  727. r = rspamd_archive_7zip_read_vint(p, end - p, &vint); \
  728. if (r == -1) { \
  729. msg_debug_archive("7z archive is invalid (bad vint)"); \
  730. return; \
  731. } \
  732. p += r; \
  733. } while (0)
  734. #define SZ_READ_VINT(var) \
  735. do { \
  736. int r; \
  737. r = rspamd_archive_7zip_read_vint(p, end - p, &(var)); \
  738. if (r == -1) { \
  739. msg_debug_archive("7z archive is invalid (bad vint): %s", G_STRLOC); \
  740. return NULL; \
  741. } \
  742. p += r; \
  743. } while (0)
  744. #define SZ_READ_UINT64(n) \
  745. do { \
  746. if (end - p < (goffset) sizeof(uint64_t)) { \
  747. msg_debug_archive("7zip archive is invalid (bad uint64): %s", G_STRLOC); \
  748. return; \
  749. } \
  750. memcpy(&(n), p, sizeof(uint64_t)); \
  751. n = GUINT64_FROM_LE(n); \
  752. p += sizeof(uint64_t); \
  753. } while (0)
  754. #define SZ_SKIP_BYTES(n) \
  755. do { \
  756. if (end - p >= (n)) { \
  757. p += (n); \
  758. } \
  759. else { \
  760. msg_debug_archive("7zip archive is invalid (truncated); wanted to read %d bytes, %d avail: %s", (int) (n), (int) (end - p), G_STRLOC); \
  761. return NULL; \
  762. } \
  763. } while (0)
  764. enum rspamd_7zip_header_mark {
  765. kEnd = 0x00,
  766. kHeader = 0x01,
  767. kArchiveProperties = 0x02,
  768. kAdditionalStreamsInfo = 0x03,
  769. kMainStreamsInfo = 0x04,
  770. kFilesInfo = 0x05,
  771. kPackInfo = 0x06,
  772. kUnPackInfo = 0x07,
  773. kSubStreamsInfo = 0x08,
  774. kSize = 0x09,
  775. kCRC = 0x0A,
  776. kFolder = 0x0B,
  777. kCodersUnPackSize = 0x0C,
  778. kNumUnPackStream = 0x0D,
  779. kEmptyStream = 0x0E,
  780. kEmptyFile = 0x0F,
  781. kAnti = 0x10,
  782. kName = 0x11,
  783. kCTime = 0x12,
  784. kATime = 0x13,
  785. kMTime = 0x14,
  786. kWinAttributes = 0x15,
  787. kComment = 0x16,
  788. kEncodedHeader = 0x17,
  789. kStartPos = 0x18,
  790. kDummy = 0x19,
  791. };
  792. #define _7Z_CRYPTO_MAIN_ZIP 0x06F10101 /* Main Zip crypto algo */
  793. #define _7Z_CRYPTO_RAR_29 0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */
  794. #define _7Z_CRYPTO_AES_256_SHA_256 0x06F10701 /* AES-256 + SHA-256 */
  795. #define IS_SZ_ENCRYPTED(codec_id) (((codec_id) == _7Z_CRYPTO_MAIN_ZIP) || \
  796. ((codec_id) == _7Z_CRYPTO_RAR_29) || \
  797. ((codec_id) == _7Z_CRYPTO_AES_256_SHA_256))
  798. static const unsigned char *
  799. rspamd_7zip_read_bits(struct rspamd_task *task,
  800. const unsigned char *p, const unsigned char *end,
  801. struct rspamd_archive *arch, unsigned int nbits,
  802. unsigned int *pbits_set)
  803. {
  804. unsigned mask = 0, avail = 0, i;
  805. gboolean bit_set = 0;
  806. for (i = 0; i < nbits; i++) {
  807. if (mask == 0) {
  808. avail = *p;
  809. SZ_SKIP_BYTES(1);
  810. mask = 0x80;
  811. }
  812. bit_set = (avail & mask) ? 1 : 0;
  813. if (bit_set && pbits_set) {
  814. (*pbits_set)++;
  815. }
  816. mask >>= 1;
  817. }
  818. return p;
  819. }
  820. static const unsigned char *
  821. rspamd_7zip_read_digest(struct rspamd_task *task,
  822. const unsigned char *p, const unsigned char *end,
  823. struct rspamd_archive *arch,
  824. uint64_t num_streams,
  825. unsigned int *pdigest_read)
  826. {
  827. unsigned char all_defined = *p;
  828. uint64_t i;
  829. unsigned int num_defined = 0;
  830. /*
  831. * BYTE AllAreDefined
  832. * if (AllAreDefined == 0)
  833. * {
  834. * for(NumStreams)
  835. * BIT Defined
  836. * }
  837. * UINT32 CRCs[NumDefined]
  838. */
  839. SZ_SKIP_BYTES(1);
  840. if (all_defined) {
  841. num_defined = num_streams;
  842. }
  843. else {
  844. if (num_streams > 8192) {
  845. /* Gah */
  846. return NULL;
  847. }
  848. p = rspamd_7zip_read_bits(task, p, end, arch, num_streams, &num_defined);
  849. if (p == NULL) {
  850. return NULL;
  851. }
  852. }
  853. for (i = 0; i < num_defined; i++) {
  854. SZ_SKIP_BYTES(sizeof(uint32_t));
  855. }
  856. if (pdigest_read) {
  857. *pdigest_read = num_defined;
  858. }
  859. return p;
  860. }
  861. static const unsigned char *
  862. rspamd_7zip_read_pack_info(struct rspamd_task *task,
  863. const unsigned char *p, const unsigned char *end,
  864. struct rspamd_archive *arch)
  865. {
  866. uint64_t pack_pos = 0, pack_streams = 0, i, cur_sz;
  867. unsigned int num_digests = 0;
  868. unsigned char t;
  869. /*
  870. * UINT64 PackPos
  871. * UINT64 NumPackStreams
  872. *
  873. * []
  874. * BYTE NID::kSize (0x09)
  875. * UINT64 PackSizes[NumPackStreams]
  876. * []
  877. *
  878. * []
  879. * BYTE NID::kCRC (0x0A)
  880. * PackStreamDigests[NumPackStreams]
  881. * []
  882. * BYTE NID::kEnd
  883. */
  884. SZ_READ_VINT(pack_pos);
  885. SZ_READ_VINT(pack_streams);
  886. while (p != NULL && p < end) {
  887. t = *p;
  888. SZ_SKIP_BYTES(1);
  889. msg_debug_archive("7zip: read pack info %xc", t);
  890. switch (t) {
  891. case kSize:
  892. /* We need to skip pack_streams VINTS */
  893. for (i = 0; i < pack_streams; i++) {
  894. SZ_READ_VINT(cur_sz);
  895. }
  896. break;
  897. case kCRC:
  898. /* CRCs are more complicated */
  899. p = rspamd_7zip_read_digest(task, p, end, arch, pack_streams,
  900. &num_digests);
  901. break;
  902. case kEnd:
  903. goto end;
  904. break;
  905. default:
  906. p = NULL;
  907. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  908. goto end;
  909. break;
  910. }
  911. }
  912. end:
  913. return p;
  914. }
  915. static const unsigned char *
  916. rspamd_7zip_read_folder(struct rspamd_task *task,
  917. const unsigned char *p, const unsigned char *end,
  918. struct rspamd_archive *arch, unsigned int *pnstreams, unsigned int *ndigests)
  919. {
  920. uint64_t ncoders = 0, i, j, noutstreams = 0, ninstreams = 0;
  921. SZ_READ_VINT(ncoders);
  922. for (i = 0; i < ncoders && p != NULL && p < end; i++) {
  923. uint64_t sz, tmp;
  924. unsigned char t;
  925. /*
  926. * BYTE
  927. * {
  928. * 0:3 CodecIdSize
  929. * 4: Is Complex Coder
  930. * 5: There Are Attributes
  931. * 6: Reserved
  932. * 7: There are more alternative methods. (Not used anymore, must be 0).
  933. * }
  934. * BYTE CodecId[CodecIdSize]
  935. * if (Is Complex Coder)
  936. * {
  937. * UINT64 NumInStreams;
  938. * UINT64 NumOutStreams;
  939. * }
  940. * if (There Are Attributes)
  941. * {
  942. * UINT64 PropertiesSize
  943. * BYTE Properties[PropertiesSize]
  944. * }
  945. */
  946. t = *p;
  947. SZ_SKIP_BYTES(1);
  948. sz = t & 0xF;
  949. /* Codec ID */
  950. tmp = 0;
  951. for (j = 0; j < sz; j++) {
  952. tmp <<= 8;
  953. tmp += p[j];
  954. }
  955. msg_debug_archive("7zip: read codec id: %L", tmp);
  956. if (IS_SZ_ENCRYPTED(tmp)) {
  957. msg_debug_archive("7zip: encrypted codec: %L", tmp);
  958. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  959. }
  960. SZ_SKIP_BYTES(sz);
  961. if (t & (1u << 4)) {
  962. /* Complex */
  963. SZ_READ_VINT(tmp); /* InStreams */
  964. ninstreams += tmp;
  965. SZ_READ_VINT(tmp); /* OutStreams */
  966. noutstreams += tmp;
  967. }
  968. else {
  969. /* XXX: is it correct ? */
  970. noutstreams++;
  971. ninstreams++;
  972. }
  973. if (t & (1u << 5)) {
  974. /* Attributes ... */
  975. SZ_READ_VINT(tmp); /* Size of attrs */
  976. SZ_SKIP_BYTES(tmp);
  977. }
  978. }
  979. if (noutstreams > 1) {
  980. /* BindPairs, WTF, huh */
  981. for (i = 0; i < noutstreams - 1; i++) {
  982. uint64_t tmp;
  983. SZ_READ_VINT(tmp);
  984. SZ_READ_VINT(tmp);
  985. }
  986. }
  987. int64_t npacked = (int64_t) ninstreams - (int64_t) noutstreams + 1;
  988. msg_debug_archive("7zip: instreams=%L, outstreams=%L, packed=%L",
  989. ninstreams, noutstreams, npacked);
  990. if (npacked > 1) {
  991. /* Gah... */
  992. for (i = 0; i < npacked; i++) {
  993. uint64_t tmp;
  994. SZ_READ_VINT(tmp);
  995. }
  996. }
  997. *pnstreams = noutstreams;
  998. (*ndigests) += npacked;
  999. return p;
  1000. }
  1001. static const unsigned char *
  1002. rspamd_7zip_read_coders_info(struct rspamd_task *task,
  1003. const unsigned char *p, const unsigned char *end,
  1004. struct rspamd_archive *arch,
  1005. unsigned int *pnum_folders, unsigned int *pnum_nodigest)
  1006. {
  1007. uint64_t num_folders = 0, i, tmp;
  1008. unsigned char t;
  1009. unsigned int *folder_nstreams = NULL, num_digests = 0, digests_read = 0;
  1010. while (p != NULL && p < end) {
  1011. /*
  1012. * BYTE NID::kFolder (0x0B)
  1013. * UINT64 NumFolders
  1014. * BYTE External
  1015. * switch(External)
  1016. * {
  1017. * case 0:
  1018. * Folders[NumFolders]
  1019. * case 1:
  1020. * UINT64 DataStreamIndex
  1021. * }
  1022. * BYTE ID::kCodersUnPackSize (0x0C)
  1023. * for(Folders)
  1024. * for(Folder.NumOutStreams)
  1025. * UINT64 UnPackSize;
  1026. * []
  1027. * BYTE NID::kCRC (0x0A)
  1028. * UnPackDigests[NumFolders]
  1029. * []
  1030. * BYTE NID::kEnd
  1031. */
  1032. t = *p;
  1033. SZ_SKIP_BYTES(1);
  1034. msg_debug_archive("7zip: read coders info %xc", t);
  1035. switch (t) {
  1036. case kFolder:
  1037. SZ_READ_VINT(num_folders);
  1038. msg_debug_archive("7zip: nfolders=%L", num_folders);
  1039. if (*p != 0) {
  1040. /* External folders */
  1041. SZ_SKIP_BYTES(1);
  1042. SZ_READ_VINT(tmp);
  1043. }
  1044. else {
  1045. SZ_SKIP_BYTES(1);
  1046. if (num_folders > 8192) {
  1047. /* Gah */
  1048. return NULL;
  1049. }
  1050. if (folder_nstreams) {
  1051. g_free(folder_nstreams);
  1052. }
  1053. folder_nstreams = g_malloc(sizeof(int) * num_folders);
  1054. for (i = 0; i < num_folders && p != NULL && p < end; i++) {
  1055. p = rspamd_7zip_read_folder(task, p, end, arch,
  1056. &folder_nstreams[i], &num_digests);
  1057. }
  1058. }
  1059. break;
  1060. case kCodersUnPackSize:
  1061. for (i = 0; i < num_folders && p != NULL && p < end; i++) {
  1062. if (folder_nstreams) {
  1063. for (unsigned int j = 0; j < folder_nstreams[i]; j++) {
  1064. SZ_READ_VINT(tmp); /* Unpacked size */
  1065. msg_debug_archive("7zip: unpacked size "
  1066. "(folder=%d, stream=%d) = %L",
  1067. (int) i, j, tmp);
  1068. }
  1069. }
  1070. else {
  1071. msg_err_task("internal 7zip error");
  1072. }
  1073. }
  1074. break;
  1075. case kCRC:
  1076. /*
  1077. * Here are dragons. Spec tells that here there could be up
  1078. * to nfolders digests. However, according to the actual source
  1079. * code, in case of multiple out streams there should be digests
  1080. * for all out streams.
  1081. *
  1082. * In the real life (tm) it is even more idiotic: all these digests
  1083. * are in another section! But that section needs number of digests
  1084. * that are absent here. It is the most stupid thing I've ever seen
  1085. * in any file format.
  1086. *
  1087. * I hope there *WAS* some reason to do such shit...
  1088. */
  1089. p = rspamd_7zip_read_digest(task, p, end, arch, num_digests,
  1090. &digests_read);
  1091. break;
  1092. case kEnd:
  1093. goto end;
  1094. break;
  1095. default:
  1096. p = NULL;
  1097. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1098. goto end;
  1099. break;
  1100. }
  1101. }
  1102. end:
  1103. if (pnum_nodigest) {
  1104. *pnum_nodigest = num_digests - digests_read;
  1105. }
  1106. if (pnum_folders) {
  1107. *pnum_folders = num_folders;
  1108. }
  1109. if (folder_nstreams) {
  1110. g_free(folder_nstreams);
  1111. }
  1112. return p;
  1113. }
  1114. static const unsigned char *
  1115. rspamd_7zip_read_substreams_info(struct rspamd_task *task,
  1116. const unsigned char *p, const unsigned char *end,
  1117. struct rspamd_archive *arch,
  1118. unsigned int num_folders, unsigned int num_nodigest)
  1119. {
  1120. unsigned char t;
  1121. unsigned int i;
  1122. uint64_t *folder_nstreams;
  1123. if (num_folders > 8192) {
  1124. /* Gah */
  1125. return NULL;
  1126. }
  1127. folder_nstreams = g_alloca(sizeof(uint64_t) * num_folders);
  1128. memset(folder_nstreams, 0, sizeof(uint64_t) * num_folders);
  1129. while (p != NULL && p < end) {
  1130. /*
  1131. * []
  1132. * BYTE NID::kNumUnPackStream; (0x0D)
  1133. * UINT64 NumUnPackStreamsInFolders[NumFolders];
  1134. * []
  1135. *
  1136. * []
  1137. * BYTE NID::kSize (0x09)
  1138. * UINT64 UnPackSizes[??]
  1139. * []
  1140. *
  1141. *
  1142. * []
  1143. * BYTE NID::kCRC (0x0A)
  1144. * Digests[Number of streams with unknown CRC]
  1145. * []
  1146. */
  1147. t = *p;
  1148. SZ_SKIP_BYTES(1);
  1149. msg_debug_archive("7zip: read substream info %xc", t);
  1150. switch (t) {
  1151. case kNumUnPackStream:
  1152. for (i = 0; i < num_folders; i++) {
  1153. uint64_t tmp;
  1154. SZ_READ_VINT(tmp);
  1155. folder_nstreams[i] = tmp;
  1156. }
  1157. break;
  1158. case kCRC:
  1159. /*
  1160. * Read the comment in the rspamd_7zip_read_coders_info
  1161. */
  1162. p = rspamd_7zip_read_digest(task, p, end, arch, num_nodigest,
  1163. NULL);
  1164. break;
  1165. case kSize:
  1166. /*
  1167. * Another brain damaged logic, but we have to support it
  1168. * as there are no ways to proceed without it.
  1169. * In fact, it is just absent in the real life...
  1170. */
  1171. for (i = 0; i < num_folders; i++) {
  1172. for (unsigned int j = 0; j < folder_nstreams[i]; j++) {
  1173. uint64_t tmp;
  1174. SZ_READ_VINT(tmp); /* Who cares indeed */
  1175. }
  1176. }
  1177. break;
  1178. case kEnd:
  1179. goto end;
  1180. break;
  1181. default:
  1182. p = NULL;
  1183. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1184. goto end;
  1185. break;
  1186. }
  1187. }
  1188. end:
  1189. return p;
  1190. }
  1191. static const unsigned char *
  1192. rspamd_7zip_read_main_streams_info(struct rspamd_task *task,
  1193. const unsigned char *p, const unsigned char *end,
  1194. struct rspamd_archive *arch)
  1195. {
  1196. unsigned char t;
  1197. unsigned int num_folders = 0, unknown_digests = 0;
  1198. while (p != NULL && p < end) {
  1199. t = *p;
  1200. SZ_SKIP_BYTES(1);
  1201. msg_debug_archive("7zip: read main streams info %xc", t);
  1202. /*
  1203. *
  1204. * []
  1205. * PackInfo
  1206. * []
  1207. * []
  1208. * CodersInfo
  1209. * []
  1210. *
  1211. * []
  1212. * SubStreamsInfo
  1213. * []
  1214. *
  1215. * BYTE NID::kEnd
  1216. */
  1217. switch (t) {
  1218. case kPackInfo:
  1219. p = rspamd_7zip_read_pack_info(task, p, end, arch);
  1220. break;
  1221. case kUnPackInfo:
  1222. p = rspamd_7zip_read_coders_info(task, p, end, arch, &num_folders,
  1223. &unknown_digests);
  1224. break;
  1225. case kSubStreamsInfo:
  1226. p = rspamd_7zip_read_substreams_info(task, p, end, arch, num_folders,
  1227. unknown_digests);
  1228. break;
  1229. break;
  1230. case kEnd:
  1231. goto end;
  1232. break;
  1233. default:
  1234. p = NULL;
  1235. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1236. goto end;
  1237. break;
  1238. }
  1239. }
  1240. end:
  1241. return p;
  1242. }
  1243. static const unsigned char *
  1244. rspamd_7zip_read_archive_props(struct rspamd_task *task,
  1245. const unsigned char *p, const unsigned char *end,
  1246. struct rspamd_archive *arch)
  1247. {
  1248. unsigned char proptype;
  1249. uint64_t proplen;
  1250. /*
  1251. * for (;;)
  1252. * {
  1253. * BYTE PropertyType;
  1254. * if (aType == 0)
  1255. * break;
  1256. * UINT64 PropertySize;
  1257. * BYTE PropertyData[PropertySize];
  1258. * }
  1259. */
  1260. if (p != NULL) {
  1261. proptype = *p;
  1262. SZ_SKIP_BYTES(1);
  1263. while (proptype != 0) {
  1264. SZ_READ_VINT(proplen);
  1265. if (p + proplen < end) {
  1266. p += proplen;
  1267. }
  1268. else {
  1269. return NULL;
  1270. }
  1271. proptype = *p;
  1272. SZ_SKIP_BYTES(1);
  1273. }
  1274. }
  1275. return p;
  1276. }
  1277. static GString *
  1278. rspamd_7zip_ucs2_to_utf8(struct rspamd_task *task, const unsigned char *p,
  1279. const unsigned char *end)
  1280. {
  1281. GString *res;
  1282. goffset dest_pos = 0, src_pos = 0;
  1283. const gsize len = (end - p) / sizeof(uint16_t);
  1284. uint16_t *up;
  1285. UChar32 wc;
  1286. UBool is_error = 0;
  1287. res = g_string_sized_new((end - p) * 3 / 2 + sizeof(wc) + 1);
  1288. up = (uint16_t *) p;
  1289. while (src_pos < len) {
  1290. U16_NEXT(up, src_pos, len, wc);
  1291. if (wc > 0) {
  1292. U8_APPEND(res->str, dest_pos,
  1293. res->allocated_len - 1,
  1294. wc, is_error);
  1295. }
  1296. if (is_error) {
  1297. g_string_free(res, TRUE);
  1298. return NULL;
  1299. }
  1300. }
  1301. g_assert(dest_pos < res->allocated_len);
  1302. res->len = dest_pos;
  1303. res->str[dest_pos] = '\0';
  1304. return res;
  1305. }
  1306. static const unsigned char *
  1307. rspamd_7zip_read_files_info(struct rspamd_task *task,
  1308. const unsigned char *p, const unsigned char *end,
  1309. struct rspamd_archive *arch)
  1310. {
  1311. uint64_t nfiles = 0, sz, i;
  1312. unsigned char t, b;
  1313. struct rspamd_archive_file *fentry;
  1314. SZ_READ_VINT(nfiles);
  1315. for (; p != NULL && p < end;) {
  1316. t = *p;
  1317. SZ_SKIP_BYTES(1);
  1318. msg_debug_archive("7zip: read file data type %xc", t);
  1319. if (t == kEnd) {
  1320. goto end;
  1321. }
  1322. /* This is SO SPECIAL, gah */
  1323. SZ_READ_VINT(sz);
  1324. switch (t) {
  1325. case kEmptyStream:
  1326. case kEmptyFile:
  1327. case kAnti: /* AntiFile, OMFG */
  1328. /* We don't care about these bits */
  1329. case kCTime:
  1330. case kATime:
  1331. case kMTime:
  1332. /* We don't care of these guys, but we still have to parse them, gah */
  1333. if (sz > 0) {
  1334. SZ_SKIP_BYTES(sz);
  1335. }
  1336. break;
  1337. case kName:
  1338. /* The most useful part in this whole bloody format */
  1339. b = *p; /* External flag */
  1340. SZ_SKIP_BYTES(1);
  1341. if (b) {
  1342. /* TODO: for the god sake, do something about external
  1343. * filenames...
  1344. */
  1345. uint64_t tmp;
  1346. SZ_READ_VINT(tmp);
  1347. }
  1348. else {
  1349. for (i = 0; i < nfiles; i++) {
  1350. /* Zero terminated wchar_t: happy converting... */
  1351. /* First, find terminator */
  1352. const unsigned char *fend = NULL, *tp = p;
  1353. GString *res;
  1354. while (tp < end - 1) {
  1355. if (*tp == 0 && *(tp + 1) == 0) {
  1356. fend = tp;
  1357. break;
  1358. }
  1359. tp += 2;
  1360. }
  1361. if (fend == NULL || fend - p == 0) {
  1362. /* Crap instead of fname */
  1363. msg_debug_archive("bad 7zip name; %s", G_STRLOC);
  1364. goto end;
  1365. }
  1366. res = rspamd_7zip_ucs2_to_utf8(task, p, fend);
  1367. if (res != NULL) {
  1368. fentry = g_malloc0(sizeof(*fentry));
  1369. fentry->fname = res;
  1370. g_ptr_array_add(arch->files, fentry);
  1371. msg_debug_archive("7zip: found file %v", res);
  1372. }
  1373. else {
  1374. msg_debug_archive("bad 7zip name; %s", G_STRLOC);
  1375. }
  1376. /* Skip zero terminating character */
  1377. p = fend + 2;
  1378. }
  1379. }
  1380. break;
  1381. case kDummy:
  1382. case kWinAttributes:
  1383. if (sz > 0) {
  1384. SZ_SKIP_BYTES(sz);
  1385. }
  1386. break;
  1387. default:
  1388. p = NULL;
  1389. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1390. goto end;
  1391. break;
  1392. }
  1393. }
  1394. end:
  1395. return p;
  1396. }
  1397. static const unsigned char *
  1398. rspamd_7zip_read_next_section(struct rspamd_task *task,
  1399. const unsigned char *p, const unsigned char *end,
  1400. struct rspamd_archive *arch,
  1401. struct rspamd_mime_part *part)
  1402. {
  1403. unsigned char t = *p;
  1404. SZ_SKIP_BYTES(1);
  1405. msg_debug_archive("7zip: read section %xc", t);
  1406. switch (t) {
  1407. case kHeader:
  1408. /* We just skip byte and go further */
  1409. break;
  1410. case kEncodedHeader:
  1411. /*
  1412. * In fact, headers are just packed, but we assume it as
  1413. * encrypted to distinguish from the normal archives
  1414. */
  1415. {
  1416. msg_debug_archive("7zip: encoded header, needs to be uncompressed");
  1417. struct archive *a = archive_read_new();
  1418. archive_read_support_format_7zip(a);
  1419. int r = archive_read_open_memory(a, part->parsed_data.begin, part->parsed_data.len);
  1420. if (r != ARCHIVE_OK) {
  1421. msg_debug_archive("7zip: cannot open memory archive: %s", archive_error_string(a));
  1422. archive_read_free(a);
  1423. return NULL;
  1424. }
  1425. /* Clean the existing files if any */
  1426. rspamd_archive_dtor(arch);
  1427. arch->files = g_ptr_array_new();
  1428. struct archive_entry *ae;
  1429. while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
  1430. const char *name = archive_entry_pathname_utf8(ae);
  1431. if (name) {
  1432. msg_debug_archive("7zip: found file %s", name);
  1433. struct rspamd_archive_file *f = g_malloc0(sizeof(*f));
  1434. f->fname = g_string_new(name);
  1435. g_ptr_array_add(arch->files, f);
  1436. }
  1437. archive_read_data_skip(a);
  1438. }
  1439. if (archive_read_has_encrypted_entries(a) > 0) {
  1440. msg_debug_archive("7zip: found encrypted stuff");
  1441. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  1442. }
  1443. archive_read_free(a);
  1444. p = NULL; /* Stop internal processor, as we rely on libarchive here */
  1445. break;
  1446. }
  1447. case kArchiveProperties:
  1448. p = rspamd_7zip_read_archive_props(task, p, end, arch);
  1449. break;
  1450. case kMainStreamsInfo:
  1451. p = rspamd_7zip_read_main_streams_info(task, p, end, arch);
  1452. break;
  1453. case kAdditionalStreamsInfo:
  1454. p = rspamd_7zip_read_main_streams_info(task, p, end, arch);
  1455. break;
  1456. case kFilesInfo:
  1457. p = rspamd_7zip_read_files_info(task, p, end, arch);
  1458. break;
  1459. case kEnd:
  1460. p = NULL;
  1461. msg_debug_archive("7zip: read final section");
  1462. break;
  1463. default:
  1464. p = NULL;
  1465. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1466. break;
  1467. }
  1468. return p;
  1469. }
  1470. static void
  1471. rspamd_archive_process_7zip(struct rspamd_task *task,
  1472. struct rspamd_mime_part *part)
  1473. {
  1474. struct rspamd_archive *arch;
  1475. const unsigned char *start, *p, *end;
  1476. const unsigned char sz_magic[] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
  1477. uint64_t section_offset = 0, section_length = 0;
  1478. start = part->parsed_data.begin;
  1479. p = start;
  1480. end = p + part->parsed_data.len;
  1481. if (end - p <= sizeof(uint64_t) + sizeof(uint32_t) ||
  1482. memcmp(p, sz_magic, sizeof(sz_magic)) != 0) {
  1483. msg_debug_archive("7z archive is invalid (no 7z magic)");
  1484. return;
  1485. }
  1486. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  1487. arch->files = g_ptr_array_new();
  1488. arch->type = RSPAMD_ARCHIVE_7ZIP;
  1489. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  1490. arch);
  1491. /* Magic (6 bytes) + version (2 bytes) + crc32 (4 bytes) */
  1492. p += sizeof(uint64_t) + sizeof(uint32_t);
  1493. SZ_READ_UINT64(section_offset);
  1494. SZ_READ_UINT64(section_length);
  1495. if (end - p > sizeof(uint32_t)) {
  1496. p += sizeof(uint32_t);
  1497. }
  1498. else {
  1499. msg_debug_archive("7z archive is invalid (truncated crc)");
  1500. return;
  1501. }
  1502. if (end - p > section_offset) {
  1503. p += section_offset;
  1504. }
  1505. else {
  1506. msg_debug_archive("7z archive is invalid (incorrect section offset)");
  1507. return;
  1508. }
  1509. while ((p = rspamd_7zip_read_next_section(task, p, end, arch, part)) != NULL)
  1510. ;
  1511. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  1512. part->specific.arch = arch;
  1513. if (part->cd != NULL) {
  1514. arch->archive_name = &part->cd->filename;
  1515. }
  1516. arch->size = part->parsed_data.len;
  1517. }
  1518. static void
  1519. rspamd_archive_process_gzip(struct rspamd_task *task,
  1520. struct rspamd_mime_part *part)
  1521. {
  1522. struct rspamd_archive *arch;
  1523. const unsigned char *start, *p, *end;
  1524. const unsigned char gz_magic[] = {0x1F, 0x8B};
  1525. unsigned char flags;
  1526. start = part->parsed_data.begin;
  1527. p = start;
  1528. end = p + part->parsed_data.len;
  1529. if (end - p <= 10 || memcmp(p, gz_magic, sizeof(gz_magic)) != 0) {
  1530. msg_debug_archive("gzip archive is invalid (no gzip magic)");
  1531. return;
  1532. }
  1533. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  1534. arch->files = g_ptr_array_sized_new(1);
  1535. arch->type = RSPAMD_ARCHIVE_GZIP;
  1536. if (part->cd) {
  1537. arch->archive_name = &part->cd->filename;
  1538. }
  1539. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  1540. arch);
  1541. flags = p[3];
  1542. if (flags & (1u << 5)) {
  1543. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  1544. }
  1545. if (flags & (1u << 3)) {
  1546. /* We have file name presented in archive, try to use it */
  1547. if (flags & (1u << 1)) {
  1548. /* Multipart */
  1549. p += 12;
  1550. }
  1551. else {
  1552. p += 10;
  1553. }
  1554. if (flags & (1u << 2)) {
  1555. /* Optional section */
  1556. uint16_t optlen = 0;
  1557. RAR_READ_UINT16(optlen);
  1558. if (end <= p + optlen) {
  1559. msg_debug_archive("gzip archive is invalid, bad extra length: %d",
  1560. (int) optlen);
  1561. return;
  1562. }
  1563. p += optlen;
  1564. }
  1565. /* Read file name */
  1566. const unsigned char *fname_start = p;
  1567. while (p < end) {
  1568. if (*p == '\0') {
  1569. if (p > fname_start) {
  1570. struct rspamd_archive_file *f;
  1571. f = g_malloc0(sizeof(*f));
  1572. rspamd_archive_file_try_utf(task, arch, f,
  1573. fname_start, p - fname_start);
  1574. if (f->fname) {
  1575. g_ptr_array_add(arch->files, f);
  1576. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  1577. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  1578. }
  1579. }
  1580. else {
  1581. /* Invalid filename, skip */
  1582. g_free(f);
  1583. }
  1584. goto set;
  1585. }
  1586. }
  1587. p++;
  1588. }
  1589. /* Wrong filename, not zero terminated */
  1590. msg_debug_archive("gzip archive is invalid, bad filename at pos %d",
  1591. (int) (p - start));
  1592. return;
  1593. }
  1594. /* Fallback, we need to extract file name from archive name if possible */
  1595. if (part->cd && part->cd->filename.len > 0) {
  1596. const char *dot_pos, *slash_pos;
  1597. dot_pos = rspamd_memrchr(part->cd->filename.begin, '.',
  1598. part->cd->filename.len);
  1599. if (dot_pos) {
  1600. struct rspamd_archive_file *f;
  1601. slash_pos = rspamd_memrchr(part->cd->filename.begin, '/',
  1602. part->cd->filename.len);
  1603. if (slash_pos && slash_pos < dot_pos) {
  1604. f = g_malloc0(sizeof(*f));
  1605. f->fname = g_string_sized_new(dot_pos - slash_pos);
  1606. g_string_append_len(f->fname, slash_pos + 1,
  1607. dot_pos - slash_pos - 1);
  1608. msg_debug_archive("fallback to gzip filename based on cd: %v",
  1609. f->fname);
  1610. g_ptr_array_add(arch->files, f);
  1611. goto set;
  1612. }
  1613. else {
  1614. const char *fname_start = part->cd->filename.begin;
  1615. f = g_malloc0(sizeof(*f));
  1616. if (memchr(fname_start, '.', part->cd->filename.len) != dot_pos) {
  1617. /* Double dots, something like foo.exe.gz */
  1618. f->fname = g_string_sized_new(dot_pos - fname_start);
  1619. g_string_append_len(f->fname, fname_start,
  1620. dot_pos - fname_start);
  1621. }
  1622. else {
  1623. /* Single dot, something like foo.gzz */
  1624. f->fname = g_string_sized_new(part->cd->filename.len);
  1625. g_string_append_len(f->fname, fname_start,
  1626. part->cd->filename.len);
  1627. }
  1628. msg_debug_archive("fallback to gzip filename based on cd: %v",
  1629. f->fname);
  1630. g_ptr_array_add(arch->files, f);
  1631. goto set;
  1632. }
  1633. }
  1634. }
  1635. return;
  1636. set:
  1637. /* Set archive data */
  1638. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  1639. part->specific.arch = arch;
  1640. arch->size = part->parsed_data.len;
  1641. }
  1642. static gboolean
  1643. rspamd_archive_cheat_detect(struct rspamd_mime_part *part, const char *str,
  1644. const unsigned char *magic_start, gsize magic_len)
  1645. {
  1646. struct rspamd_content_type *ct;
  1647. const char *p;
  1648. rspamd_ftok_t srch, *fname;
  1649. ct = part->ct;
  1650. RSPAMD_FTOK_ASSIGN(&srch, "application");
  1651. if (ct && ct->type.len && ct->subtype.len > 0 && rspamd_ftok_cmp(&ct->type, &srch) == 0) {
  1652. if (rspamd_substring_search_caseless(ct->subtype.begin, ct->subtype.len,
  1653. str, strlen(str)) != -1) {
  1654. /* We still need to check magic, see #1848 */
  1655. if (magic_start != NULL) {
  1656. if (part->parsed_data.len > magic_len &&
  1657. memcmp(part->parsed_data.begin,
  1658. magic_start, magic_len) == 0) {
  1659. return TRUE;
  1660. }
  1661. /* No magic, refuse this type of archive */
  1662. return FALSE;
  1663. }
  1664. else {
  1665. return TRUE;
  1666. }
  1667. }
  1668. }
  1669. if (part->cd) {
  1670. fname = &part->cd->filename;
  1671. if (fname && fname->len > strlen(str)) {
  1672. p = fname->begin + fname->len - strlen(str);
  1673. if (rspamd_lc_cmp(p, str, strlen(str)) == 0) {
  1674. if (*(p - 1) == '.') {
  1675. if (magic_start != NULL) {
  1676. if (part->parsed_data.len > magic_len &&
  1677. memcmp(part->parsed_data.begin,
  1678. magic_start, magic_len) == 0) {
  1679. return TRUE;
  1680. }
  1681. /* No magic, refuse this type of archive */
  1682. return FALSE;
  1683. }
  1684. return TRUE;
  1685. }
  1686. }
  1687. }
  1688. if (magic_start != NULL) {
  1689. if (part->parsed_data.len > magic_len &&
  1690. memcmp(part->parsed_data.begin, magic_start, magic_len) == 0) {
  1691. return TRUE;
  1692. }
  1693. }
  1694. }
  1695. else {
  1696. if (magic_start != NULL) {
  1697. if (part->parsed_data.len > magic_len &&
  1698. memcmp(part->parsed_data.begin, magic_start, magic_len) == 0) {
  1699. return TRUE;
  1700. }
  1701. }
  1702. }
  1703. return FALSE;
  1704. }
  1705. void rspamd_archives_process(struct rspamd_task *task)
  1706. {
  1707. unsigned int i;
  1708. struct rspamd_mime_part *part;
  1709. const unsigned char rar_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07};
  1710. const unsigned char zip_magic[] = {0x50, 0x4b, 0x03, 0x04};
  1711. const unsigned char sz_magic[] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
  1712. const unsigned char gz_magic[] = {0x1F, 0x8B, 0x08};
  1713. PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part)
  1714. {
  1715. if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) {
  1716. if (part->parsed_data.len > 0) {
  1717. if (rspamd_archive_cheat_detect(part, "zip",
  1718. zip_magic, sizeof(zip_magic))) {
  1719. rspamd_archive_process_zip(task, part);
  1720. }
  1721. else if (rspamd_archive_cheat_detect(part, "rar",
  1722. rar_magic, sizeof(rar_magic))) {
  1723. rspamd_archive_process_rar(task, part);
  1724. }
  1725. else if (rspamd_archive_cheat_detect(part, "7z",
  1726. sz_magic, sizeof(sz_magic))) {
  1727. rspamd_archive_process_7zip(task, part);
  1728. }
  1729. else if (rspamd_archive_cheat_detect(part, "gz",
  1730. gz_magic, sizeof(gz_magic))) {
  1731. rspamd_archive_process_gzip(task, part);
  1732. }
  1733. if (part->ct && (part->ct->flags & RSPAMD_CONTENT_TYPE_TEXT) &&
  1734. part->part_type == RSPAMD_MIME_PART_ARCHIVE &&
  1735. part->specific.arch) {
  1736. struct rspamd_archive *arch = part->specific.arch;
  1737. msg_info_task("found %s archive with incorrect content-type: %T/%T",
  1738. rspamd_archive_type_str(arch->type),
  1739. &part->ct->type, &part->ct->subtype);
  1740. if (!(part->ct->flags & RSPAMD_CONTENT_TYPE_MISSING)) {
  1741. part->ct->flags |= RSPAMD_CONTENT_TYPE_BROKEN;
  1742. }
  1743. }
  1744. }
  1745. }
  1746. }
  1747. }
  1748. const char *
  1749. rspamd_archive_type_str(enum rspamd_archive_type type)
  1750. {
  1751. const char *ret = "unknown";
  1752. switch (type) {
  1753. case RSPAMD_ARCHIVE_ZIP:
  1754. ret = "zip";
  1755. break;
  1756. case RSPAMD_ARCHIVE_RAR:
  1757. ret = "rar";
  1758. break;
  1759. case RSPAMD_ARCHIVE_7ZIP:
  1760. ret = "7z";
  1761. break;
  1762. case RSPAMD_ARCHIVE_GZIP:
  1763. ret = "gz";
  1764. break;
  1765. }
  1766. return ret;
  1767. }