Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to unload? #3

Open
lqqqc opened this issue Dec 10, 2024 · 0 comments
Open

how to unload? #3

lqqqc opened this issue Dec 10, 2024 · 0 comments

Comments

@lqqqc
Copy link

lqqqc commented Dec 10, 2024

how to unload this driver? NtUnloadDriver() can't

LSTATUS UnloadDriver(CONST LPSTR aService) {
	LSTATUS ret{ -1 };
	HMODULE hNtdll{ 0 };
	LPSTR aDriverKey{ 0 };
	LSTATUS l{ 0 };
	UNICODE_STRING uDriver{ 0 };
	ANSI_STRING asDriverKey{ 0 };
	_NtUnloadDriver NtUnloadDriver = nullptr;

	hNtdll = GetModuleHandleA("Ntdll.dll");
	if (hNtdll == 0) {
		goto CLEANUP;
	}

	NtUnloadDriver = (_NtUnloadDriver)GetProcAddress(hNtdll, "NtUnloadDriver");
	if (NtUnloadDriver == nullptr) {
		goto CLEANUP;
	}

	aDriverKey = (LPSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH);
	if (aDriverKey == NULL) {
		goto CLEANUP;
	}

	_snprintf_s(aDriverKey, MAX_PATH, MAX_PATH, "\\Registry\\Machine\\System\\CurrentControlSet\\Services\\%s", aService);
	RtlInitAnsiString(&asDriverKey, aDriverKey);
	l = RtlAnsiStringToUnicodeString(&uDriver, &asDriverKey, TRUE);
	if (l) {
		goto CLEANUP;
	}

	ret = NtUnloadDriver(&uDriver);

CLEANUP:
	if (uDriver.Length != 0) {
		RtlFreeUnicodeString(&uDriver);
	}

	if (aDriverKey)
		HeapFree(GetProcessHeap(), NULL, aDriverKey);

	return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant