Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added SafeExecute-v1.0.zip
Binary file not shown.
7 changes: 3 additions & 4 deletions SafeExecute/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
#include "readmode.h"

char processPath[MAX_PATH];
char processDir[MAX_PATH];
char localDir[MAX_PATH];

DWORD WINAPI ThreadMain(LPVOID params) {
GetModuleFileNameA(NULL, processPath, MAX_PATH);
strcpy_s(processDir, processPath);
PathRemoveFileSpecA(processDir);
strcat_s(processDir, "\\");
ExpandEnvironmentStringsA("%LOCALAPPDATA%", localDir, MAX_PATH);
strcat_s(localDir, "\\安全実行侍~俺を信じろ~");

ReadMode();
ReadCheckList();
Expand Down
71 changes: 33 additions & 38 deletions SafeExecute/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool IsSafeExecuteFilesA(LPSTR lpFileName) {
GetFullPathNameA(lpFileName, MAX_PATH, fullPath, NULL);

string strFullPath(fullPath);
string safeExecutePath(processDir);
string safeExecutePath(localDir);
transform(strFullPath.begin(), strFullPath.end(), strFullPath.begin(), ::toupper);
transform(safeExecutePath.begin(), safeExecutePath.end(), safeExecutePath.begin(), ::toupper);

Expand All @@ -41,7 +41,7 @@ bool IsSafeExecuteFilesW(LPWSTR lpFileName) {
GetFullPathNameA(WStringToString(lpFileName).c_str(), MAX_PATH, fullPath, NULL);

string strFullPath(fullPath);
string safeExecutePath(processDir);
string safeExecutePath(localDir);
transform(strFullPath.begin(), strFullPath.end(), strFullPath.begin(), ::toupper);
transform(safeExecutePath.begin(), safeExecutePath.end(), safeExecutePath.begin(), ::toupper);

Expand Down Expand Up @@ -201,15 +201,16 @@ bool WINAPI CreateProcessA_Hook(
if (res == IDNO)
ExitProcess(1);
}

BOOL suspended = ((dwCreationFlags & CREATE_SUSPENDED) != 0);
dwCreationFlags |= CREATE_SUSPENDED;
BOOL res2 = orig_CreateProcessA(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);


// inject SafeExecute.dll to child process
FARPROC lib = GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
char dllpath[MAX_PATH];
GetModuleFileNameA(GetModuleHandleA("SafeExecute.dll"), dllpath, MAX_PATH);
GetModuleFileNameA(GetModuleHandleA("SafeExecute.dll"), dllpath, MAX_PATH);
size_t dllpathSize = strlen(dllpath);
LPVOID allocMem = VirtualAllocEx(lpProcessInformation->hProcess, NULL, dllpathSize, MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(lpProcessInformation->hProcess, allocMem, dllpath, dllpathSize, NULL);
Expand Down Expand Up @@ -380,28 +381,25 @@ HANDLE WINAPI CreateFileA_Hook(
strFileName = PathToFileName((LPSTR)lpFileName);

char path[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, path);
strcat_s(path, "\\backups\\");
strcpy_s(path, localDir);
strcat_s(path, "backups\\");

if (PathFileExistsA(path)) {
char buf[50];
memset(buf, 0, 50);
if (!PathFileExistsA(path))
CreateDirectoryA(path, NULL);

char buf[50];
memset(buf, 0, 50);

string strProcPath;
strProcPath = PathToFileName(processPath);
string strProcPath;
strProcPath = PathToFileName(processPath);

strcat_s(path, strProcPath.c_str());
strcat_s(path, strProcPath.c_str());

CreateDirectoryA(path, NULL);
CreateDirectoryA(path, NULL);

strcat_s(path, "\\");
strcat_s(path, strFileName.c_str());
CopyFileA(lpFileName, path, FALSE);
}
else {
MessageBoxA(NULL, "Something went wrong in path calculation.\n'backups/' folder missing?", "File Backup Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
}
strcat_s(path, "\\");
strcat_s(path, strFileName.c_str());
CopyFileA(lpFileName, path, FALSE);
}

return orig_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
Expand All @@ -427,28 +425,25 @@ HANDLE WINAPI CreateFileW_Hook(
strFileName = PathToFileName((LPSTR)WStringToString(lpFileName).c_str());

char path[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, path);
strcat_s(path, "\\backups\\");
strcpy_s(path, localDir);
strcat_s(path, "backups\\");

if (PathFileExistsA(path)) {
char buf[50];
memset(buf, 0, 50);
if (!PathFileExistsA(path))
CreateDirectoryA(path, NULL);

string strProcPath;
strProcPath = PathToFileName(processPath);
char buf[50];
memset(buf, 0, 50);

strcat_s(path, strProcPath.c_str());
string strProcPath;
strProcPath = PathToFileName(processPath);

CreateDirectoryA(path, NULL);
strcat_s(path, strProcPath.c_str());

strcat_s(path, "\\");
strcat_s(path, strFileName.c_str());
CopyFileA(WStringToString(lpFileName).c_str(), path, FALSE);
}
else {
MessageBoxA(NULL, "Something went wrong in path calculation.\n'backups/' folder missing?", "File Backup Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
}
CreateDirectoryA(path, NULL);

strcat_s(path, "\\");
strcat_s(path, strFileName.c_str());
CopyFileA(WStringToString(lpFileName).c_str(), path, FALSE);
}

return orig_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
Expand Down
2 changes: 1 addition & 1 deletion SafeExecute/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define MsgBox(X) MessageBoxA(NULL, X, "SafeExecute", MB_YESNO)

extern char processPath[MAX_PATH];
extern char processDir[MAX_PATH];
extern char localDir[MAX_PATH];
extern bool CreateProcessChecked;

typedef struct HookFunc {
Expand Down
67 changes: 32 additions & 35 deletions SafeExecute/prehook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,45 @@

void LogHookedApi(int argc, VCHAR argv) {
char path[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, path);
strcat_s(path, "\\logs");
ExpandEnvironmentStringsA("%LOCALAPPDATA%", path, MAX_PATH);
strcat_s(path, "\\���S���s���`����M����`\\logs");

if (PathFileExistsA(path)) {
strcat_s(path, "\\log.csv");
if (!PathFileExistsA(path))
CreateDirectoryA(path, NULL);

HANDLE hFile;
DWORD writesize;
if (PathFileExistsA(path)) {
hFile = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
else {
hFile = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
char header[] = "timestamp,executable path,hooked windows api,args\n";
WriteFile(hFile, header, strlen(header), &writesize, NULL);
}
strcat_s(path, "\\log.csv");

SetFilePointer(hFile, 0, NULL, FILE_END);
HANDLE hFile;
DWORD writesize;
if (PathFileExistsA(path)) {
hFile = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
else {
hFile = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
char header[] = "timestamp,executable path,hooked windows api,args\n";
WriteFile(hFile, header, strlen(header), &writesize, NULL);
}

SYSTEMTIME time;
GetLocalTime(&time);
SetFilePointer(hFile, 0, NULL, FILE_END);

char buf[1000];
memset(buf, 0, 1000);
snprintf(buf, 1000, "%04d/%02d/%02d_%02d:%02d:%02d.%d,%s,%s",
time.wYear, time.wMonth, time.wDay,
time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
processPath, argv[0]);
for (int i = 1; i < argc; i++) {
strcat_s(buf, ",");
strcat_s(buf, argv[i]);
}
strcat_s(buf, "\n");
SYSTEMTIME time;
GetLocalTime(&time);

WriteFile(hFile, buf, strlen(buf), &writesize, NULL);
CloseHandle(hFile);
return;
}
else {
MessageBoxA(NULL, "Please execute SafeExecutor from project home directory", "PreHook Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
char buf[1000];
memset(buf, 0, 1000);
snprintf(buf, 1000, "%04d/%02d/%02d_%02d:%02d:%02d.%d,%s,%s",
time.wYear, time.wMonth, time.wDay,
time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
processPath, argv[0]);
for (int i = 1; i < argc; i++) {
strcat_s(buf, ",");
strcat_s(buf, argv[i]);
}
strcat_s(buf, "\n");

WriteFile(hFile, buf, strlen(buf), &writesize, NULL);
CloseHandle(hFile);
return;
}

void PreHook(int argc, ...) {
Expand Down
2 changes: 1 addition & 1 deletion SafeExecute/readchecklist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bool IsNumber(const string& s) {

void ReadCheckList() {
char path[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, path);
strcpy_s(path, localDir);
strcat_s(path, "\\rules");

if (PathFileExistsA(path)) {
Expand Down
4 changes: 2 additions & 2 deletions SafeExecute/readmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ DWORD MODE = MODE_NORMAL;

void ReadMode() {
char path[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, path);
strcat_s(path, "\\rules");
ExpandEnvironmentStringsA("%LOCALAPPDATA%", path, MAX_PATH);
strcat_s(path, "\\���S���s���`����M����`\\rules");

if (PathFileExistsA(path)) {
strcat_s(path, "\\mode.txt");
Expand Down
Binary file added SafeExecutePackage/Images/BadgeLogo.scale-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/BadgeLogo.scale-125.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/BadgeLogo.scale-150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/BadgeLogo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/BadgeLogo.scale-400.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/LargeTile.scale-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/LargeTile.scale-125.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/LargeTile.scale-150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/LargeTile.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/LargeTile.scale-400.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/SmallTile.scale-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/SmallTile.scale-125.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/SmallTile.scale-150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/SmallTile.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/SmallTile.scale-400.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafeExecutePackage/Images/StoreLogo.backup.png
Binary file added SafeExecutePackage/Images/StoreLogo.scale-125.png
Binary file added SafeExecutePackage/Images/StoreLogo.scale-150.png
Binary file added SafeExecutePackage/Images/StoreLogo.scale-200.png
Binary file added SafeExecutePackage/Images/StoreLogo.scale-400.png
56 changes: 56 additions & 0 deletions SafeExecutePackage/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>

<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">

<Identity
Name="4c089c08-edd9-4511-b58f-adb16c064298"
Publisher="CN=MachineHunter"
Version="1.0.0.0" />

<Properties>
<DisplayName>SafeExecutePackage</DisplayName>
<PublisherDisplayName>MachineHunter</PublisherDisplayName>
<Logo>Images\StoreLogo.png</Logo>
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.14393.0" MaxVersionTested="10.0.14393.0" />
</Dependencies>

<Resources>
<Resource Language="x-generate"/>
</Resources>

<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="安全実行侍~俺を信じろ~"
Description="実行ファイルの挙動を制限及び可視化するためのツール。Visual Studio 2019にてビルド。MWS Cup 2022用。"
BackgroundColor="transparent"
Square150x150Logo="Images\Square150x150Logo.png"
Square44x44Logo="Images\Square44x44Logo.png">
<uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png" Square71x71Logo="Images\SmallTile.png" Square310x310Logo="Images\LargeTile.png" ShortName="安全実行侍~俺を信じろ~">
<uap:ShowNameOnTiles>
<uap:ShowOn Tile="square150x150Logo"/>
<uap:ShowOn Tile="wide310x150Logo"/>
<uap:ShowOn Tile="square310x310Logo"/>
</uap:ShowNameOnTiles>
</uap:DefaultTile >
<uap:SplashScreen Image="Images\SplashScreen.png" />
<uap:LockScreen BadgeLogo="Images\BadgeLogo.png" Notification="badgeAndTileText"/>
</uap:VisualElements>
</Application>
</Applications>

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="runFullTrust" />
</Capabilities>
</Package>
Loading