Skip to content

Commit

Permalink
Added DecryptGIF for Flare-On 6 challenge 10 "Mugatu"
Browse files Browse the repository at this point in the history
  • Loading branch information
HomeSen committed Oct 20, 2019
1 parent 18f9fdb commit 3b48512
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 0 deletions.
157 changes: 157 additions & 0 deletions Flare-On 6/10 - Mugatu/DecryptGIF/DecryptGIF.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <stdint.h>

void decipher(unsigned long *const v, unsigned long *const w, const unsigned long *const k)
{
register unsigned long y = v[0], z = v[1], sum = 0xC6EF3720,
delta = 0x9E3779B9, a = k[0], b = k[1], c = k[2],
d = k[3], n = 32;

/* sum = delta<<5, in general sum = delta * n */

while (n-- > 0)
{
z -= (y << 4) + c ^ y + sum ^ (y >> 5) + d;
y -= (z << 4) + a ^ z + sum ^ (z >> 5) + b;
sum -= delta;
}

w[0] = y; w[1] = z;
}

void decryptData(unsigned int rounds, uint32_t v[2], uint32_t const k[4])
{
uint32_t v0, v1;
uint32_t sum;
const uint32_t delta = 0x9E3779B9;

v0 = v[0];
v1 = v[1];
sum = delta * rounds;
do {
v1 -= ((((v0 << 4) ^ (v0 >> 5)) + v0) ^ (k[(sum >> 0xb) & 3] + sum));
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (k[sum & 3] + sum);

rounds--;
} while (rounds != 0);
v[0] = v0;
v[1] = v1;

return;
}

void long_to_string(uint32_t v[2], unsigned char buffer[8])
{
buffer[0] = (v[0] >> 0) & 0xff;
buffer[1] = (v[0] >> 8) & 0xff;
buffer[2] = (v[0] >> 16) & 0xff;
buffer[3] = (v[0] >> 24) & 0xff;
buffer[4] = (v[1] >> 0) & 0xff;
buffer[5] = (v[1] >> 8) & 0xff;
buffer[6] = (v[1] >> 16) & 0xff;
buffer[7] = (v[1] >> 24) & 0xff;
}

int findKey(unsigned char *buffer, uint32_t k[4])
{
unsigned char result[8];
char *target = "GIF8";
int found = 0;
uint32_t v[2];

while (found == 0)
{
for (k[0]=0x31; k[0] < 256; k[0]++)
{
for (k[1]=0x73; k[1] < 256; k[1]++)
{
for (k[2]=0x35; k[2] < 256; k[2]++)
{
for (k[3]=0xb1; k[3] < 256; k[3]++)
{
v[0] = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
v[1] = (buffer[7] << 24) | (buffer[6] << 16) | (buffer[5] << 8) | buffer[4];

//decipher(v, v, k);
decryptData(32, v, k);
long_to_string(v, result);
found = 1;
for (int i = 0; i < 4; i++)
{
if (result[i] != target[i])
{
found = 0;
break;
}
}
if (found == 1)
break;
}
if (found == 1)
break;
}
if (found == 1)
break;
}
if (found == 1)
break;
}
}

return found;
}

int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Usage: %s input_file output_file\n", argv[0]);
return 1;
}

unsigned char buffer[8];
uint32_t v[2];
uint32_t key[4];

FILE *hInputFile, *hOutputFile;
fopen_s(&hInputFile, argv[1], "rb");

fread(buffer, sizeof(buffer), 1, hInputFile);
if (findKey(buffer, key) == 0)
{
printf("Failed to find key!\n");
fclose(hInputFile);
return 2;
}

printf("Found key: ");
for (int i = 0; i < 4; i++)
printf("%08x", key[i]);
printf("\n");
fseek(hInputFile, 0, 0);

//key[0] = 0x31;
//key[1] = 0x73;
//key[2] = 0x35;
//key[3] = 0xb1;
fopen_s(&hOutputFile, argv[2], "wb");
do
{
memset(buffer, 0, sizeof(buffer));
fread(buffer, sizeof(buffer), 1, hInputFile);
v[0] = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
v[1] = (buffer[7] << 24) | (buffer[6] << 16) | (buffer[5] << 8) | buffer[4];
//decipher(v, v, k);
decryptData(32, v, key);
long_to_string(v, buffer);
fwrite(buffer, sizeof(buffer), 1, hOutputFile);
} while (!feof(hInputFile));

fclose(hInputFile);
fclose(hOutputFile);

return 0;
}
31 changes: 31 additions & 0 deletions Flare-On 6/10 - Mugatu/DecryptGIF/DecryptGIF.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 15 for Windows Desktop
VisualStudioVersion = 15.0.28307.572
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DecryptGIF", "DecryptGIF.vcxproj", "{82B0A257-B26F-40FD-8658-166D844667A9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{82B0A257-B26F-40FD-8658-166D844667A9}.Debug|x64.ActiveCfg = Debug|x64
{82B0A257-B26F-40FD-8658-166D844667A9}.Debug|x64.Build.0 = Debug|x64
{82B0A257-B26F-40FD-8658-166D844667A9}.Debug|x86.ActiveCfg = Debug|Win32
{82B0A257-B26F-40FD-8658-166D844667A9}.Debug|x86.Build.0 = Debug|Win32
{82B0A257-B26F-40FD-8658-166D844667A9}.Release|x64.ActiveCfg = Release|x64
{82B0A257-B26F-40FD-8658-166D844667A9}.Release|x64.Build.0 = Release|x64
{82B0A257-B26F-40FD-8658-166D844667A9}.Release|x86.ActiveCfg = Release|Win32
{82B0A257-B26F-40FD-8658-166D844667A9}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1A871118-36A7-462B-B9F1-B398F180D340}
EndGlobalSection
EndGlobal
161 changes: 161 additions & 0 deletions Flare-On 6/10 - Mugatu/DecryptGIF/DecryptGIF.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{82B0A257-B26F-40FD-8658-166D844667A9}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>DecryptGIF</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<DisableLanguageExtensions>true</DisableLanguageExtensions>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<DisableLanguageExtensions>true</DisableLanguageExtensions>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="DecryptGIF.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions Flare-On 6/10 - Mugatu/DecryptGIF/DecryptGIF.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="DecryptGIF.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions Flare-On 6/10 - Mugatu/DecryptGIF/pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

#ifndef PCH_H
#define PCH_H

// TODO: add headers that you want to pre-compile here

#endif //PCH_H

0 comments on commit 3b48512

Please sign in to comment.