⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kmdmanager.asm

📁 KmdKit 汇编下开发驱动的工具 由俄罗斯某强男开发
💻 ASM
📖 第 1 页 / 共 2 页
字号:

; KmdManager - utility for simplify kmd un/loading and sending control codes
; Written by Four-F (four-f@mail.ru)

.386
.model flat, stdcall
option casemap:none

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                  I N C L U D E   F I L E S                                        
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

include \masm32\include\windows.inc

include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\advapi32.inc
include \masm32\include\comdlg32.inc
include \masm32\include\shell32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\shell32.lib

include Macros.mac
include \masm32\Macros\Strings.mac
include \masm32\cocomac\cocomac.mac
include \masm32\cocomac\ListView.mac
include \masm32\cocomac\Header.mac
include htodw.asm
include memory.asm
include string.asm
include MaskedEdit.asm
include theme.asm

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                         F U N C T I O N S   P R O T O T Y P E S                                   
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

DlgProc proto :HWND, :UINT, :WPARAM, :LPARAM

IDD_DIALOG				equ		1000
IDB_BROWSE				equ		1001
IDB_REGISTER			equ		1002
IDB_RUN					equ		1003
IDB_IOCONTROL			equ		1004
IDB_UNREGISTER			equ		1005
IDB_STOP				equ		1006

IDB_OPTIONS				equ		1007
IDB_ABOUT				equ		1008
IDB_EXIT				equ		1009

IDCHK_REGTORUNLINK		equ		1010
IDCHK_UNREGTOSTOPLINK	equ		1011
IDCHK_IOCONTROLLINK		equ		1012

IDE_PATH				equ		1020
IDE_CONTROL_CODE		equ		1021

IDC_REPORT_LIST			equ		1030

IDI_ICON				equ		2000

IDM_CLEAR_LOG			equ		5000

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                      U S E R   D E F I N E D   S T R U C T U R E S                                
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                     C O N S T A N T S                                             
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

.const
g_szFilterString		db "Kernel-Mode Drivers", 0, "*.sys", 0
						db "All Files", 0, "*.*", 0, 0

g_szOpenDriverTitle		db "Choose Driver", 0

g_szSuccess				db "Success", 0
g_szFail				db "Fail", 0
g_szCriticalError		db "Critical Error", 0
g_szOpenSCManagerError	db "Can't get Service Control Manager handle.", 0
g_szEnterFullDriverPath	db "Enter full driver path.", 0

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                              I N I T I A L I Z E D  D A T A                                       
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

.data

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                              U N I N I T I A L I Z E D  D A T A                                   
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

.data?
g_hInstance				HINSTANCE	?
;g_pfnPrevStaticProc		LPVOID		?
g_hwndEditDriverPath	HWND		?
g_hwndEditControlCode	HWND		?
g_hwndReportListView	HWND		?

g_hwndButtonRegister	HWND		?
g_hwndButtonRun			HWND		?
g_hwndButtonControl		HWND		?
g_hwndButtonStop		HWND		?
g_hwndButtonUnregister	HWND		?
g_hListViewPopupMenu	HMENU		?
g_hwndCheckRegToRun		HWND		?
g_hwndCheckUnregToStop	HWND		?
g_hwndCheckLinkAll		HWND		?

g_pfnListViewProcPrev	LPVOID		?

g_acErrorDescription	CHAR	256	dup(?)


g_dwDlgMinHeight		DWORD		?
g_dwDlgMaxHeight		DWORD		?

g_dwDlgWidth			DWORD		?

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                       C O D E                                                     
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

.code

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                        LastError                                                  
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

LastError proc; pacBuffer:LPVOID

    pushfd
    pushad
    
    invoke GetLastError
    push eax

	invoke RtlZeroMemory, offset g_acErrorDescription, sizeof g_acErrorDescription
	pop eax
    mov ecx, SUBLANG_DEFAULT
    shl ecx, 10
    add ecx, LANG_NEUTRAL               ; MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) User default language
    
    invoke FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM + FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, \
    						eax, ecx, offset g_acErrorDescription, 128, NULL

    .if eax == 0
		invoke lstrcpy, offset g_acErrorDescription, $CTA0("Error number not found.")
    .endif

    popad
    popfd
    
    ret

LastError endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                      ReportStatus                                                 
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

ReportStatus proc uses esi pszDriverName:LPSTR, pszOperation:LPSTR, pszStatus:LPSTR, pszLastError:LPSTR

LOCAL lvi:LV_ITEM

	mov lvi.imask, LVIF_TEXT 
	m2m lvi.pszText,pszDriverName
	and lvi.iSubItem, 0
	ListView_GetItemCount g_hwndReportListView
	mov esi, eax
	mov lvi.iItem, eax

	ListView_InsertItem g_hwndReportListView, addr lvi	

	ListView_SetItemText g_hwndReportListView, esi, 1, pszOperation
	ListView_SetItemText g_hwndReportListView, esi, 2, pszStatus
	ListView_SetItemText g_hwndReportListView, esi, 3, pszLastError

	; Make it fully visible
	ListView_EnsureVisible g_hwndReportListView, esi, FALSE

	ret

ReportStatus endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                     RegisterDriver                                                
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

RegisterDriver proc uses esi edi ebx pszDriverName:LPSTR, pszDriverPath:LPSTR

	xor ebx, ebx		; assume error
	mov edi, offset g_szFail

	invoke OpenSCManager, NULL, NULL, SC_MANAGER_CREATE_SERVICE
	.if eax != NULL
		mov esi, eax

		; Register driver - fill registry directory
		invoke CreateService, esi, pszDriverName, pszDriverName, \
					0, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, \
					pszDriverPath, NULL, NULL, NULL, NULL, NULL

		invoke LastError

		.if eax != NULL
			invoke CloseServiceHandle, eax
			inc ebx					; success
			mov edi, offset g_szSuccess
		.endif
		invoke CloseServiceHandle, esi
	.else
		invoke MessageBox, NULL, addr g_szOpenSCManagerError, addr g_szCriticalError, MB_OK + MB_ICONSTOP
	.endif

	invoke ReportStatus, pszDriverName, $CTA0("Register"), edi, offset g_acErrorDescription

	return ebx

RegisterDriver endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                    UnregisterDriver                                               
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

UnregisterDriver proc uses esi edi ebx pszDriverName:LPSTR

	xor ebx, ebx		; assume error
	mov edi, offset g_szFail

	invoke OpenSCManager, NULL, NULL, SC_MANAGER_CONNECT
	.if eax != NULL
		mov esi, eax
		
		; Unregister driver - remove registry directory
		invoke OpenService, esi, pszDriverName, DELETE

		invoke LastError

		.if eax != NULL
			push eax
			invoke DeleteService, eax

			invoke LastError

			.if eax != 0
				inc ebx					; success
				mov edi, offset g_szSuccess
			.endif
			call CloseServiceHandle
		.endif

		invoke CloseServiceHandle, esi
	.else
		invoke MessageBox, NULL, addr g_szOpenSCManagerError, addr g_szCriticalError, MB_OK + MB_ICONSTOP
	.endif

	invoke ReportStatus, pszDriverName, $CTA0("Unregister"), edi, offset g_acErrorDescription

	return ebx

UnregisterDriver endp


;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                       RunDriver                                                   
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

RunDriver proc uses esi edi ebx pszDriverName:LPSTR

	xor ebx, ebx		; assume error
	mov edi, offset g_szFail

	invoke OpenSCManager, NULL, NULL, SC_MANAGER_CONNECT
	.if eax != NULL
		mov esi, eax
		
		; Unregister driver - remove registry directory
		invoke OpenService, esi, pszDriverName, SERVICE_START

		invoke LastError

		.if eax != NULL
			push eax
			invoke StartService, eax, 0, NULL
			
			invoke LastError
			
			.if eax != 0
				inc ebx					; success
				mov edi, offset g_szSuccess
			.endif

			call CloseServiceHandle
			mov edi, offset g_szSuccess
		.endif
		invoke CloseServiceHandle, esi
	.else
		invoke MessageBox, NULL, addr g_szOpenSCManagerError, addr g_szCriticalError, MB_OK + MB_ICONSTOP
	.endif

	invoke ReportStatus, pszDriverName, $CTA0("Start"), edi, offset g_acErrorDescription

	return ebx

RunDriver endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                       StopDriver                                                  
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

StopDriver proc uses esi ebx pszDriverName:LPSTR

LOCAL sest:SERVICE_STATUS

	xor ebx, ebx		; assume error
	mov edi, offset g_szFail

	invoke OpenSCManager, NULL, NULL, SC_MANAGER_CONNECT
	.if eax != NULL
		mov esi, eax
		
		; Unregister driver - remove registry directory
		invoke OpenService, esi, pszDriverName, SERVICE_STOP

		invoke LastError

		.if eax != NULL
			push eax
			mov ecx, eax
			invoke ControlService, ecx, SERVICE_CONTROL_STOP, addr sest
			
			invoke LastError

			.if eax != 0
				inc ebx					; success
				mov edi, offset g_szSuccess
			.endif
			call CloseServiceHandle
		.endif

		invoke CloseServiceHandle, esi
	.else
		invoke MessageBox, NULL, addr g_szOpenSCManagerError, addr g_szCriticalError, MB_OK + MB_ICONSTOP
	.endif

	invoke ReportStatus, pszDriverName, $CTA0("Stop"), edi, offset g_acErrorDescription

	return ebx

StopDriver endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                      ControlDevice                                                
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

ControlDriver proc uses esi edi ebx pszDriverName:LPSTR, dwCode:DWORD

LOCAL acBuffer[MAX_PATH]:CHAR
LOCAL dwBytesReturned:DWORD

	xor ebx, ebx		; assume error
	mov edi, offset g_szFail

	invoke GetVersion
	.if al >= 5
		mov eax, $CTA0("\\\\.\\Global\\%s")
	.else
		mov eax, $CTA0("\\\\.\\%s")
	.endif
	invoke wsprintf, addr acBuffer, eax, pszDriverName

	invoke CreateFile, addr acBuffer, GENERIC_READ + GENERIC_WRITE, 0, \
				NULL, OPEN_EXISTING, 0, NULL

	invoke LastError

	.if eax != INVALID_HANDLE_VALUE
		mov esi, eax
		invoke DeviceIoControl, esi, dwCode, NULL, 0, NULL, 0, addr dwBytesReturned, NULL

		invoke LastError

		.if eax != 0
			inc ebx					; success
			mov edi, offset g_szSuccess
		.endif
		invoke CloseHandle, esi
	.else
		invoke MessageBox, NULL, $CTA0("Can't get Driver handle."), addr g_szCriticalError, MB_OK + MB_ICONSTOP
	.endif

	invoke ReportStatus, pszDriverName, $CTA0("Control"), edi, offset g_acErrorDescription

	return ebx

ControlDriver endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                  GetDriverNameFromPath                                            
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

GetDriverNameFromPath proc uses esi edi ebx pDriverPath:LPSTR, pBuffer:LPVOID

	xor ebx, ebx	; assume error

	mov edi, pDriverPath
	mov esi, edi
	invoke lstrlen, edi
	add esi, eax
	sub esi, 4			; ".sys"
	invoke lstrcmpi, $CTA0(".sys"), esi
	.if eax == 0
		xor ecx, ecx
		dec esi

	    .while esi > edi
		    mov al, [esi]
	    	.break .if al == '\'
		    inc ecx
		    dec esi
		.endw

		.if esi != edi
			inc esi
			mov edi, pBuffer
			rep movsb
		    mov byte ptr [edi], 0
		    inc ebx				; success
		.else
			invoke MessageBox, NULL, $CTA0("Can't extract Driver Name.\nYou have to specify full path."), \
									NULL, MB_OK + MB_ICONSTOP
		.endif

	.else
		invoke MessageBox, NULL, $CTA0("Can't recognize Driver Name.\nThe file extension must be '.sys'."), \
									NULL, MB_OK + MB_ICONSTOP
	.endif

	return ebx

GetDriverNameFromPath endp

;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;                                  InsertReportListColumns                                          
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

InsertReportListColumns proc hwndListView:HWND
	
LOCAL lvc:LV_COLUMN
LOCAL lvi:LV_ITEM
	
	ListView_SetExtendedListViewStyle hwndListView, LVS_EX_GRIDLINES + LVS_EX_FULLROWSELECT

	mov lvc.imask, LVCF_TEXT + LVCF_WIDTH + LVCF_FMT
	mov lvc.fmt, LVCFMT_LEFT
	mov lvc.pszText, $TA0("Driver")
	mov lvc.lx, 60
	ListView_InsertColumn hwndListView, 0, addr lvc

	mov lvc.pszText, $TA0("Operation")
	ListView_InsertColumn hwndListView, 1, addr lvc

	mov lvc.pszText, $TA0("Status")
	ListView_InsertColumn hwndListView, 2, addr lvc

	mov lvc.lx, 400
	mov lvc.pszText, $TA0("Last Error")
	ListView_InsertColumn hwndListView, 3, addr lvc

	ret

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -