Skip to content

Commit

Permalink
Adding UCLHASP usermode dongle emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
leecher1337 committed Oct 21, 2022
1 parent 5f29fb6 commit 8aebc83
Show file tree
Hide file tree
Showing 46 changed files with 4,092 additions and 137 deletions.
137 changes: 129 additions & 8 deletions dumplog/dumplog.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,125 @@
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "log.h"
#include "haspvdd.h"
#include "haspcrypt.h"
#include "haspapi.h"


static void ShowPrefix(LOG_HDR* hdr, char emu)
{
const char* m_pszWhat[3] = { LOGTYPE_STR };

printf("%02d.%02d.%04d %02d:%02d:%02d.%04d%c%s [%02X] ",
hdr->st.wDay, hdr->st.wMonth, hdr->st.wYear,
hdr->st.wHour, hdr->st.wMinute, hdr->st.wSecond, hdr->st.wMilliseconds,
emu, m_pszWhat[hdr->Direction], hdr->Length);
}

static BYTE *PrepareDataBuffer(FILE* fp, HaspBufferInStruc* IOBuffer, LOG_HDR *phdr)
{
BYTE* pBuffer;
int i;

if (!fread(phdr, sizeof(LOG_HDR), 1, fp)) return NULL;
if (phdr->Direction == 2 && (pBuffer = malloc(IOBuffer->DOSBuffer.SI * 2)))
{
if (fread(pBuffer, IOBuffer->DOSBuffer.SI * 2, 1, fp))
{
IOBuffer->DOSBuffer.AX = (DWORD)pBuffer;
printf("Data buffer allocated at %08X\n", IOBuffer->DOSBuffer.AX);
ShowPrefix(phdr, ' ');
for (i = 0; i < phdr->Length; i++) printf("%02X ", pBuffer[i]);
printf("\n");
return pBuffer;
}
free(pBuffer);
}
fseek(fp, (long)sizeof(LOG_HDR) * -1, SEEK_CUR);
return NULL;

}

int TestEmulator(char* pszEmuDLL, FILE* fp)
{
fnHaspIOCtl pfunc;
HMODULE hDLL = LoadLibrary(pszEmuDLL);
BYTE buffer[65535], *pBuffer = NULL;
int i;
LOG_HDR hdr, hdrdata;
DWORD dwRead;

if (!hDLL)
{
fprintf(stderr, "Loading %s failed with %d\n", pszEmuDLL, GetLastError());
return -1;
}

if (!(pfunc = (fnHaspIOCtl)GetProcAddress(hDLL, "CallHardlock")))
{
fprintf(stderr, "Emulator DLL doesn't contain correct dispatcher function\n");
FreeLibrary(hDLL);
return -1;
}

while (fread(&hdr, sizeof(hdr), 1, fp) == 1)
{
ShowPrefix(&hdr, ' ');
fread(buffer, hdr.Length, 1, fp);
for (i = 0; i < hdr.Length; i++) printf("%02X ", buffer[i]);
printf("\n");
if (hdr.Direction == 0)
{
HaspBufferInStruc* IOBuffer = (HaspBufferInStruc*)buffer;
printf("Press any key to send...\n");
_getch();
switch (IOBuffer->DOSBuffer.Service)
{
case MEMOHASP_READBLOCK:
case MEMOHASP_WRITEBLOCK:
case TIMEHASP_READBLOCK:
case TIMEHASP_WRITEBLOCK:
case LOCALHASP_ENCODEDATA:
case LOCALHASP_DECODEDATA:
if (!(pBuffer = PrepareDataBuffer(fp, IOBuffer, &hdrdata)))
{
fprintf(stderr, "Prepare Buffer failed, skip...\n");
continue;
}
break;
}

Encrypt28((PUSHORT)buffer, hdr.Length);
if (pfunc((HaspBufferInStruc*)buffer, hdr.Length, &dwRead))
{
GetLocalTime(&hdr.st);
hdr.Direction = 1;
hdr.Length = (USHORT)dwRead;
ShowPrefix(&hdr, 'E');
Decrypt28((PUSHORT)buffer, hdr.Length);
for (i = 0; i < hdr.Length; i++) printf("%02X ", buffer[i]);
printf("\n");
}
if (pBuffer)
{
GetLocalTime(&hdrdata.st);
ShowPrefix(&hdrdata, 'E');
for (i = 0; i < hdrdata.Length; i++) printf("%02X ", pBuffer[i]);
printf("\n");
free(pBuffer);
pBuffer = NULL;
}
}
printf("-------------------------------------------------------------------------------\n");
}
printf("\n");

FreeLibrary(hDLL);
return 0;
}

int main(int argc, char** argv)
{
Expand All @@ -13,7 +131,8 @@ int main(int argc, char** argv)

if (argc < 2)
{
fprintf(stderr, "Usage: %s <Logfile>\n", argv[0]);
fprintf(stderr, "Usage: %s <Logfile> [emulator.dll]\n\n" \
"If emualtor.dll specified, sends data to emulator, otherwise just dumps to screen\n\n", argv[0]);
return -1;
}

Expand All @@ -24,15 +143,17 @@ int main(int argc, char** argv)
}

printf(" %s\n", LOG_HEADER);
while (fread(&hdr, sizeof(hdr), 1, fp) == 1)
if (argc > 2) TestEmulator(argv[2], fp);
else
{
printf ("%02d.%02d.%04d %02d:%02d:%02d.%04d %s [%02X] ",
hdr.st.wDay, hdr.st.wMonth, hdr.st.wYear,
hdr.st.wHour, hdr.st.wMinute, hdr.st.wSecond, hdr.st.wMilliseconds,
m_pszWhat[hdr.Direction], hdr.Length);
for (i = 0; i < hdr.Length; i++) printf("%02X ", (UCHAR)fgetc(fp));
while (fread(&hdr, sizeof(hdr), 1, fp) == 1)
{
ShowPrefix(&hdr, ' ');
for (i = 0; i < hdr.Length; i++) printf("%02X ", (UCHAR)fgetc(fp));
printf("\n");
}
printf("\n");
}
printf("\n");
fclose(fp);
return 0;
}
1 change: 1 addition & 0 deletions dumplog/dumplog.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\haspvdd\haspcrypt.c" />
<ClCompile Include="dumplog.c" />
</ItemGroup>
<PropertyGroup Label="Globals">
Expand Down
3 changes: 3 additions & 0 deletions dumplog/dumplog.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
<ClCompile Include="dumplog.c">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="..\haspvdd\haspcrypt.c">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
</Project>
18 changes: 16 additions & 2 deletions haspnt64.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "haspvdd", "haspvdd\haspvdd.
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dumplog", "dumplog\dumplog.vcxproj", "{8A3FD706-33D4-4BED-B666-9041013178FE}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "uclhasp", "uclhasp\uclhasp.vcxproj", "{439F0459-5AA9-4C08-AE25-B17E062235F8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Expand Down Expand Up @@ -56,8 +58,8 @@ Global
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|ARM.ActiveCfg = Release-W64|Win32
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|ARM64.ActiveCfg = Release|Win32
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|x64.ActiveCfg = Release-W64|Win32
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|x86.ActiveCfg = Release|Win32
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|x86.Build.0 = Release|Win32
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|x86.ActiveCfg = Release-W64|Win32
{F777CFD4-3240-4EC0-AFA6-AA10183B0581}.Release|x86.Build.0 = Release-W64|Win32
{8A3FD706-33D4-4BED-B666-9041013178FE}.Debug|ARM.ActiveCfg = Debug|Win32
{8A3FD706-33D4-4BED-B666-9041013178FE}.Debug|ARM64.ActiveCfg = Debug|Win32
{8A3FD706-33D4-4BED-B666-9041013178FE}.Debug|x64.ActiveCfg = Debug|x64
Expand All @@ -70,6 +72,18 @@ Global
{8A3FD706-33D4-4BED-B666-9041013178FE}.Release|x64.Build.0 = Release|x64
{8A3FD706-33D4-4BED-B666-9041013178FE}.Release|x86.ActiveCfg = Release|Win32
{8A3FD706-33D4-4BED-B666-9041013178FE}.Release|x86.Build.0 = Release|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Debug|ARM.ActiveCfg = Debug|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Debug|ARM64.ActiveCfg = Debug|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Debug|x64.ActiveCfg = Debug|x64
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Debug|x64.Build.0 = Debug|x64
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Debug|x86.ActiveCfg = Debug|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Debug|x86.Build.0 = Debug|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Release|ARM.ActiveCfg = Release|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Release|ARM64.ActiveCfg = Release|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Release|x64.ActiveCfg = Release|x64
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Release|x64.Build.0 = Release|x64
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Release|x86.ActiveCfg = Release|Win32
{439F0459-5AA9-4C08-AE25-B17E062235F8}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions haspnt64/haspnt64.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "hardlock.h"
#include "haspintl.h"
#include "haspio.h"
#include "haspcrypt.h"
#include "hardlockdrv.h"

