diff --git a/lib/cabfile.h b/lib/cabfile.h index 95f68b74..8fb0ae26 100644 --- a/lib/cabfile.h +++ b/lib/cabfile.h @@ -72,6 +72,8 @@ typedef struct uint64_t data_offset; uint8_t md5[16]; uint16_t volume; + uint16_t dos_date; + uint16_t dos_time; uint32_t link_previous; uint32_t link_next; uint8_t link_flags; diff --git a/lib/file.c b/lib/file.c index e39f1e26..74ea25b9 100644 --- a/lib/file.c +++ b/lib/file.c @@ -64,7 +64,12 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind fd->expanded_size = READ_UINT32(p); p += 4; fd->compressed_size = READ_UINT32(p); p += 4; - p += 0x14; + + p += 4; + fd->dos_date = (uint16_t)READ_UINT32(p); p += 4; + fd->dos_time = (uint16_t)READ_UINT32(p); p += 4; + p += 8; + fd->data_offset = READ_UINT32(p); p += 4; #if VERBOSE >= 2 @@ -73,6 +78,8 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind unshield_trace("Flags: %04x", fd->flags); unshield_trace("Expanded size: %08x", fd->expanded_size); unshield_trace("Compressed size: %08x", fd->compressed_size); + unshield_trace("DOS date: %04x", fd->dos_date); + unshield_trace("DOS time: %04x", fd->dos_time); unshield_trace("Data offset: %08x", fd->data_offset); #endif @@ -113,8 +120,12 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind UNSHIELD_ASSERT(fd->directory_index < unshield_directory_count(unshield)); UNSHIELD_ASSERT((p - saved_p) == 0x40); - - p += 0xc; + + p += 4; + fd->dos_date = READ_UINT16(p); p += 2; + fd->dos_time = READ_UINT16(p); p += 2; + + p += 4; fd->link_previous = READ_UINT32(p); p += 4; fd->link_next = READ_UINT32(p); p += 4; fd->link_flags = *p; p ++; @@ -955,6 +966,40 @@ size_t unshield_file_size(Unshield* unshield, int index)/*{{{*/ return 0; }/*}}}*/ +/* This uses the ISO 8601 date format (except for milliseconds) */ +bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size)/*{{{*/ +{ + FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); + struct tm date; + + if (size < 32) + { + unshield_error("Buffer size %" SIZE_FORMAT " is too small", size); + return false; + } + + buf[0] = '\0'; + if (!fd || !unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date)) + return false; + + return strftime(buf, size, "%Y-%m-%d %H:%M:%S", &date) != 0; +}/*}}}*/ + +time_t unshield_file_timestamp (Unshield* unshield, int index)/*{{{*/ +{ + FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); + struct tm date; + + if (fd && unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date)) + { + time_t timestamp = mktime(&date); + if (timestamp != (time_t)-1) + return timestamp; + } + + return 0; +}/*}}}*/ + uint64_t unshield_file_size_compressed(Unshield* unshield, int index)/*{{{*/ { FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); diff --git a/lib/helper.c b/lib/helper.c index 9b816aaa..45d5dfba 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -266,4 +266,31 @@ const char* unshield_header_get_string(Header* header, uint32_t offset) return unshield_get_utf8_string(header, unshield_header_get_buffer(header, offset)); } +/** + Based on MIT Wine code and DosDateTimeToFileTime() MSDN documentation + */ +bool unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm) +{ + static const int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int year, days; + + memset(tm, 0, sizeof(*tm)); + tm->tm_isdst = -1; + tm->tm_year = (dos_date >> 9) + 80; + tm->tm_mon = ((dos_date >> 5) & 0x0f) - 1; + tm->tm_mday = dos_date & 0x1f; + tm->tm_hour = dos_time >> 11; + tm->tm_min = (dos_time >> 5) & 0x3f; + tm->tm_sec = (dos_time & 0x1f) * 2; + + if (tm->tm_mon < 0 || tm->tm_mon > 11 || + tm->tm_hour > 23 || tm->tm_min > 59 || tm->tm_sec > 59) + return false; + + year = tm->tm_year + 1900; + days = month_days[tm->tm_mon]; + if (tm->tm_mon == 1 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + days++; + return tm->tm_mday >= 1 && tm->tm_mday <= days; +} diff --git a/lib/internal.h b/lib/internal.h index f9d091d4..14b70bb7 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -87,6 +87,8 @@ const char* unshield_header_get_string(Header* header, uint32_t offset); uint8_t* unshield_header_get_buffer(Header* header, uint32_t offset); char *unshield_get_last_path_separator(char *path); +bool unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm); + static inline void* unshield_fopen(Unshield* unshield, const char *filename, const char *modes) { return unshield->io_callbacks->fopen(filename, modes, unshield->io_userdata); diff --git a/lib/libunshield.h b/lib/libunshield.h index ade96f44..37a2e98c 100644 --- a/lib/libunshield.h +++ b/lib/libunshield.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #define UNSHIELD_LOG_LEVEL_LOWEST 0 @@ -134,6 +135,9 @@ UNSHIELD_API bool unshield_file_is_valid (Unshield* unshield, int in UNSHIELD_API bool unshield_file_save (Unshield* unshield, int index, const char* filename); UNSHIELD_API int unshield_file_directory (Unshield* unshield, int index); UNSHIELD_API size_t unshield_file_size (Unshield* unshield, int index); +UNSHIELD_API bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size); +/* Returns 0 if the date is missing, invalid, or cannot be converted to time_t. */ +UNSHIELD_API time_t unshield_file_timestamp (Unshield* unshield, int index); UNSHIELD_API uint64_t unshield_file_size_compressed(Unshield* unshield, int index); /** Raw file flags (UNSHIELD_FILE_* bits), or 0 if the index is invalid. */ UNSHIELD_API uint16_t unshield_file_flags_raw (Unshield* unshield, int index); diff --git a/man/unshield.1 b/man/unshield.1 index 284dc67d..6476fda6 100644 --- a/man/unshield.1 +++ b/man/unshield.1 @@ -1,8 +1,8 @@ -.TH UNSHIELD "1" "November 2023" "The Unshield project" "https://github.com/twogood/unshield" +.TH UNSHIELD "1" "January 2026" "The Unshield project" "https://github.com/twogood/unshield" .SH NAME unshield \- extract CAB files from an InstallShield installer archive .SH SYNOPSIS -unshield [\-c COMPONENT] [\-d DIRECTORY] [\-D LEVEL] [\-g GROUP] [\-h] [\-i VERSION] [\-e ENCODING] [\-j] [\-L] [\-O] [\-r] [\-R] [\-V] c|g|l|t|x CABFILE [FILENAME...] +unshield [\-c COMPONENT] [\-d DIRECTORY] [\-D LEVEL] [\-g GROUP] [\-h] [\-i VERSION] [\-e ENCODING] [\-j] [\-L] [\-O] [\-r] [\-R] [\-t] [\-V] c|g|l|t|x CABFILE [FILENAME...] .SH DESCRIPTION Unshield extracts CAB files from InstallShield installers, used to install software on Microsoft Windows based machines. @@ -50,6 +50,9 @@ Save raw data (do not decompress) \fB\-R\fR Don't do any conversion to file and directory names when extracting .TP +\fB\-t\fR +Set the modification date/time from the metadata when extracting +.TP \fB\-V\fR, \fB\-\-version\fR Print version information .SS "Commands:" diff --git a/src/unshield.c b/src/unshield.c index 6f2a3dbd..e5062822 100644 --- a/src/unshield.c +++ b/src/unshield.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "../lib/libunshield.h" #ifdef HAVE_CONFIG_H @@ -68,6 +69,7 @@ static const char* component_name = NULL; static bool junk_paths = false; static bool make_lowercase = false; static bool raw_filename = false; +static bool set_timestamp = false; static ACTION action = ACTION_EXTRACT; static int log_level = UNSHIELD_LOG_LEVEL_LOWEST; static int exit_status = 0; @@ -192,7 +194,7 @@ static void show_usage(const char* name) fprintf(stderr, "Syntax:\n" "\n" - "\t%s [-c COMPONENT] [-d DIRECTORY] [-D LEVEL] [-g GROUP] [-h] [-i VERSION] [-e ENCODING] [-j] [-L] [-O] [-r] [-R] [-V] c|g|l|t|x CABFILE [FILENAME...]\n" + "\t%s [-c COMPONENT] [-d DIRECTORY] [-D LEVEL] [-g GROUP] [-h] [-i VERSION] [-e ENCODING] [-j] [-L] [-O] [-r] [-R] [-t] [-V] c|g|l|t|x CABFILE [FILENAME...]\n" "\n" "Options:\n" "\t-c COMPONENT Only list/extract this component\n" @@ -211,6 +213,7 @@ static void show_usage(const char* name) "\t-O Use old compression\n" "\t-r Save raw data (do not decompress)\n" "\t-R Don't do any conversion to file and directory names when extracting\n" + "\t-t Set the modification date/time from the metadata when extracting\n" "\t-V --version Print copyright and version information\n" "\n" "Commands:\n" @@ -243,7 +246,7 @@ static bool handle_parameters( { NULL, 0, NULL, 0 } }; - while ((c = getopt_long(argc, argv, "c:d:D:g:hi:e:jLOrRV", long_options, NULL)) != -1) + while ((c = getopt_long(argc, argv, "c:d:D:g:hi:e:jLOrRtV", long_options, NULL)) != -1) { switch (c) { @@ -297,6 +300,10 @@ static bool handle_parameters( format = FORMAT_RAW; break; + case 't': + set_timestamp = true; + break; + case 'V': printf("Unshield version " VERSION ". MIT License. (C) 2003-2023 David Eriksson.\n"); exit(0); @@ -366,7 +373,8 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) { bool success = false; char* dirname; - char* filename; + const char* filename = unshield_file_name(unshield, index); + char* full_filename; char* p; int directory = unshield_file_directory(unshield, index); long int path_max; @@ -384,8 +392,9 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) real_output_directory = malloc(path_max); real_filename = malloc(path_max); dirname = malloc(path_max); - filename = malloc(path_max); - if (real_output_directory == NULL || real_filename == NULL) + full_filename = calloc(1, path_max); + if (real_output_directory == NULL || real_filename == NULL || + dirname == NULL || full_filename == NULL) { fprintf(stderr,"Unable to allocate memory."); success=false; @@ -486,10 +495,9 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) make_sure_directory_exists(dirname); - snprintf(filename, path_max, "%s%s", - dirname, unshield_file_name(unshield, index)); + snprintf(full_filename, path_max, "%s%s", dirname, filename); - for (p = filename + strlen(dirname); *p != '\0'; p++) + for (p = full_filename + strlen(dirname); *p != '\0'; p++) { if (!raw_filename) { @@ -501,7 +509,7 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) } #ifdef HAVE_ICONV - if (!convert_encoding(filename + strlen(dirname), + if (!convert_encoding(full_filename + strlen(dirname), path_max - strlen(dirname))) { success = false; @@ -514,7 +522,7 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) #pragma GCC diagnostic ignored "-Wunused-result" /* use GNU extension to return non-existing files to real_output_directory */ realpath(output_directory, real_output_directory); - realpath(filename, real_filename); + realpath(full_filename, real_filename); #pragma GCC diagnostic pop if (real_filename == NULL || strncmp(real_filename, real_output_directory, @@ -522,40 +530,70 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) { fprintf(stderr, "\n\nExtraction failed.\n"); fprintf(stderr, "Error: %s (%d).\n", strerror(errno), errno); - fprintf(stderr, "Possible directory traversal attack for: %s\n", filename); + fprintf(stderr, "Possible directory traversal attack for: %s\n", full_filename); fprintf(stderr, "To be placed at: %s\n\n", real_filename); success = false; goto exit; } #endif - printf(" extracting: %s\n", filename); + printf(" extracting: %s\n", full_filename); switch (format) { case FORMAT_NEW: - success = unshield_file_save(unshield, index, filename); + success = unshield_file_save(unshield, index, full_filename); break; case FORMAT_OLD: - success = unshield_file_save_old(unshield, index, filename); + success = unshield_file_save_old(unshield, index, full_filename); break; case FORMAT_RAW: - success = unshield_file_save_raw(unshield, index, filename); + success = unshield_file_save_raw(unshield, index, full_filename); break; } + if (success && set_timestamp) + { + time_t timestamp = unshield_file_timestamp(unshield, index); + int ret = 0, ret2 = 0; + struct utimbuf utim; + struct stat info; + + if (!timestamp || timestamp == (time_t)-1) + { + fprintf(stderr, "Failed to get timestamp for file '%s'.", filename); + } + else + { + memset(&utim, 0, sizeof(utim)); + memset(&info, 0, sizeof(info)); + + ret = stat(full_filename, &info); + if (ret == 0) + { + utim.actime = info.st_atime; + utim.modtime = timestamp; + ret2 = utime(full_filename, &utim); + } + + if (ret < 0 || ret2 < 0) + fprintf(stderr, "Failed to set timestamp for file '%s'.", filename); + } + } + exit: if (!success) { fprintf(stderr, "Failed to extract file '%s'.%s\n", - unshield_file_name(unshield, index), + filename, (log_level < 3) ? "Run unshield again with -D 3 for more information." : ""); - unlink(filename); + if (full_filename) + unlink(full_filename); exit_status = 1; } free(real_filename); free(real_output_directory); free(dirname); - free(filename); + free(full_filename); return success; } @@ -686,11 +724,15 @@ static int list_files_helper(Unshield* unshield, const char* prefix, int first, for (i = first; i <= last; i++) { char dirname[4096]; + char date[32]; if (unshield_file_is_valid(unshield, i) && should_process_file(unshield, i)) { valid_count++; + if (!unshield_file_iso_date(unshield, i, date, sizeof(date))) + strcpy(date, "-------------------"); + if (prefix && prefix[0]) { strcpy(dirname, prefix); @@ -711,10 +753,11 @@ static int list_files_helper(Unshield* unshield, const char* prefix, int first, if (dirname[strlen(dirname)-1] != '\\') strcat(dirname, "\\"); - printf(" %8" SIZE_FORMAT " %s%s\n", + printf(" %8" SIZE_FORMAT " %s %s%s\n", unshield_file_size(unshield, i), + date, dirname, - unshield_file_name(unshield, i)); + unshield_file_name(unshield, i)); } } @@ -751,7 +794,7 @@ static bool do_action(Unshield* unshield, ActionHelper helper) } } - printf(" -------- -------\n %i files\n", count); + printf(" -------- ------------------- -------\n %i files\n", count); return true; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a075212b..d6ad7c76 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,3 +12,13 @@ else() add_subdirectory(v5/CVE-2015-1386) endif() #add_subdirectory(v5/stack_memory_file) + +add_executable(test_timestamps timestamps.c) +target_link_libraries(test_timestamps libunshield) +add_test(NAME timestamps + COMMAND ${CMAKE_COMMAND} + -DUNSHIELD=$ + -DPROBE=$ + -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/timestamps + -P ${CMAKE_CURRENT_SOURCE_DIR}/timestamps.cmake) +set_tests_properties(timestamps PROPERTIES ENVIRONMENT "TZ=UTC0") diff --git a/test/timestamps.c b/test/timestamps.c new file mode 100644 index 00000000..70dc8dcc --- /dev/null +++ b/test/timestamps.c @@ -0,0 +1,107 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif +#include +#include +#include +#include +#include +#include +#include +#include "libunshield.h" + +static void put32(uint8_t* p, uint32_t value) +{ + for (int i = 0; i < 4; i++) + p[i] = (uint8_t)(value >> (8 * i)); +} + +static void write_file(const char* name, const uint8_t* data, size_t size) +{ + FILE* file = fopen(name, "wb"); + assert(file); + assert(fwrite(data, 1, size, file) == size); + assert(fclose(file) == 0); +} + +int main(int argc, char** argv) +{ + assert(argc >= 3); + if (strcmp(argv[1], "prepare") == 0) + { + /* Minimal IS5 archive: one uncompressed file in one file group. */ + uint8_t header[0x400] = {0}; + uint8_t cabinet[0x45] = {0}; + uint8_t* descriptor = header + 0x2a8; + Unshield* archive; + char date[32]; + assert(argc == 6); + put32(header, 0x28635349); + put32(header + 4, 0x01005000); + put32(header + 12, 0x10); + put32(header + 16, sizeof(header) - 0x10); + put32(header + 0x1c, 0x290); /* file table, relative to cabinet descriptor */ + put32(header + 0x24, 8); + put32(header + 0x28, 8); + put32(header + 0x2c, 1); /* directory count */ + put32(header + 0x38, 1); /* file count */ + put32(header + 0x4e, 0x350); /* file group offset list */ + put32(header + 0x2a0, 0x60); /* directory name */ + put32(header + 0x2a4, 8); /* file descriptor */ + put32(descriptor, 0x50); /* file name */ + put32(descriptor + 10, 5); + put32(descriptor + 14, 5); + put32(descriptor + 22, (uint32_t)strtoul(argv[2], NULL, 0)); + put32(descriptor + 26, (uint32_t)strtoul(argv[3], NULL, 0)); + put32(descriptor + 38, 0x40); /* data offset */ + memcpy(header + 0x2f0, "test.txt", 9); + memcpy(header + 0x300, "files", 6); + put32(header + 0x310, 0x370); /* group name */ + put32(header + 0x360, 0x370); + put32(header + 0x364, 0x300); /* group descriptor */ + memcpy(header + 0x380, "group", 6); + memcpy(cabinet, header, 20); + put32(cabinet + 36, 0x40); + put32(cabinet + 40, 5); + put32(cabinet + 44, 5); + put32(cabinet + 48, 0x40); + put32(cabinet + 52, 5); + put32(cabinet + 56, 5); + memcpy(cabinet + 0x40, "TEST", 5); + write_file("data1.hdr", header, sizeof(header)); + write_file("data1.cab", cabinet, sizeof(cabinet)); + archive = unshield_open("data1.cab"); + assert(archive); + if (strcmp(argv[4], "missing") == 0) + { + assert(!unshield_file_iso_date(archive, 0, date, sizeof(date))); + assert(date[0] == '\0'); + assert(unshield_file_timestamp(archive, 0) == 0); + } + else + { + assert(unshield_file_iso_date(archive, 0, date, sizeof(date))); + assert(strcmp(date, argv[4]) == 0); + assert(unshield_file_timestamp(archive, 0) == (time_t)strtoll(argv[5], NULL, 10)); + } + unshield_close(archive); + } + else + { + struct stat info; + char content[5]; + FILE* file = fopen(argv[2], "rb"); + assert(argc == 4); + assert(file); + assert(fread(content, 1, sizeof(content), file) == sizeof(content)); + assert(memcmp(content, "TEST", sizeof(content)) == 0); + assert(fclose(file) == 0); + assert(stat(argv[2], &info) == 0); + if (strcmp(argv[3], "current") != 0) + assert(info.st_mtime == (time_t)strtoll(argv[3], NULL, 10)); + else + assert(difftime(time(NULL), info.st_mtime) >= -2 && + difftime(time(NULL), info.st_mtime) < 60); + } + return 0; +} diff --git a/test/timestamps.cmake b/test/timestamps.cmake new file mode 100644 index 00000000..53f4a312 --- /dev/null +++ b/test/timestamps.cmake @@ -0,0 +1,73 @@ +file(MAKE_DIRECTORY "${TEST_DIR}") + +function(run) + execute_process(COMMAND ${ARGV} WORKING_DIRECTORY "${TEST_DIR}" + RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error) + if(NOT result STREQUAL "0") + message(FATAL_ERROR "${ARGV}: ${result}\n${output}\n${error}") + endif() + set(output "${output}" PARENT_SCOPE) +endfunction() + +# DOS date/time: 2024-06-15 12:34:56, zero date, invalid calendar/time fields. +foreach(case valid valid_midnight valid_leap missing missing_zero month day february leap_century hour minute second) + set(date 0x58cf) + set(time 0x645c) + set(expected missing) + set(timestamp 0) + if(case STREQUAL "valid") + set(expected "2024-06-15 12:34:56") + set(timestamp 1718454896) + elseif(case STREQUAL "valid_midnight") + set(time 0) + set(expected "2024-06-15 00:00:00") + set(timestamp 1718409600) + elseif(case STREQUAL "valid_leap") + set(date 0x585d) + set(expected "2024-02-29 12:34:56") + set(timestamp 1709210096) + elseif(case STREQUAL "missing") + set(date 0) + elseif(case STREQUAL "month") + set(date 0x59af) + elseif(case STREQUAL "day") + set(date 0x58c0) + elseif(case STREQUAL "february") + set(date 0x585e) + elseif(case STREQUAL "leap_century") + set(date 0xf05d) # 2100-02-29 is not a leap day. + elseif(case STREQUAL "hour") + set(time 0xc45c) + elseif(case STREQUAL "minute") + set(time 0x679c) + elseif(case STREQUAL "second") + set(time 0x645e) + else() + set(date 0) + set(time 0) + endif() + run("${PROBE}" prepare "${date}" "${time}" "${expected}" "${timestamp}") + run("${UNSHIELD}" -t -d "${case}" x data1.cab) + if(NOT expected STREQUAL "missing") + run("${PROBE}" check "${case}/group/files/test.txt" "${timestamp}") + run("${UNSHIELD}" -d default x data1.cab) + run("${PROBE}" check default/group/files/test.txt current) + else() + run("${PROBE}" check "${case}/group/files/test.txt" current) + endif() +endforeach() + +# Listing must not invent a date for invalid metadata. +run("${UNSHIELD}" l data1.cab) +if(NOT output MATCHES "-------------------") + message(FATAL_ERROR "Missing date not marked in listing: ${output}") +endif() + +# Fail before constructing the file path; diagnostics still need the file name. +string(REPEAT x 8192 long_directory) +execute_process(COMMAND "${UNSHIELD}" -d "${long_directory}" x data1.cab + WORKING_DIRECTORY "${TEST_DIR}" RESULT_VARIABLE result + OUTPUT_VARIABLE output ERROR_VARIABLE error) +if(NOT result STREQUAL "1" OR NOT error MATCHES "Failed to extract file 'test.txt'") + message(FATAL_ERROR "Early extraction failure: ${result}\n${error}") +endif()