Skip to content

Instantly share code, notes, and snippets.

@levisre
Created July 13, 2015 06:39
Show Gist options
  • Save levisre/41a931845fb0d4770cbd to your computer and use it in GitHub Desktop.
Save levisre/41a931845fb0d4770cbd to your computer and use it in GitHub Desktop.
Small C Program to unpack Level 2 Protection of Jar2exe
/*
================================================|
Jar2Exe level 2 Unpacker (Hide Class protection)|
Created by Levis Nickaster |
http://ltops9.wordpress.com |
================================================|
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define ENCRYPTED_RES_ID 128 //ID for encrypted JAR file in RCDATA
int main(int argc, char const *argv[])
{
HMODULE hExe;
char* fileName = (char*)argv[1];
hExe = LoadLibrary(TEXT(fileName)); //Load exe file which contains encrypted resource
if(hExe)
{
//Find encrypted resource
HRSRC hRes = FindResource(hExe, MAKEINTRESOURCE(ENCRYPTED_RES_ID),RT_RCDATA);
if(hRes)
{
//Load Resource and get Pointer to it
HGLOBAL ResData = LoadResource(hExe,hRes);
void* encryptedJAR = LockResource(ResData);
//Get Resource size
int len = SizeofResource(hExe,hRes);
printf("Found embedded Resource with length %d bytes! Unpacking...\n",len);
//allocate memory to store decrypted Java
char* decryptedJAR = (char *)malloc(len+1);
if(decryptedJAR)
{
char* pChar= (char *)encryptedJAR;
//decrypt byte by byte and save it to memory region
for (int i = 0; i < len; ++i)
{
decryptedJAR[i]=(4*pChar[i])|(pChar[i]>>6)&3;
}
//Write out Decrypted JAR file and free memory
strcat(fileName,"-decrypted.jar");
FILE* outputFile = fopen(fileName, "wb");
fwrite(decryptedJAR,len,1,outputFile);
printf("File saved to : %s", fileName);
fclose(outputFile);
free(decryptedJAR);
FreeLibrary(hExe);
}
//if cannot allocate memory
else{printf("ERROR: Could not allocate enough memory!");}
}
//if cannot find encrypted resource (file not packed by jar2exe)
else{printf("ERROR: Encrypted Resource not found! Maybe it's not packed by jar2exe");}
}
//if file not found
else{printf("ERROR: File not found!");}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment