ZtsFw.Asm
资源名称:ZtsFw.zip [点击查看]
上传用户:lingfuwu
上传日期:2013-03-31
资源大小:13k
文件大小:10k
源码类别:
防火墙与安全工具
开发平台:
Asm
- .386
- .model flat, stdcall ;32 bit memory model
- option casemap :none ;case sensitive
- include common.inc
- include ZtsFw.inc
- .code
- start:
- invoke GetModuleHandle, NULL
- mov g_hInst, eax
- invoke _OpenDevice
- .if eax
- invoke DialogBoxParam, g_hInst, IDD_DIALOG1, NULL, addr DlgProc, NULL
- invoke InitCommonControls
- invoke _CloseDevice
- .else
- invoke MessageBox, NULL, offset szDrvError, offset szZtsFw, MB_OK + MB_ICONINFORMATION
- .endif
- invoke ExitProcess, 0
- DlgProc proc uses ebx esi edi, hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
- LOCAL buf[512] : BYTE
- LOCAL dwByteReturned
- mov eax, uMsg
- .if eax==WM_INITDIALOG
- invoke _Init, hWnd
- .elseif eax==WM_COMMAND
- mov eax, wParam
- and eax, 0ffffh
- .if eax==IDC_BTN_Exit
- invoke EndDialog, hWnd, 0
- .elseif eax==IDC_BTN_AddPorts
- xor ebx, ebx
- lea esi, buf
- mov edi, offset g_Ports
- invoke GetDlgItemText, hWnd, IDC_PORTS, esi, sizeof buf
- @001:
- xor eax, eax
- push eax
- invoke sscanf, esi, offset szFormat, esp
- pop eax
- .if eax<=0FFFFh && ax!=0 && ebx<MAX_PORTS
- rol ax, 8
- stosw
- inc ebx
- .endif
- @002:
- lodsb
- test al, al
- jz @003
- cmp al, ','
- jz @001
- jmp @002
- @003:
- add ebx, ebx
- invoke DeviceIoControl, g_hDevice, IOCTL_ADD_FILTER, offset g_Ports, ebx, 0, 0, addr dwByteReturned, 0
- .elseif eax==IDC_BTN_Start
- .if g_bStarted==0
- invoke CreateThread, NULL, 0, offset _StartRoutine, NULL, NULL, addr g_dwThreadID
- mov g_hThread, eax
- mov g_bStarted, 1
- .endif
- .elseif eax==IDC_BTN_Stop
- .if g_bStarted
- invoke EnableWindow, g_hStartBtn, TRUE
- invoke EnableWindow, g_hStopBtn, FALSE
- invoke TerminateThread, g_hThread, 0
- invoke DeviceIoControl, g_hDevice, IOCTL_STOP_IP_HOOK, 0, 0, 0, 0, addr dwByteReturned, 0
- mov g_bStarted, 0
- .endif
- .endif
- .elseif eax==WM_PAINT
- invoke _Paint, hWnd
- .elseif eax==WM_CLOSE
- invoke EndDialog, hWnd, 0
- .else
- mov eax, FALSE
- ret
- .endif
- mov eax, TRUE
- ret
- DlgProc endp
- _StartRoutine proc uses ebx esi edi, lParam:DWORD
- LOCAL FwEvent
- LOCAL stIpInfo : CALLBACK_IP_INFO
- LOCAL dwByteReturned
- invoke EnableWindow, g_hStartBtn, FALSE
- invoke EnableWindow, g_hStopBtn, TRUE
- invoke DeviceIoControl, g_hDevice, IOCTL_START_IP_HOOK, 0, 0, 0, 0, addr dwByteReturned, 0
- invoke OpenEvent, SYNCHRONIZE, 0, addr szFwEvent
- mov FwEvent, eax
- xor eax, eax
- .while TRUE
- invoke WaitForSingleObject, FwEvent, INFINITE
- invoke DeviceIoControl, g_hDevice, IOCTL_GET_IP_INFO, 0, 0, addr stIpInfo, sizeof stIpInfo, addr dwByteReturned, 0
- invoke _InsertIpInfo, addr stIpInfo
- .endw
- ret
- _StartRoutine endp
- _InsertIpInfo proc uses ebx esi edi, lpIpInfo:DWORD
- LOCAL lvi : LV_ITEM
- LOCAL buf[256] : BYTE
- mov esi, lpIpInfo
- assume esi : ptr CALLBACK_IP_INFO
- ;Protocol
- mov lvi.imask, LVIF_TEXT
- m2m lvi.iItem, g_dwNum
- mov lvi.iSubItem, 0
- mov lvi.pszText, CTXT("TCP")
- invoke SendMessage, g_hList, LVM_INSERTITEM, 0, addr lvi
- inc g_dwNum
- ;Source IP
- mov lvi.iSubItem, 1
- mov eax, [esi].srcIp
- invoke inet_ntoa, eax
- mov lvi.pszText, eax
- invoke SendMessage, g_hList, LVM_SETITEM, 0, addr lvi
- ;Source Port
- mov lvi.iSubItem, 2
- xor eax, eax
- mov ax, [esi].srcPort
- rol ax, 8
- lea edi, buf
- invoke wsprintf, edi, offset szFormat, eax
- mov lvi.pszText, edi
- invoke SendMessage, g_hList, LVM_SETITEM, 0, addr lvi
- ;Destination IP
- mov lvi.iSubItem, 3
- mov eax, [esi].dstIp
- invoke inet_ntoa, eax
- mov lvi.pszText, eax
- invoke SendMessage, g_hList, LVM_SETITEM, 0, addr lvi
- ;Destination Port
- mov lvi.iSubItem, 4
- xor eax, eax
- mov ax, [esi].dstPort
- rol ax, 8
- lea edi, buf
- invoke wsprintf, edi, offset szFormat, eax
- mov lvi.pszText, edi
- invoke SendMessage, g_hList, LVM_SETITEM, 0, addr lvi
- ;Flags
- mov lvi.iSubItem, 5
- mov eax, [esi].bIn
- .if eax
- mov eax, CTXT("In")
- .else
- mov eax, CTXT("Out")
- .endif
- mov lvi.pszText, eax
- invoke SendMessage, g_hList, LVM_SETITEM, 0, addr lvi
- ret
- _InsertIpInfo endp
- ;安装驱动程序
- _OpenDevice proc uses ebx esi edi
- LOCAL hSCManager
- LOCAL hService
- LOCAL buf[MAX_PATH] : byte
- LOCAL szDriverPath[MAX_PATH] : byte
- LOCAL hResInfo, dwResSize, lpRes, hFile, dwResult
- invoke GetEnvironmentVariable, CTXT("TEMP"), addr g_szDrvFile, MAX_PATH
- invoke lstrcat, addr g_szDrvFile, CTXT("drvZtsFw.sys")
- invoke FindResource, g_hInst, ID_DRVFILE, RT_RCDATA
- test eax, eax
- jz exit_0
- mov hResInfo, eax
- invoke SizeofResource, g_hInst, eax
- mov dwResSize, eax
- invoke LoadResource, g_hInst, hResInfo
- test eax, eax
- jz exit_0
- invoke LockResource, eax
- test eax, eax
- jz exit_0
- mov lpRes, eax
- invoke CreateFile, addr g_szDrvFile, GENERIC_WRITE, FILE_SHARE_READ,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL
- cmp eax, INVALID_HANDLE_VALUE
- jz exit_0
- mov hFile, eax
- invoke WriteFile, hFile, lpRes, dwResSize, addr dwResult, NULL
- invoke CloseHandle, hFile
- ;打开驱动链接
- invoke CreateFile, CTXT("\.slZtsFw"), GENERIC_READ+GENERIC_WRITE,
- FILE_SHARE_READ+FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL
- .if eax!=INVALID_HANDLE_VALUE
- mov g_hDevice, eax
- ret
- .endif
- ;如果上面的打开失败,则说明驱动没有安装或者没有启动
- invoke OpenSCManager, NULL, NULL, SC_MANAGER_CREATE_SERVICE
- .if eax!=0
- mov hSCManager, eax
- ;如果驱动已经安装了,则启动驱动程序
- invoke OpenService, hSCManager, CTXT("drvZtsFw"), SERVICE_START+DELETE
- .if eax!=0
- mov hService, eax
- invoke StartService, hService, 0, NULL
- invoke CloseServiceHandle, hService
- ;如果驱动程序没有安装,则先安装,再启动
- .else
- invoke CreateService, hSCManager, CTXT("drvZtsFw"), CTXT("ZTS's Simple Firewall Service"),
- SERVICE_START+DELETE, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,
- SERVICE_ERROR_IGNORE, addr g_szDrvFile, NULL, NULL, NULL, NULL, NULL
- .if eax!=0
- mov hService, eax
- invoke StartService, hService, 0, NULL
- invoke CloseServiceHandle, hService
- .endif
- .endif
- invoke CloseServiceHandle, hSCManager
- .endif
- ;启动驱动程序后,再一次打开驱动链接,如果不出意外,这一次应该可以成功
- invoke CreateFile, CTXT("\.slZtsFw"), GENERIC_READ+GENERIC_WRITE,
- FILE_SHARE_READ+FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL
- .if eax!=INVALID_HANDLE_VALUE
- mov g_hDevice, eax
- .else
- xor eax, eax
- .endif
- ret
- exit_0:
- xor eax, eax
- ret
- _OpenDevice endp
- ;卸载驱动程序
- _CloseDevice proc uses ebx esi edi
- LOCAL hSCManager
- LOCAL hService
- LOCAL sest : SERVICE_STATUS
- .if g_hDevice
- invoke CloseHandle, g_hDevice
- .endif
- invoke OpenSCManager, NULL, NULL, SC_MANAGER_CONNECT
- .if eax!=0
- mov hSCManager, eax
- invoke OpenService, hSCManager, CTXT("drvZtsFw"), SERVICE_STOP+DELETE
- .if eax!=0
- mov hService, eax
- invoke ControlService, hService, SERVICE_CONTROL_STOP, addr sest
- invoke DeleteService, hService
- invoke CloseServiceHandle, hService
- .endif
- invoke CloseServiceHandle, hSCManager
- .endif
- invoke DeleteFile, addr g_szDrvFile
- ret
- _CloseDevice endp
- _ListViewProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
- mov eax, uMsg
- .if eax == WM_CONTEXTMENU
- invoke SendMessage, g_hList, LVM_GETITEMCOUNT, 0, 0
- .if eax != 0
- mov eax, lParam
- mov ecx, eax
- and eax, 0FFFFh
- shr ecx, 16
- invoke TrackPopupMenu, g_hListMenu, TPM_LEFTALIGN, eax, ecx, NULL, hWnd, NULL
- .endif
- .elseif eax == WM_COMMAND
- mov eax, wParam
- and eax, 0ffffh
- .if eax == IDM_CLEAR_LOG
- invoke SendMessage, g_hList, LVM_DELETEALLITEMS, 0, 0
- mov g_dwNum, 0
- .endif
- .endif
- invoke CallWindowProc, g_lpListViewOldProc, hWnd, uMsg, wParam, lParam
- ret
- _ListViewProc endp
- _Init proc uses ebx esi edi, hWnd:HWND
- LOCAL lvc:LV_COLUMN
- mov g_bStarted, 0
- mov g_dwNum, 0
- invoke CreateFont, 16, 6, 0, 0, FW_NORMAL, FALSE, FALSE, 0, 0, 0, 0, 0, 0, CTXT("Tahoma")
- mov g_hFont, eax
- invoke GetDlgItem, hWnd, IDC_BTN_Start
- mov g_hStartBtn, eax
- invoke GetDlgItem, hWnd, IDC_BTN_Stop
- mov g_hStopBtn, eax
- invoke EnableWindow, g_hStopBtn, FALSE
- invoke GetDlgItem, hWnd, IDC_LIST
- mov g_hList, eax
- mov lvc.imask, LVCF_TEXT+LVCF_WIDTH
- mov lvc.pszText, CTXT("Protocol")
- mov lvc.lx, 60
- invoke SendMessage, g_hList, LVM_INSERTCOLUMN, 0, addr lvc
- mov lvc.pszText, CTXT("Source IP")
- mov lvc.lx, 160
- invoke SendMessage, g_hList, LVM_INSERTCOLUMN, 1, addr lvc
- mov lvc.pszText, CTXT("S.Port")
- mov lvc.lx, 60
- invoke SendMessage, g_hList, LVM_INSERTCOLUMN, 2, addr lvc
- mov lvc.pszText, CTXT("Destination IP")
- mov lvc.lx, 160
- invoke SendMessage, g_hList, LVM_INSERTCOLUMN, 3, addr lvc
- mov lvc.pszText, CTXT("D.Port")
- mov lvc.lx, 60
- invoke SendMessage, g_hList, LVM_INSERTCOLUMN, 4, addr lvc
- mov lvc.pszText, CTXT("Flags")
- mov lvc.lx, 120
- invoke SendMessage, g_hList, LVM_INSERTCOLUMN, 5, addr lvc
- invoke SendMessage, g_hList, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT
- invoke CreatePopupMenu
- mov g_hListMenu, eax
- invoke AppendMenu, g_hListMenu, MF_STRING, IDM_CLEAR_LOG, CTXT("&Clear log")
- invoke SetWindowLong, g_hList, GWL_WNDPROC, offset _ListViewProc
- mov g_lpListViewOldProc, eax
- ret
- _Init endp
- _Paint proc hWnd:HWND
- LOCAL hdc : HDC
- LOCAL ps : PAINTSTRUCT
- LOCAL rect : RECT
- LOCAL pt : POINT
- pushad
- ;根据最左边的一个控件和最下边的一个控件来计算左下角的位置
- invoke BeginPaint, hWnd, addr ps
- mov hdc, eax
- invoke GetDlgItem, hWnd, IDC_STC1
- mov ebx, eax
- invoke GetWindowRect, ebx, addr rect
- mov ebx, rect.left
- invoke GetDlgItem, hWnd, IDC_BTN_Exit
- mov esi, eax
- invoke GetWindowRect, esi, addr rect
- m2m pt.x, rect.left
- m2m pt.y, rect.bottom
- invoke ScreenToClient, hWnd, addr pt
- m2m rect.right, pt.x
- m2m rect.bottom, pt.y
- m2m pt.x, ebx
- m2m pt.y, rect.top
- invoke ScreenToClient, hWnd, addr pt
- m2m rect.left, pt.x
- m2m rect.top, pt.y
- ;画上标志,用不同的颜色画两次,产生立体效果
- invoke SetBkMode, hdc, TRANSPARENT
- invoke SelectObject, hdc, g_hFont
- invoke GetSysColor, COLOR_3DHILIGHT
- invoke SetTextColor, hdc, eax
- invoke DrawText, hdc, offset szLogo, sizeof szLogo, addr rect, DT_VCENTER or DT_LEFT or DT_SINGLELINE
- invoke GetSysColor, COLOR_3DSHADOW
- invoke SetTextColor, hdc, eax
- invoke OffsetRect, addr rect, -1, -1
- invoke DrawText, hdc, offset szLogo, sizeof szLogo, addr rect, DT_VCENTER or DT_LEFT or DT_SINGLELINE
- invoke EndPaint, hWnd, addr ps
- popad
- ret
- _Paint endp
- end start