//----------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions haspnt64/haspnt64.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ copy /Y $(Inf2CatSource)..\..\Release\haspvdd.dll $(Inf2CatSource)
<FilesToPackage Include="$(TargetPath)" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\haspvdd\haspcrypt.c" />
<ClCompile Include="..\haspvdd\haspio.c" />
<ClCompile Include="hardlockdrv.c" />
<ClCompile Include="haspnt64.c" />
Expand Down
3 changes: 3 additions & 0 deletions haspnt64/haspnt64.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
<ClCompile Include="..\haspvdd\haspio.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\haspvdd\haspcrypt.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
59 changes: 47 additions & 12 deletions haspvdd/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,66 @@
*
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "haspvdd.h"
#include "hasp.h"
#include "haspintl.h"
#include "haspio.h"
#include "haspcrypt.h"
#include "log.h"

HaspDOSBufferStruc EmulatorCodeTemplate;
BOOL EmulateCalls(HaspBufferInStruc* Buffer, ULONG Length, PDWORD ReadBytes);

static HaspDOSBufferStruc EmulatorCodeTemplate;

/* Initializes the emulator by loading its configuration
*
* Parameters:
* hKey - Registry key of the node containing the configuration settings
* hKey - Registry key of the node containing the configuration settings
* pInterfaceMode - Interface mode will be set to appropriate dispatcher function
* pfnHaspIOCtl - IO Control dispatcher function will be set accordingly
*
* Returns:
* TRUE - Initialization succeeded
* FALSE - Failure to read configuration
*/
BOOL EmulatorInit(HKEY hKey)
BOOL EmulatorInit(HKEY hKey, DWORD *pInterfaceMode, fnHaspIOCtl *pfnHaspIOCtl)
{
DWORD cbData = 4 * sizeof(DWORD);
char szEmulator[MAX_PATH];

return RegQueryValueEx(hKey, "EmulateParams", NULL, NULL,
// User just wants to emulate 02 call with predefined data
if (RegQueryValueEx(hKey, "EmulateParams", NULL, NULL,
(LPBYTE)&EmulatorCodeTemplate.Param1Ret, &cbData) == ERROR_SUCCESS &&
cbData == 4 * sizeof(DWORD);
cbData == 4 * sizeof(DWORD))
{
*pfnHaspIOCtl = EmulateCalls;
*pInterfaceMode = MODE_IOCTLIF;
return TRUE;
}

// User registered an emulator library
cbData = sizeof(szEmulator);
if (RegQueryValueEx(hKey, "EmulatorDLL", NULL, NULL, szEmulator, &cbData) == ERROR_SUCCESS)
{
HMODULE hDLL = LoadLibrary(szEmulator);
fnHaspIOCtl pfunc;

if (!hDLL)
{
LogLastError("Cannot load specified emulator DLL: ");
return FALSE;
}

if (!(pfunc = (fnHaspIOCtl)GetProcAddress(hDLL, "CallHardlock")))
{
LogLastError("Emulator DLL doesn't contain correct dispatcher function: ");
FreeLibrary(hDLL);
return FALSE;
}
*pfnHaspIOCtl = pfunc;
*pInterfaceMode = MODE_LEGACYIF;
return TRUE;
}

return FALSE;
}

/* Emulates a call to the HASP dongle, dispatches incoming calls.
Expand All @@ -42,16 +77,16 @@ BOOL EmulatorInit(HKEY hKey)
* to the next driver.
*
* Parameters:
* Buffer - [IN] Decrypoted Buffer from caller to check
* Buffer - [IN] Decrypted Buffer from caller to check
* Length - [IN] Length of Buffer
* ReadBytes - [OUT] Numbre of bytes read
* ReadBytes - [OUT] Number of bytes read
*
* Returns:
* TRUE - Incoming packet has been processed successfully
* FALSE - Packet could not be processed, onknown Service code
* Even on FALSE, return Buffer is being Re-encrypted
*/
BOOL EmulateCalls(HaspBufferInStruc* Buffer, USHORT Length, PDWORD ReadBytes)
BOOL EmulateCalls(HaspBufferInStruc* Buffer, ULONG Length, PDWORD ReadBytes)
{
BOOL bRet = TRUE;

Expand Down
2 changes: 1 addition & 1 deletion haspvdd/emulate.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
BOOL EmulatorInit(HKEY hKey);
BOOL EmulatorInit(HKEY hKey, DWORD* pInterfaceMode, fnHaspIOCtl* pfnHaspIOCtl);
BOOL EmulateCalls(HaspBufferInStruc* Buffer, USHORT Length, PDWORD ReadBytes);

Loading

0 comments on commit 8aebc83

Please sign in to comment.