Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
riverar committed Apr 7, 2012
0 parents commit cb0fe0e
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .gitignore
@@ -0,0 +1,54 @@
#OS junk files
[Tt]humbs.db
*.DS_Store

#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.xap
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.opensdf
*.unsuccessfulbuild
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*

#Project files
[Bb]uild/

#Subversion files
.svn

# Office Temp Files
~$*

#NuGet
packages/

#ncrunch
*ncrunch*
*crunch*.local.xml
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 Rafael Rivera

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
146 changes: 146 additions & 0 deletions hashlnk.cpp
@@ -0,0 +1,146 @@
/*
* Copyright (c) 2012 Rafael Rivera
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

// An Anonymous fellow and Peter Bright (yes, that guy) helped me refactor.
// Appreciate it, sirs.

#include "stdafx.h"

// {7B2D8DFB-D190-344E-BF60-6EAC09922BBF}
DEFINE_PROPERTYKEY(PKEY_WINX_HASH, 0xFB8D2D7B, 0x90D1, 0x4E34, 0xBF, 0x60, 0x6E, 0xAC, 0x09, 0x92, 0x2B, 0xBF, 0x02);

#define CHECKHR(HR, MESSAGE) \
if(FAILED(HR)) \
{ \
wprintf(L"%s (hr: %X)\r\n", MESSAGE, hr); \
return HRESULT_FROM_WIN32(HR); \
}

using namespace std;

int wmain(int argc, wchar_t* argv[])
{
wprintf(
L"\nHashLnk v0.1.1.0\n"
L"Copyright(c) 2012 Rafael Rivera\n"
L"Within Windows - http://withinwindows.com\n\n");

//
// Before we go too far, let's see if we have a few things in order.
//
if(argc != 2)
{
wprintf(L"usage: hashlnk <.lnk file>\n\n");
return ERROR_BAD_ARGUMENTS;
}

DWORD bufferSize = GetFullPathNameW(argv[1], 0, nullptr, nullptr);
if(bufferSize == 0)
{
wprintf(L"Specified path is invalid.");
return ERROR_INVALID_PARAMETER;
}

unique_ptr<wchar_t[]> resolvedTarget(new wchar_t[bufferSize]);
GetFullPathNameW(argv[1], bufferSize, resolvedTarget.get(), nullptr);

if(!PathFileExistsW(resolvedTarget.get()))
{
wprintf(L"Specified file does not exist.");
return ERROR_FILE_NOT_FOUND;
}

HRESULT hr = CoInitialize(nullptr);
CHECKHR(hr, L"Failed to initialize COM.");

//
// Let's pull out the shortcut's target path/args
//
CComPtr<IShellItem2> lnk;
hr = SHCreateItemFromParsingName(resolvedTarget.get(), nullptr, IID_PPV_ARGS(&lnk));
CHECKHR(hr, L"Failed to create shell item.");

CComHeapPtr<wchar_t> targetPath;
hr = lnk->GetString(PKEY_Link_TargetParsingPath, &targetPath);
CHECKHR(hr, L"Failed to retrieve target path.");

CComHeapPtr<wchar_t> targetArgs;
hr = lnk->GetString(PKEY_Link_Arguments, &targetArgs);
if(FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
{
wprintf(L"Failed to retrieve target arguments.");
return HRESULT_FROM_WIN32(hr);
}
wchar_t* targetPathSansRoot = PathSkipRootW(targetPath);

//
// Glue everything together and lowercase it, so we can hash it.
//
const wchar_t salt[] = L"do not prehash links. this should only be done by the user.";

wstring blob = targetPathSansRoot;
if(targetArgs)
{
blob += targetArgs;
}
blob += salt;

int lowerCaseLength = LCMapStringEx(LOCALE_NAME_INVARIANT, LCMAP_LOWERCASE, blob.data(), blob.size(), nullptr, 0, nullptr, nullptr, 0);
if(!lowerCaseLength)
{
wprintf(L"Failed to lowercase blob string.");
return GetLastError();
}

unique_ptr<wchar_t[]> hashableBlob(new wchar_t[lowerCaseLength]);
LCMapStringEx(LOCALE_NAME_INVARIANT, LCMAP_LOWERCASE, blob.data(), blob.size(), hashableBlob.get(), lowerCaseLength, nullptr, nullptr, 0);

ULONG hash = 0;
hr = HashData(reinterpret_cast<BYTE*>(hashableBlob.get()), lowerCaseLength * sizeof(wchar_t), reinterpret_cast<BYTE*>(&hash), sizeof(hash));
CHECKHR(hr, L"Failed to hash data.");

//
// We have a hash, let's stamp it onto the .lnk now.
//
CComPtr<IPropertyStore> store;
hr = lnk->GetPropertyStore(GPS_READWRITE, IID_PPV_ARGS(&store));
CHECKHR(hr, L"Failed to get property store.");

PROPVARIANT propValue = {0};
propValue.ulVal = hash;
propValue.vt = VT_UI4;

hr = store->SetValue(PKEY_WINX_HASH, propValue);
CHECKHR(hr, L"Failed to set property store value.");

hr = store->Commit();
CHECKHR(hr, L"Failed to write changes to .lnk.");

wprintf(L"Hash generated and applied (0x%X)", hash);

CoUninitialize();

return 0;
}
Binary file added hashlnk.rc
Binary file not shown.
22 changes: 22 additions & 0 deletions hashlnk.sln
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 11
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hashlnk", "hashlnk.vcxproj", "{A74CC7DA-5D68-2664-56F3-D027B2557CBA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A74CC7DA-5D68-2664-56F3-D027B2557CBA}.Debug|Win32.ActiveCfg = Debug|Win32
{A74CC7DA-5D68-2664-56F3-D027B2557CBA}.Debug|Win32.Build.0 = Debug|Win32
{A74CC7DA-5D68-2664-56F3-D027B2557CBA}.Debug|Win32.Deploy.0 = Debug|Win32
{A74CC7DA-5D68-2664-56F3-D027B2557CBA}.Release|Win32.ActiveCfg = Release|Win32
{A74CC7DA-5D68-2664-56F3-D027B2557CBA}.Release|Win32.Build.0 = Release|Win32
{A74CC7DA-5D68-2664-56F3-D027B2557CBA}.Release|Win32.Deploy.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
99 changes: 99 additions & 0 deletions hashlnk.vcxproj
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.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>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCTargetsPath Condition="'$(VCTargetsPath11)' != '' and '$(VSVersion)' == '' and '$(VisualStudioVersion)' == ''">$(VCTargetsPath11)</VCTargetsPath>
</PropertyGroup>
<PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword>
<RootNamespace>hashlnk</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<UseOfAtl>Static</UseOfAtl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v100</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfAtl>Static</UseOfAtl>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</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>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;Shlwapi.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;Shlwapi.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="hashlnk.cpp" />
<ClCompile Include="stdafx.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hashlnk.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
14 changes: 14 additions & 0 deletions resource.h
@@ -0,0 +1,14 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by hashlnk.rc

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
1 change: 1 addition & 0 deletions stdafx.cpp
@@ -0,0 +1 @@
#include "stdafx.h"

0 comments on commit cb0fe0e

Please sign in to comment.