commit 9eae6bf20247ee29a956f2b94fcdceb4f5925a33 Author: cfif Date: Tue Jun 10 15:02:41 2025 +0300 Init diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/RunImageVC.iml b/.idea/RunImageVC.iml new file mode 100644 index 0000000..bc2cd87 --- /dev/null +++ b/.idea/RunImageVC.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1988395 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8a2c36b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/InstallDrv.vcxproj b/InstallDrv.vcxproj new file mode 100644 index 0000000..a28fd1a --- /dev/null +++ b/InstallDrv.vcxproj @@ -0,0 +1,136 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {031654c4-f73b-4465-8ec4-f4dc348fbb25} + InstallDrv + 10.0 + Run + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/InstallDrv.vcxproj.filters b/InstallDrv.vcxproj.filters new file mode 100644 index 0000000..6395c08 --- /dev/null +++ b/InstallDrv.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/InstallDrv.vcxproj.user b/InstallDrv.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/InstallDrv.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Release/Run.exe b/Release/Run.exe new file mode 100644 index 0000000..f726571 Binary files /dev/null and b/Release/Run.exe differ diff --git a/Release/Run.pdb b/Release/Run.pdb new file mode 100644 index 0000000..8719473 Binary files /dev/null and b/Release/Run.pdb differ diff --git a/Release/ZIP/.venv.zip b/Release/ZIP/.venv.zip new file mode 100644 index 0000000..6b0b240 Binary files /dev/null and b/Release/ZIP/.venv.zip differ diff --git a/Release/ZIP/dist.zip b/Release/ZIP/dist.zip new file mode 100644 index 0000000..8122d39 Binary files /dev/null and b/Release/ZIP/dist.zip differ diff --git a/Release/script.bat b/Release/script.bat new file mode 100644 index 0000000..e69de29 diff --git a/Release/script.txt b/Release/script.txt new file mode 100644 index 0000000..e69de29 diff --git a/Release/volume b/Release/volume new file mode 100644 index 0000000..a4ea55e --- /dev/null +++ b/Release/volume @@ -0,0 +1 @@ +tpo_mnz \ No newline at end of file diff --git a/Run.c b/Run.c new file mode 100644 index 0000000..f2315cf --- /dev/null +++ b/Run.c @@ -0,0 +1,376 @@ +// InstallDrv.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы. +// + +#define _CRT_SECURE_NO_WARNINGS + +#include +#include + +#define bzero(b) (memset((b), '\0', sizeof(b)), (void) 0) + +char volumeName[_MAX_PATH]; + +BOOL SetPrivilege( + HANDLE hToken, + LPCTSTR lpszPrivilege, // name of privilege to enable/disable + BOOL bEnablePrivilege // to enable or disable privilege +) +{ + TOKEN_PRIVILEGES tp; + LUID luid; + + if (!LookupPrivilegeValue( + NULL, // lookup privilege on local system + lpszPrivilege, // privilege to lookup + &luid)) // receives LUID of privilege + { + // printf("LookupPrivilegeValue error: %u\n", GetLastError() ); + return FALSE; + } + + tp.PrivilegeCount = 1; + tp.Privileges[0].Luid = luid; + if (bEnablePrivilege) { + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + } + else { + tp.Privileges[0].Attributes = 0; + } + + // Enable the privilege or disable all privileges. + + if (!AdjustTokenPrivileges( + hToken, + FALSE, + &tp, + sizeof(TOKEN_PRIVILEGES), + (PTOKEN_PRIVILEGES)NULL, + (PDWORD)NULL)) { + // printf("AdjustTokenPrivileges error: %u\n", GetLastError() ); + return FALSE; + } + + if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { + // printf("The token does not have the specified privilege. \n"); + return FALSE; + } + + return TRUE; +} + +int setSeDebug(HANDLE handleThreadId) +{ + HANDLE hToken; + if (!OpenThreadToken(handleThreadId, + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, + FALSE, + &hToken) + ) { + if (GetLastError() == ERROR_NO_TOKEN) { + if (!ImpersonateSelf(SecurityImpersonation)) { + //Log2File("Error setting impersonation! [UnsetSeDebug()]", L_DEBUG); + return 0; + } + + if (!OpenThreadToken(handleThreadId, + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, + FALSE, + &hToken) + ) { + //Log2File("Error Opening Thread Token! [UnsetSeDebug()]", L_DEBUG); + return 0; + } + } + } + + //now disable SeDebug + if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) { + //Log2File("Error unsetting SeDebug Privilege [SetPrivilege()]", L_WARN); + return 0; + } + + CloseHandle(hToken); + return 1; +} + +int runProcess(char* strDevcon, BOOL isVolume) { +// char strDevcon[_MAX_PATH]; +// strcpy_s(strDevcon, _MAX_PATH, "1.bat "); + +// for (int i = 1; i < argc; ++i) { +// strcat_s(strDevcon, _MAX_PATH, argv[i]); +// strcat_s(strDevcon, _MAX_PATH, " "); +// } + + char buf[1024 * 10]; //буфер ввода/вывода + + PROCESS_INFORMATION piProcInfo; + STARTUPINFOA siStartInfo; + SECURITY_ATTRIBUTES sa; + SECURITY_DESCRIPTOR sd; //структура security для пайпов + HANDLE newstdin, newstdout, read_stdout, write_stdin; //дескрипторы пайпов + + // if (IsWinNT()) //инициализация security для Windows NT + // { + InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); + SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); + sa.lpSecurityDescriptor = &sd; + // } + + // else sa.lpSecurityDescriptor = NULL; + + sa.nLength = sizeof(SECURITY_ATTRIBUTES); + sa.bInheritHandle = TRUE; //разрешаем наследование дескрипторов + + if (!CreatePipe(&newstdin, &write_stdin, &sa, 0)) //создаем пайп + { + printf("Error creating input channel"); + return 1; + } + + if (!CreatePipe(&read_stdout, &newstdout, &sa, 0)) //создаем пайп + // для stdout + { + printf("Error creating output channel"); + + CloseHandle(newstdin); + CloseHandle(write_stdin); + return 1; + } + ZeroMemory(&siStartInfo, sizeof(siStartInfo)); + ZeroMemory(&piProcInfo, sizeof(piProcInfo)); + GetStartupInfoA(&siStartInfo); //создаем startupinfo для дочернего процесса + siStartInfo.cb = sizeof(STARTUPINFO); + siStartInfo.wShowWindow = SW_HIDE; //прячем окно + siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + // siStartInfo.lpDesktop = NULL; + siStartInfo.hStdOutput = newstdout; + siStartInfo.hStdError = newstdout; //подменяем дескрипторы для + siStartInfo.hStdInput = newstdin; // дочернего процесса + //char app_spawn[] = "devcon.exe"; + + + int result = CreateProcessA(NULL, strDevcon, &sa, &sa, TRUE, CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS, + NULL, NULL, &siStartInfo, &piProcInfo); + + + if (!result) + { + printf("Error starting thread"); + + CloseHandle(newstdin); + CloseHandle(newstdout); + CloseHandle(read_stdout); + CloseHandle(write_stdin); + return 1; + } + + unsigned long exit = 0; //код завершения процесса + unsigned long bread; //кол-во прочитанных байт + unsigned long avail; //кол-во доступных байт + bzero(buf); + + for (;;) //основной цикл программы + { + Sleep(1); + + GetExitCodeProcess(piProcInfo.hProcess, &exit); //пока дочерний процесс + + // не закрыт + if (exit != STILL_ACTIVE) { + break; + } + + } + + + PeekNamedPipe(read_stdout, buf, sizeof(buf) - 1, &bread, &avail, NULL); + + //Проверяем, есть ли данные для чтения в stdout + + if (bread != 0) + { + bzero(buf); + + ReadFile(read_stdout, buf, avail, &bread, NULL); //читаем из пайпа stdout + + //printf("%s", buf); + + } + + int volume = 0; + + if (isVolume) { + + char bufStr[1024]; + bzero(bufStr); + int j = 0; + + for (unsigned long i = 0; i < bread; ++i) { + + if (buf[i] == 0x0A) { + printf("%s", bufStr); + + char* res_ptr = strstr(bufStr, volumeName); + + if (res_ptr) { + + char str[_MAX_PATH]; + + sscanf(bufStr, "%s %d", str, &volume); + + //volume = atoi(&bufStr[10]); + } + + bzero(bufStr); + j = 0; + } + + bufStr[j] = buf[i]; + ++j; + + } + + } + + + // KillProcByPid(piProcInfo.dwProcessId); + + CloseHandle(piProcInfo.hThread); + CloseHandle(piProcInfo.hProcess); // в stdin + CloseHandle(newstdin); + CloseHandle(newstdout); + CloseHandle(read_stdout); + CloseHandle(write_stdin); + + return volume; +} + +void CreateScriptFile(char* text) { + FILE* file = fopen("script.txt", "w"); + fprintf(file, text); + fclose(file); +} + +void CreateBatFile(char* text) { + FILE* file = fopen("script.bat", "w"); + fprintf(file, text); + fclose(file); +} + +DWORD CurrentProcessId; + +// Функция обратного вызова для EnumWindows +BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam) { + DWORD processId; + GetWindowThreadProcessId(hWnd, &processId); + + if (processId == CurrentProcessId) { + // HWND найден, передаем его через lParam + *(HWND*)lParam = hWnd; + return FALSE; // Завершаем перебор окон + } + + return TRUE; // Продолжаем перебор окон +} + +HWND GetWindowHwndByProcessId() { + HWND hWnd = NULL; + + // Перебираем все окна + EnumWindows(EnumWindowProc, (LPARAM)&hWnd); + + return hWnd; +} + +int main(int argc, char* argv[]) { + +// if (!setSeDebug(GetCurrentThread())) { +// printf("Error", "Could not set Debug Privilege"); +// return 1; +// } + + + CurrentProcessId = GetCurrentProcessId(); + + HWND WindowHandle = GetWindowHwndByProcessId(); + + if (WindowHandle) { + ShowWindow(WindowHandle, SW_HIDE); + } + + + bzero(volumeName); + + FILE* file = fopen("volume", "r"); + fscanf(file, "%s", volumeName); + fclose(file); + + char current_work_dir_mount[FILENAME_MAX]; + _getcwd(current_work_dir_mount, sizeof(current_work_dir_mount)); + strcat(current_work_dir_mount, "\\mount"); + + char current_work_dir_vhd[FILENAME_MAX]; + _getcwd(current_work_dir_vhd, sizeof(current_work_dir_vhd)); + strcat(current_work_dir_vhd, "\\image.vhd"); + + char scriptCom[FILENAME_MAX]; + + // readonly + if (access("readonly", 0) == 0) { + sprintf(scriptCom, "select vdisk file=\"%s\"\r\nattach vdisk readonly", current_work_dir_vhd); + } + else { + sprintf(scriptCom, "select vdisk file=\"%s\"\r\nattach vdisk", current_work_dir_vhd); + } + + CreateScriptFile(scriptCom); + runProcess("diskpart.exe /s script.txt", FALSE); + + CreateScriptFile("list volume"); + int volume = runProcess("diskpart.exe /s script.txt", TRUE); + + if (volume > 0) { + sprintf(scriptCom, "select volume %d\r\nassign mount=\"%s\"", volume, current_work_dir_mount); + CreateScriptFile(scriptCom); + runProcess("diskpart.exe /s script.txt", FALSE); + + CreateScriptFile(""); + + CreateBatFile("cd mount\r\nrun.bat"); + runProcess("script.bat", FALSE); + + CreateBatFile(""); + + sprintf(scriptCom, "select volume %d\r\nremove mount=\"%s\"", volume, current_work_dir_mount); + CreateScriptFile(scriptCom); + runProcess("diskpart.exe /s script.txt", FALSE); + + sprintf(scriptCom, "select vdisk file=\"%s\"\r\ndetach vdisk", current_work_dir_vhd); + CreateScriptFile(scriptCom); + runProcess("diskpart.exe /s script.txt", FALSE); + + CreateScriptFile(""); + + } + else { + printf("Virtual Disk not Found"); + + exit(1); + } + +} + + + + +// Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки" +// Отладка программы: F5 или меню "Отладка" > "Запустить отладку" + +// Советы по началу работы +// 1. В окне обозревателя решений можно добавлять файлы и управлять ими. +// 2. В окне Team Explorer можно подключиться к системе управления версиями. +// 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения. +// 4. В окне "Список ошибок" можно просматривать ошибки. +// 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода. +// 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл. diff --git a/Run.sln b/Run.sln new file mode 100644 index 0000000..39ff6a6 --- /dev/null +++ b/Run.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34728.123 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "InstallDrv", "InstallDrv.vcxproj", "{031654C4-F73B-4465-8EC4-F4DC348FBB25}" +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 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Debug|x64.ActiveCfg = Debug|x64 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Debug|x64.Build.0 = Debug|x64 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Debug|x86.ActiveCfg = Debug|Win32 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Debug|x86.Build.0 = Debug|Win32 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Release|x64.ActiveCfg = Release|x64 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Release|x64.Build.0 = Release|x64 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Release|x86.ActiveCfg = Release|Win32 + {031654C4-F73B-4465-8EC4-F4DC348FBB25}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8D68BD9B-5CC1-4310-AD87-9912C280C571} + EndGlobalSection +EndGlobal diff --git a/Run/Release/InstallDrv.log b/Run/Release/InstallDrv.log new file mode 100644 index 0000000..1e3d78f --- /dev/null +++ b/Run/Release/InstallDrv.log @@ -0,0 +1,9 @@ + Run.c +C:\VC\InstallDrv\Run.c(310,5): warning C4013: _getcwd: нет определения; предполагается "extern" с возвратом типа "int" +C:\VC\InstallDrv\Run.c(320,9): warning C4013: access: нет определения; предполагается "extern" с возвратом типа "int" + Создание кода + Создание кода завершено + 1 of 18 functions ( 5.6%) were compiled, the rest were copied from previous compilation. + 0 functions were new in current compilation + 0 functions had inline decision re-evaluated but remain unchanged + InstallDrv.vcxproj -> C:\VC\InstallDrv\Release\Run.exe diff --git a/Run/Release/Run.exe.recipe b/Run/Release/Run.exe.recipe new file mode 100644 index 0000000..1eea8fa --- /dev/null +++ b/Run/Release/Run.exe.recipe @@ -0,0 +1,11 @@ + + + + + C:\VC\InstallDrv\Release\Run.exe + + + + + + \ No newline at end of file diff --git a/Run/Release/Run.iobj b/Run/Release/Run.iobj new file mode 100644 index 0000000..20e8d1d Binary files /dev/null and b/Run/Release/Run.iobj differ diff --git a/Run/Release/Run.ipdb b/Run/Release/Run.ipdb new file mode 100644 index 0000000..4bb5666 Binary files /dev/null and b/Run/Release/Run.ipdb differ diff --git a/Run/Release/Run.obj b/Run/Release/Run.obj new file mode 100644 index 0000000..7a9f194 Binary files /dev/null and b/Run/Release/Run.obj differ diff --git a/Run/Release/Run.tlog/CL.command.1.tlog b/Run/Release/Run.tlog/CL.command.1.tlog new file mode 100644 index 0000000..c46cc73 Binary files /dev/null and b/Run/Release/Run.tlog/CL.command.1.tlog differ diff --git a/Run/Release/Run.tlog/CL.read.1.tlog b/Run/Release/Run.tlog/CL.read.1.tlog new file mode 100644 index 0000000..a476842 Binary files /dev/null and b/Run/Release/Run.tlog/CL.read.1.tlog differ diff --git a/Run/Release/Run.tlog/CL.write.1.tlog b/Run/Release/Run.tlog/CL.write.1.tlog new file mode 100644 index 0000000..95c6638 Binary files /dev/null and b/Run/Release/Run.tlog/CL.write.1.tlog differ diff --git a/Run/Release/Run.tlog/Cl.items.tlog b/Run/Release/Run.tlog/Cl.items.tlog new file mode 100644 index 0000000..70ddab2 --- /dev/null +++ b/Run/Release/Run.tlog/Cl.items.tlog @@ -0,0 +1 @@ +C:\VC\InstallDrv\Run.c;C:\VC\InstallDrv\Run\Release\Run.obj diff --git a/Run/Release/Run.tlog/Run.lastbuildstate b/Run/Release/Run.tlog/Run.lastbuildstate new file mode 100644 index 0000000..904300b --- /dev/null +++ b/Run/Release/Run.tlog/Run.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v143:VCToolArchitecture=Native32Bit:VCToolsVersion=14.39.33519:TargetPlatformVersion=10.0.22621.0: +Release|Win32|C:\VC\InstallDrv\| diff --git a/Run/Release/Run.tlog/link.command.1.tlog b/Run/Release/Run.tlog/link.command.1.tlog new file mode 100644 index 0000000..dde784b Binary files /dev/null and b/Run/Release/Run.tlog/link.command.1.tlog differ diff --git a/Run/Release/Run.tlog/link.read.1.tlog b/Run/Release/Run.tlog/link.read.1.tlog new file mode 100644 index 0000000..87266a2 Binary files /dev/null and b/Run/Release/Run.tlog/link.read.1.tlog differ diff --git a/Run/Release/Run.tlog/link.secondary.1.tlog b/Run/Release/Run.tlog/link.secondary.1.tlog new file mode 100644 index 0000000..97b3eda --- /dev/null +++ b/Run/Release/Run.tlog/link.secondary.1.tlog @@ -0,0 +1,3 @@ +^C:\VC\INSTALLDRV\RUN\RELEASE\RUN.OBJ +C:\VC\InstallDrv\Run\Release\Run.IPDB +C:\VC\InstallDrv\Run\Release\Run.iobj diff --git a/Run/Release/Run.tlog/link.write.1.tlog b/Run/Release/Run.tlog/link.write.1.tlog new file mode 100644 index 0000000..4f9338b Binary files /dev/null and b/Run/Release/Run.tlog/link.write.1.tlog differ diff --git a/Run/Release/vc143.pdb b/Run/Release/vc143.pdb new file mode 100644 index 0000000..2960b45 Binary files /dev/null and b/Run/Release/vc143.pdb differ diff --git a/script.bat b/script.bat new file mode 100644 index 0000000..e69de29 diff --git a/script.txt b/script.txt new file mode 100644 index 0000000..e69de29