Track only LFH offsets and init zip struct

This commit is contained in:
atxr 2024-02-21 17:08:57 +01:00
parent 812ff2fe3e
commit 090e17b3d9
5 changed files with 70 additions and 59 deletions

View file

@ -82,19 +82,20 @@ typedef struct DHCH
typedef struct zip
{
// compression type
char* start;
unsigned int size;
char* cd;
CDH** cdh;
LFH** lfh;
unsigned int* lfh_off;
unsigned int entries;
EOCD* eocd;
} zip;
void get_eocd(char* data, int size, zip* out);
void get_cdh(char* data, zip* out);
zip init_zip(char* data, int size);
void get_eocd(zip* out);
void get_cdh(zip* out);
char* get_encoded_block(zip* in, int n);
void parse_zip(char* filename, zip* out);
void deflate(zip* in);

View file

@ -1,5 +1,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libmineziper.h"
@ -12,9 +14,10 @@ int get_uncompressed_size(zip* in)
{
int size = 0;
for (int i = 0; i < in->eocd->number_of_entries; i++)
for (int i = 0; i < in->entries; i++)
{
size += in->cdh[i]->uncompressed_size; // TODO overflow
LFH* lfh = &in->start[in->lfh_off[i]];
size += lfh->uncompressed_size;
}
return size;
@ -22,13 +25,11 @@ int get_uncompressed_size(zip* in)
bool scan_zip(char* zip_data, int zip_size)
{
zip zip;
get_eocd(zip_data, zip_size, &zip);
get_cdh(zip_data, &zip);
zip zip = init_zip(zip_data, zip_size);
for (int i = 0; i < zip.entries; i++)
{
LFH* lfh = &zip_data[zip.lfh_off[i]];
LFH* lfh = &zip.start[zip.lfh_off[i]];
if (lfh->compression_method == DEFLATE)
{
@ -72,8 +73,7 @@ bool scan_zip(char* zip_data, int zip_size)
fprintf(stderr, "[FILE %d] Error in compressed data\n", i);
}
#include <string.h>
if (strcmp("VIRUS", decoded_data) == NULL)
if (strcmp("VIRUS", decoded_data) == 0)
{
printf("-> VIRUS FOUND\n");
return true;

View file

@ -5,28 +5,36 @@
#include "libmineziper_huffman_tree.h"
#include "libmineziper_zip.h"
void get_eocd(char* data, int size, zip* out)
zip init_zip(char* data, int size)
{
if (size < START_EOCD_SEARCH)
zip z = {.start = data, .size = size};
get_eocd(&z);
get_cdh(&z);
return z;
}
void get_eocd(zip* z)
{
if (z->size < START_EOCD_SEARCH)
return;
char* se = &data[size - START_EOCD_SEARCH];
while (se > data)
char* se = &z->start[z->size - START_EOCD_SEARCH];
while (se > z->start)
{
if (strcmp(se, EOCD_SIG) == 0)
{
out->eocd = (EOCD*) se;
out->entries = out->eocd->number_of_entries;
z->eocd = (EOCD*) se;
z->entries = z->eocd->number_of_entries;
out->cdh = (CDH**) malloc(out->entries * sizeof(CDH*));
out->lfh = (LFH**) malloc(out->entries * sizeof(LFH*));
out->lfh_off = malloc(out->entries * sizeof(int));
z->lfh_off = malloc(z->entries * sizeof(int));
if (!out->cdh || !out->lfh || !out->lfh_off)
if (!z->lfh_off)
{
printf(
"[ERROR] Failed to allocate CDH/LFH buffer for %d entries\n",
out->entries);
z->entries);
exit(1);
}
@ -37,23 +45,20 @@ void get_eocd(char* data, int size, zip* out)
}
}
void get_cdh(char* data, zip* out)
void get_cdh(zip* z)
{
if (out->eocd == 0 || out->eocd->off_cdh == 0)
if (z->eocd == 0 || z->eocd->off_cdh == 0)
{
printf("<get_cdh> error: No EOCD found.\n");
fprintf(stderr, "[ERROR]: No EOCD found when fetching CDH.\n");
exit(-1);
}
out->cd = data + out->eocd->off_cdh;
z->cd = z->start + z->eocd->off_cdh;
CDH* cdh = (CDH*) out->cd;
for (int i = 0; i < out->eocd->number_of_entries; i++)
CDH* cdh = (CDH*) z->cd;
for (int i = 0; i < z->eocd->number_of_entries; i++)
{
out->cdh[i] = cdh;
out->lfh[i] = (LFH*) (data + cdh->off_lfh);
out->lfh_off[i] = cdh->off_lfh;
z->lfh_off[i] = cdh->off_lfh;
cdh = (CDH*) (((char*) cdh) + sizeof(CDH) + cdh->filename_length +
cdh->extraf_length + cdh->file_comment_length);
@ -62,8 +67,9 @@ void get_cdh(char* data, zip* out)
char* get_encoded_block(zip* in, int n)
{
return (char*) (in->lfh[n]) + sizeof(LFH) + in->lfh[n]->filename_length +
in->lfh[n]->extraf_length;
LFH* lfh = &in->start[in->lfh_off[n]];
return in->start + in->lfh_off[n] + sizeof(LFH) + lfh->filename_length +
lfh->extraf_length;
}
char* decode_type1_block_vuln(bitstream* bs, char* decoded_data)