|
| 1 | +# File comment contents |
| 2 | + |
| 3 | +The ZIP specification does not specify what the contents of a file |
| 4 | +comment can be. Intuitively it makes sense to assume that it should be text |
| 5 | +but it hasn't been defined. In fact, the Python `zipfile` module documentation |
| 6 | +says: |
| 7 | + |
| 8 | +``` |
| 9 | +ZipInfo.comment |
| 10 | + Comment for the individual archive member as a bytes object. |
| 11 | +``` |
| 12 | + |
| 13 | +Because it is a bytes object it basically means that there are no restrictions |
| 14 | +on the *contents* of the file comment itself and any kind of data is accepted |
| 15 | +when assembling a ZIP file using Python. For example, embedding a small PNG |
| 16 | +as a file comment is absolutely no problem at all: |
| 17 | + |
| 18 | +``` |
| 19 | +>>> import zipfile |
| 20 | +>>> z = zipfile.ZipInfo(40*'a') |
| 21 | +>>> test_image = open('blue.png', 'rb').read() |
| 22 | +>>> len(test_image) |
| 23 | +162 |
| 24 | +>>> z.comment = test_image |
| 25 | +>>> contents = 10*b'c' |
| 26 | +>>> bla = zipfile.ZipFile('binary_comment.zip', mode='w') |
| 27 | +>>> bla.writestr(z, contents) |
| 28 | +>>> bla.close() |
| 29 | +``` |
| 30 | + |
| 31 | +When expecting the file with `hexdump` it is very easy to see that there |
| 32 | +is a PNG file embedded in the file comment: |
| 33 | + |
| 34 | +``` |
| 35 | +$ hexdump -C binary_comment.zip | grep PNG |
| 36 | +000000a0 61 61 61 61 61 61 89 50 4e 47 0d 0a 1a 0a 00 00 |aaaaaa.PNG......| |
| 37 | +``` |
| 38 | + |
| 39 | +`zipinfo` tries to display the content when run in verbose mode, but cannot: |
| 40 | + |
| 41 | +``` |
| 42 | +$ zipinfo -v binary_comment.zip |
| 43 | +Archive: binary_comment.zip |
| 44 | +There is no zipfile comment. |
| 45 | + |
| 46 | +End-of-central-directory record: |
| 47 | +------------------------------- |
| 48 | + |
| 49 | + Zip archive file size: 350 (000000000000015Eh) |
| 50 | + Actual end-cent-dir record offset: 328 (0000000000000148h) |
| 51 | + Expected end-cent-dir record offset: 328 (0000000000000148h) |
| 52 | + (based on the length of the central directory and its expected offset) |
| 53 | + |
| 54 | + This zipfile constitutes the sole disk of a single-part archive; its |
| 55 | + central directory contains 1 entry. |
| 56 | + The central directory is 248 (00000000000000F8h) bytes long, |
| 57 | + and its (expected) offset in bytes from the beginning of the zipfile |
| 58 | + is 80 (0000000000000050h). |
| 59 | + |
| 60 | + |
| 61 | +Central directory entry #1: |
| 62 | +--------------------------- |
| 63 | + |
| 64 | + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 65 | + |
| 66 | + offset of local header from start of archive: 0 |
| 67 | + (0000000000000000h) bytes |
| 68 | + file system or operating system of origin: Unix |
| 69 | + version of encoding software: 2.0 |
| 70 | + minimum file system compatibility required: MS-DOS, OS/2 or NT FAT |
| 71 | + minimum software version required to extract: 2.0 |
| 72 | + compression method: none (stored) |
| 73 | + file security status: not encrypted |
| 74 | + extended local header: no |
| 75 | + file last modified on (DOS date/time): 1980 Jan 1 00:00:00 |
| 76 | + 32-bit CRC value (hex): f115ce3f |
| 77 | + compressed size: 10 bytes |
| 78 | + uncompressed size: 10 bytes |
| 79 | + length of filename: 40 characters |
| 80 | + length of extra field: 0 bytes |
| 81 | + length of file comment: 162 characters |
| 82 | + disk number on which file begins: disk 1 |
| 83 | + apparent file type: binary |
| 84 | + Unix file attributes (000600 octal): ?rw------- |
| 85 | + MS-DOS file attributes (00 hex): none |
| 86 | + |
| 87 | +------------------------- file comment begins ---------------------------- |
| 88 | +�PNG |
| 89 | +� |
| 90 | +-------------------------- file comment ends ----------------------------- |
| 91 | +``` |
| 92 | + |
| 93 | +This would allow someone to hide information in the ZIP file that is not |
| 94 | +easy to extract unless the ZIP file is parsed in a particular way (and not |
| 95 | +with regular unpacking tools). |
0 commit comments