[Help] Getamped kar file Extract

Extraction and unpacking of game archives and compression, encryption, obfuscation, decoding of unknown files
llaaccop2
Posts: 1
Joined: Tue Oct 26, 2021 7:05 am

[Help] Getamped kar file Extract

Post by llaaccop2 »

Code: Select all

# CyberStep (Cosmic Break) KAR format
# script for QuickBMS http://quickbms.aluigi.org

set MEMORY_FILE10 string "
unsigned int kar_gethash(const char* m_String, int dwLength)
{
  unsigned int dwHash = 0;
  for(int i = 0; i < dwLength; ++i)
  {
        dwHash = 31 * dwHash + m_String[i];
  }
 return dwHash;
}

static unsigned long mt_table[624];
static int mt_state = 0;

void mt_init(unsigned long dwSeed)
{
  mt_table[0] = dwSeed & 0xffffffffUL;
  for (mt_state = 1; mt_state < 624; ++mt_state)
  {
    mt_table[mt_state] = (69069 * mt_table[mt_state - 1]) & 0xffffffffUL;
  }
}

unsigned long mt_get_rand(void)
{
  unsigned long mt_result;
  static unsigned long mt_magic[2] = {0x0UL, 0x9908b0dfUL};

  int j = 0;

  if ( mt_state >= 624 )
  {
    if (mt_state == 624 + 1)
        mt_init(5489UL);

        for (j = 0; j < 624 - 397; j++)
        {
          mt_result = (mt_table[j] & 0x80000000UL) | (mt_table[j + 1] & 0x7fffffffUL);
          mt_table[j] = mt_table[j + 397] ^ (mt_result >> 1) ^ mt_magic[mt_result & 0x1UL];
        }
     
        for (;j < 624 - 1; j++)
        {
          mt_result = (mt_table[j] & 0x80000000UL) | (mt_table[j + 1] & 0x7fffffffUL);
          mt_table[j] = mt_table[j + (397 - 624)] ^ (mt_result >> 1) ^ mt_magic[mt_result & 0x1UL];
        }
     
        mt_result = (mt_table[624 - 1] & 0x80000000UL) | (mt_table[0] & 0x7fffffffUL);
        mt_table[624 - 1] = mt_table[397 - 1] ^ (mt_result >> 1) ^ mt_magic[mt_result & 0x1UL];

        mt_state = 0;
   }

   mt_result = mt_table[mt_state++];
   mt_result ^= (mt_result >> 11);
   mt_result ^= (mt_result << 7) & 0x9d2c5680UL;
   mt_result ^= (mt_result << 15) & 0xefc60000UL;
   mt_result ^= (mt_result >> 18);
   
   mt_result = ((mt_result << 8) & 0xff00ff00UL) | ((mt_result >> 8) & 0xff00ffUL);
   return (mt_result << 16) | (mt_result >> 16);
}

void kar_decrypt(unsigned char* lpBuffer, unsigned int dwSeed, int dwSize)
{
  mt_init(dwSeed);
  for (int i = 0; i < dwSize; i++)
  {
     lpBuffer[i] ^= (unsigned char)mt_get_rand();
  }
}

int swap_int32(int val)
{
    val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
    return (val << 16) | ((val >> 16) & 0xFFFF);
}

void kar_rebuild_table(unsigned char* lpSrcBuffer, int dwSize)
{
  int dwPosDest = 0;
  int dwPosBegin = 4;
  int dwBlocks = dwSize / 1024 + 1;
  for (int i = 0; i < dwBlocks; i++)
  {
     char bCheck = 0;
     int dwBlockSize = 0;

     memcpy(&bCheck, lpSrcBuffer + dwPosBegin, 1);
     memcpy(&dwBlockSize, lpSrcBuffer + dwPosBegin + 1, 4);

     dwBlockSize = swap_int32(dwBlockSize);
     memcpy(lpSrcBuffer + dwPosDest, lpSrcBuffer + dwPosBegin + 5, dwBlockSize);
     dwPosBegin += dwBlockSize + 5;
     dwPosDest += dwBlockSize;
  }
}
"
endian big
comtype gzip
get ARCHIVE_SIZE asize
get TABLE_SIZE long
math TABLE_SIZE ^= 0x85FD91A1
math TABLE_OFFSET = ARCHIVE_SIZE
math TABLE_OFFSET -= TABLE_SIZE

string TABLE_KEY p= "%d" TABLE_OFFSET
strlen TABLE_KEY_LENGTH TABLE_KEY

log MEMORY_FILE TABLE_OFFSET TABLE_SIZE
calldll MEMORY_FILE10 "kar_gethash" "tcc" TABLE_HASH TABLE_KEY TABLE_KEY_LENGTH
calldll MEMORY_FILE10 "kar_decrypt" "tcc" RET MEMORY_FILE TABLE_HASH TABLE_SIZE

get ARCHIVE_NAME filename
if ARCHIVE_NAME == "keel.dat"
   set ARCHIVE_PASSWORD string "dhfuhsudfh98vhdsovnfdhiouer8u8hgjbkjciudsuifsjdiosajfn"
   calldll MEMORY_FILE10 "kar_rebuild_table" "tcc" RET MEMORY_FILE TABLE_SIZE
   get DUMMY1 byte MEMORY_FILE #1,3
endif

get MD5_LENGTH short MEMORY_FILE
getdstring MD5_HASH MD5_LENGTH MEMORY_FILE
get TIMESTAMP longlong MEMORY_FILE
get DUMMY3 byte MEMORY_FILE #0,1,3

if DUMMY3 == 3
  get COMPRESSION_STR_LENGTH short MEMORY_FILE
  getdstring COMPRESSION COMPRESSION_STR_LENGTH MEMORY_FILE #deflate=-1 -> data is compressed with gzip
endif

get FILES long MEMORY_FILE
for i = 0 < FILES
  get FIRST_PATH_LENGTH short MEMORY_FILE
  getdstring FIRST_PATH FIRST_PATH_LENGTH MEMORY_FILE 
  get SECOND_PATH_LENGTH short MEMORY_FILE
  getdstring SECOND_PATH SECOND_PATH_LENGTH MEMORY_FILE
  get SIZE long MEMORY_FILE
  get OFFSET longlong MEMORY_FILE
   
  string FILE_KEY p= "%s%s%s" ARCHIVE_PASSWORD FIRST_PATH TIMESTAMP
  strlen FILE_KEY_LENGTH FILE_KEY
  callfunction EXTRACT_FILE 1
next i

startfunction EXTRACT_FILE
  log MEMORY_FILE2 OFFSET SIZE
  calldll MEMORY_FILE10 "kar_gethash" "tcc" FILE_HASH FILE_KEY FILE_KEY_LENGTH
  calldll MEMORY_FILE10 "kar_decrypt" "tcc" RET MEMORY_FILE2 FILE_HASH SIZE
   
  if DUMMY3 == 1 || DUMMY3 == 3
     clog FIRST_PATH 0 SIZE SIZE MEMORY_FILE2
  else
     log FIRST_PATH 0 SIZE MEMORY_FILE2
  endif
endfunction


ERROR
120 calldll MEMORY_FILE10 "kar_rebuild_table" "tcc" RET MEMORY_FILE TABLE_SIZE

FILE
https://mega.nz/file/XhoEACQT#KOct1oyW9RkTqd4jIMTMIaVMrDlwTlaKzrgamHmBh34
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: [Help] Getamped kar file Extract

Post by aluigi »

Why didn't you post in the original topic where you got the script instead?
viewtopic.php?t=13086