首页 > 开发 > 综合 > 正文

在VB中验证Windows和域用户

2024-07-21 02:27:41
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

sample code
the following visual basic module contains a public function called sspvalidateuser(). this function attempts to validate the supplied user name, domain name, and password by using sspi functions.

' module name: ssplogon.bas
option explicit

private const heap_zero_memory = &h8

private const sec_winnt_auth_identity_ansi = &h1

private const secbuffer_token = &h2

private const security_native_drep = &h10

private const secpkg_cred_inbound = &h1
private const secpkg_cred_outbound = &h2

private const sec_i_continue_needed = &h90312
private const sec_i_complete_needed = &h90313
private const sec_i_complete_and_continue = &h90314

private const ver_platform_win32_nt = &h2

type secpkginfo
fcapabilities as long
wversion as integer
wrpcid as integer
cbmaxtoken as long
name as long
comment as long
end type

type sechandle
dwlower as long
dwupper as long
end type

type auth_seq
finitialized as boolean
fhavecredhandle as boolean
fhavectxthandle as boolean
hcred as sechandle
hctxt as sechandle
end type

type sec_winnt_auth_identity
user as string
userlength as long
domain as string
domainlength as long
password as string
passwordlength as long
flags as long
end type

type timestamp
lowpart as long
highpart as long
end type

type secbuffer
cbbuffer as long
buffertype as long
pvbuffer as long
end type

type secbufferdesc
ulversion as long
cbuffers as long
pbuffers as long
end type

public type osversioninfo
dwosversioninfosize as long
dwmajorversion as long
dwminorversion as long
dwbuildnumber as long
dwplatformid as long
szcsdversion as string * 128
end type

private declare sub copymemory lib "kernel32" alias "rtlmovememory" _
(destination as any, source as any, byval length as long)

private declare function nt4querysecuritypackageinfo lib "security" _
alias "querysecuritypackageinfoa" (byval packagename as string, _
byref ppackageinfo as long) as long

private declare function querysecuritypackageinfo lib "secur32" _
alias "querysecuritypackageinfoa" (byval packagename as string, _
byref ppackageinfo as long) as long

private declare function nt4freecontextbuffer lib "security" _
alias "freecontextbuffer" (byval pvcontextbuffer as long) as long

private declare function freecontextbuffer lib "secur32" _
(byval pvcontextbuffer as long) as long

private declare function nt4initializesecuritycontext lib "security" _
alias "initializesecuritycontexta" _
(byref phcredential as sechandle, byref phcontext as sechandle, _
byval psztargetname as long, byval fcontextreq as long, _
byval reserved1 as long, byval targetdatarep as long, _
byref pinput as secbufferdesc, byval reserved2 as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function initializesecuritycontext lib "secur32" _
alias "initializesecuritycontexta" _
(byref phcredential as sechandle, byref phcontext as sechandle, _
byval psztargetname as long, byval fcontextreq as long, _
byval reserved1 as long, byval targetdatarep as long, _
byref pinput as secbufferdesc, byval reserved2 as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function nt4initializesecuritycontext2 lib "security" _
alias "initializesecuritycontexta" _
(byref phcredential as sechandle, byval phcontext as long, _
byval psztargetname as long, byval fcontextreq as long, _
byval reserved1 as long, byval targetdatarep as long, _
byval pinput as long, byval reserved2 as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function initializesecuritycontext2 lib "secur32" _
alias "initializesecuritycontexta" _
(byref phcredential as sechandle, byval phcontext as long, _
byval psztargetname as long, byval fcontextreq as long, _
byval reserved1 as long, byval targetdatarep as long, _
byval pinput as long, byval reserved2 as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function nt4acquirecredentialshandle lib "security" _
alias "acquirecredentialshandlea" (byval pszprincipal as long, _
byval pszpackage as string, byval fcredentialuse as long, _
byval pvlogonid as long, _
byref pauthdata as sec_winnt_auth_identity, _
byval pgetkeyfn as long, byval pvgetkeyargument as long, _
byref phcredential as sechandle, byref ptsexpiry as timestamp) _
as long

private declare function acquirecredentialshandle lib "secur32" _
alias "acquirecredentialshandlea" (byval pszprincipal as long, _
byval pszpackage as string, byval fcredentialuse as long, _
byval pvlogonid as long, _
byref pauthdata as sec_winnt_auth_identity, _
byval pgetkeyfn as long, byval pvgetkeyargument as long, _
byref phcredential as sechandle, byref ptsexpiry as timestamp) _
as long

private declare function nt4acquirecredentialshandle2 lib "security" _
alias "acquirecredentialshandlea" (byval pszprincipal as long, _
byval pszpackage as string, byval fcredentialuse as long, _
byval pvlogonid as long, byval pauthdata as long, _
byval pgetkeyfn as long, byval pvgetkeyargument as long, _
byref phcredential as sechandle, byref ptsexpiry as timestamp) _
as long

private declare function acquirecredentialshandle2 lib "secur32" _
alias "acquirecredentialshandlea" (byval pszprincipal as long, _
byval pszpackage as string, byval fcredentialuse as long, _
byval pvlogonid as long, byval pauthdata as long, _
byval pgetkeyfn as long, byval pvgetkeyargument as long, _
byref phcredential as sechandle, byref ptsexpiry as timestamp) _
as long

private declare function nt4acceptsecuritycontext lib "security" _
alias "acceptsecuritycontext" (byref phcredential as sechandle, _
byref phcontext as sechandle, byref pinput as secbufferdesc, _
byval fcontextreq as long, byval targetdatarep as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function acceptsecuritycontext lib "secur32" _
(byref phcredential as sechandle, _
byref phcontext as sechandle, byref pinput as secbufferdesc, _
byval fcontextreq as long, byval targetdatarep as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function nt4acceptsecuritycontext2 lib "security" _
alias "acceptsecuritycontext" (byref phcredential as sechandle, _
byval phcontext as long, byref pinput as secbufferdesc, _
byval fcontextreq as long, byval targetdatarep as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function acceptsecuritycontext2 lib "secur32" _
alias "acceptsecuritycontext" (byref phcredential as sechandle, _
byval phcontext as long, byref pinput as secbufferdesc, _
byval fcontextreq as long, byval targetdatarep as long, _
byref phnewcontext as sechandle, byref poutput as secbufferdesc, _
byref pfcontextattr as long, byref ptsexpiry as timestamp) as long

private declare function nt4completeauthtoken lib "security" _
alias "completeauthtoken" (byref phcontext as sechandle, _
byref ptoken as secbufferdesc) as long

private declare function completeauthtoken lib "secur32" _
(byref phcontext as sechandle, _
byref ptoken as secbufferdesc) as long

private declare function nt4deletesecuritycontext lib "security" _
alias "deletesecuritycontext" (byref phcontext as sechandle) _
as long

private declare function deletesecuritycontext lib "secur32" _
(byref phcontext as sechandle) _
as long

private declare function nt4freecredentialshandle lib "security" _
alias "freecredentialshandle" (byref phcontext as sechandle) _
as long

private declare function freecredentialshandle lib "secur32" _
(byref phcontext as sechandle) _
as long

private declare function getprocessheap lib "kernel32" () as long

private declare function heapalloc lib "kernel32" _
(byval hheap as long, byval dwflags as long, _
byval dwbytes as long) as long

private declare function heapfree lib "kernel32" (byval hheap as long, _
byval dwflags as long, byval lpmem as long) as long

private declare function getversionexa lib "kernel32" _
(lpversioninformation as osversioninfo) as integer

dim g_nt4 as boolean

private function genclientcontext(byref authseq as auth_seq, _
byref authidentity as sec_winnt_auth_identity, _
byval pin as long, byval cbin as long, _
byval pout as long, byref cbout as long, _
byref fdone as boolean) as boolean

dim ss as long
dim tsexpiry as timestamp
dim sbdout as secbufferdesc
dim sbout as secbuffer
dim sbdin as secbufferdesc
dim sbin as secbuffer
dim fcontextattr as long

genclientcontext = false

if not authseq.finitialized then

if g_nt4 then
ss = nt4acquirecredentialshandle(0&, "ntlm", _
secpkg_cred_outbound, 0&, authidentity, 0&, 0&, _
authseq.hcred, tsexpiry)
else
ss = acquirecredentialshandle(0&, "ntlm", _
secpkg_cred_outbound, 0&, authidentity, 0&, 0&, _
authseq.hcred, tsexpiry)
end if

if ss < 0 then
exit function
end if

authseq.fhavecredhandle = true

end if

' prepare output buffer
sbdout.ulversion = 0
sbdout.cbuffers = 1
sbdout.pbuffers = heapalloc(getprocessheap(), heap_zero_memory, _
len(sbout))

sbout.cbbuffer = cbout
sbout.buffertype = secbuffer_token
sbout.pvbuffer = pout

copymemory byval sbdout.pbuffers, sbout, len(sbout)

' prepare input buffer
if authseq.finitialized then

sbdin.ulversion = 0
sbdin.cbuffers = 1
sbdin.pbuffers = heapalloc(getprocessheap(), heap_zero_memory, _
len(sbin))

sbin.cbbuffer = cbin
sbin.buffertype = secbuffer_token
sbin.pvbuffer = pin

copymemory byval sbdin.pbuffers, sbin, len(sbin)

end if

if authseq.finitialized then

if g_nt4 then
ss = nt4initializesecuritycontext(authseq.hcred, _
authseq.hctxt, 0&, 0, 0, security_native_drep, sbdin, _
0, authseq.hctxt, sbdout, fcontextattr, tsexpiry)
else
ss = initializesecuritycontext(authseq.hcred, _
authseq.hctxt, 0&, 0, 0, security_native_drep, sbdin, _
0, authseq.hctxt, sbdout, fcontextattr, tsexpiry)
end if

else

if g_nt4 then
ss = nt4initializesecuritycontext2(authseq.hcred, 0&, 0&, _
0, 0, security_native_drep, 0&, 0, authseq.hctxt, _
sbdout, fcontextattr, tsexpiry)
else
ss = initializesecuritycontext2(authseq.hcred, 0&, 0&, _
0, 0, security_native_drep, 0&, 0, authseq.hctxt, _
sbdout, fcontextattr, tsexpiry)
end if

end if

if ss < 0 then
goto freeresourcesandexit
end if

authseq.fhavectxthandle = true

' if necessary, complete token
if ss = sec_i_complete_needed _
or ss = sec_i_complete_and_continue then

if g_nt4 then
ss = nt4completeauthtoken(authseq.hctxt, sbdout)
else
ss = completeauthtoken(authseq.hctxt, sbdout)
end if

if ss < 0 then
goto freeresourcesandexit
end if

end if

copymemory sbout, byval sbdout.pbuffers, len(sbout)
cbout = sbout.cbbuffer

if not authseq.finitialized then
authseq.finitialized = true
end if

fdone = not (ss = sec_i_continue_needed _
or ss = sec_i_complete_and_continue)

genclientcontext = true

freeresourcesandexit:

if sbdout.pbuffers <> 0 then
heapfree getprocessheap(), 0, sbdout.pbuffers
end if

if sbdin.pbuffers <> 0 then
heapfree getprocessheap(), 0, sbdin.pbuffers
end if

end function


private function genservercontext(byref authseq as auth_seq, _
byval pin as long, byval cbin as long, _
byval pout as long, byref cbout as long, _
byref fdone as boolean) as boolean

dim ss as long
dim tsexpiry as timestamp
dim sbdout as secbufferdesc
dim sbout as secbuffer
dim sbdin as secbufferdesc
dim sbin as secbuffer
dim fcontextattr as long

genservercontext = false

if not authseq.finitialized then

if g_nt4 then
ss = nt4acquirecredentialshandle2(0&, "ntlm", _
secpkg_cred_inbound, 0&, 0&, 0&, 0&, authseq.hcred, _
tsexpiry)
else
ss = acquirecredentialshandle2(0&, "ntlm", _
secpkg_cred_inbound, 0&, 0&, 0&, 0&, authseq.hcred, _
tsexpiry)
end if

if ss < 0 then
exit function
end if

authseq.fhavecredhandle = true

end if

' prepare output buffer
sbdout.ulversion = 0
sbdout.cbuffers = 1
sbdout.pbuffers = heapalloc(getprocessheap(), heap_zero_memory, _
len(sbout))

sbout.cbbuffer = cbout
sbout.buffertype = secbuffer_token
sbout.pvbuffer = pout

copymemory byval sbdout.pbuffers, sbout, len(sbout)

' prepare input buffer
sbdin.ulversion = 0
sbdin.cbuffers = 1
sbdin.pbuffers = heapalloc(getprocessheap(), heap_zero_memory, _
len(sbin))

sbin.cbbuffer = cbin
sbin.buffertype = secbuffer_token
sbin.pvbuffer = pin

copymemory byval sbdin.pbuffers, sbin, len(sbin)

if authseq.finitialized then

if g_nt4 then
ss = nt4acceptsecuritycontext(authseq.hcred, authseq.hctxt, _
sbdin, 0, security_native_drep, authseq.hctxt, sbdout, _
fcontextattr, tsexpiry)
else
ss = acceptsecuritycontext(authseq.hcred, authseq.hctxt, _
sbdin, 0, security_native_drep, authseq.hctxt, sbdout, _
fcontextattr, tsexpiry)
end if

else

if g_nt4 then
ss = nt4acceptsecuritycontext2(authseq.hcred, 0&, sbdin, 0, _
security_native_drep, authseq.hctxt, sbdout, _
fcontextattr, tsexpiry)
else
ss = acceptsecuritycontext2(authseq.hcred, 0&, sbdin, 0, _
security_native_drep, authseq.hctxt, sbdout, _
fcontextattr, tsexpiry)
end if

end if

if ss < 0 then
goto freeresourcesandexit
end if

authseq.fhavectxthandle = true

' if necessary, complete token
if ss = sec_i_complete_needed _
or ss = sec_i_complete_and_continue then

if g_nt4 then
ss = nt4completeauthtoken(authseq.hctxt, sbdout)
else
ss = completeauthtoken(authseq.hctxt, sbdout)
end if

if ss < 0 then
goto freeresourcesandexit
end if

end if

copymemory sbout, byval sbdout.pbuffers, len(sbout)
cbout = sbout.cbbuffer

if not authseq.finitialized then
authseq.finitialized = true
end if

fdone = not (ss = sec_i_continue_needed _
or ss = sec_i_complete_and_continue)

genservercontext = true

freeresourcesandexit:

if sbdout.pbuffers <> 0 then
heapfree getprocessheap(), 0, sbdout.pbuffers
end if

if sbdin.pbuffers <> 0 then
heapfree getprocessheap(), 0, sbdin.pbuffers
end if

end function


public function sspvalidateuser(user as string, domain as string, _
password as string) as boolean

dim pspi as long
dim spi as secpkginfo
dim cbmaxtoken as long

dim pclientbuf as long
dim pserverbuf as long

dim ai as sec_winnt_auth_identity

dim asclient as auth_seq
dim asserver as auth_seq
dim cbin as long
dim cbout as long
dim fdone as boolean

dim osinfo as osversioninfo

sspvalidateuser = false

' determine if system is windows nt (version 4.0 or earlier)
osinfo.dwosversioninfosize = len(osinfo)
osinfo.szcsdversion = space$(128)
getversionexa osinfo
g_nt4 = (osinfo.dwplatformid = ver_platform_win32_nt and _
osinfo.dwmajorversion <= 4)

' get max token size
if g_nt4 then
nt4querysecuritypackageinfo "ntlm", pspi
else
querysecuritypackageinfo "ntlm", pspi
end if

copymemory spi, byval pspi, len(spi)
cbmaxtoken = spi.cbmaxtoken

if g_nt4 then
nt4freecontextbuffer pspi
else
freecontextbuffer pspi
end if

' allocate buffers for client and server messages
pclientbuf = heapalloc(getprocessheap(), heap_zero_memory, _
cbmaxtoken)
if pclientbuf = 0 then
goto freeresourcesandexit
end if

pserverbuf = heapalloc(getprocessheap(), heap_zero_memory, _
cbmaxtoken)
if pserverbuf = 0 then
goto freeresourcesandexit
end if

' initialize auth identity structure
ai.domain = domain
ai.domainlength = len(domain)
ai.user = user
ai.userlength = len(user)
ai.password = password
ai.passwordlength = len(password)
ai.flags = sec_winnt_auth_identity_ansi

' prepare client message (negotiate) .
cbout = cbmaxtoken
if not genclientcontext(asclient, ai, 0, 0, pclientbuf, cbout, _
fdone) then
goto freeresourcesandexit
end if

' prepare server message (challenge) .
cbin = cbout
cbout = cbmaxtoken
if not genservercontext(asserver, pclientbuf, cbin, pserverbuf, _
cbout, fdone) then
' most likely failure: acceptservercontext fails with
' sec_e_logon_denied in the case of bad szuser or szpassword.
' unexpected result: logon will succeed if you pass in a bad
' szuser and the guest account is enabled in the specified domain.
goto freeresourcesandexit
end if

' prepare client message (authenticate) .
cbin = cbout
cbout = cbmaxtoken
if not genclientcontext(asclient, ai, pserverbuf, cbin, pclientbuf, _
cbout, fdone) then
goto freeresourcesandexit
end if

' prepare server message (authentication) .
cbin = cbout
cbout = cbmaxtoken
if not genservercontext(asserver, pclientbuf, cbin, pserverbuf, _
cbout, fdone) then
goto freeresourcesandexit
end if

sspvalidateuser = true

freeresourcesandexit:

' clean up resources
if asclient.fhavectxthandle then
if g_nt4 then
nt4deletesecuritycontext asclient.hctxt
else
deletesecuritycontext asclient.hctxt
end if
end if

if asclient.fhavecredhandle then
if g_nt4 then
nt4freecredentialshandle asclient.hcred
else
freecredentialshandle asclient.hcred
end if
end if

if asserver.fhavectxthandle then
if g_nt4 then
nt4deletesecuritycontext asserver.hctxt
else
deletesecuritycontext asserver.hctxt
end if
end if

if asserver.fhavecredhandle then
if g_nt4 then
nt4freecredentialshandle asserver.hcred
else
freecredentialshandle asserver.hcred
end if
end if

if pclientbuf <> 0 then
heapfree getprocessheap(), 0, pclientbuf
end if

if pserverbuf <> 0 then
heapfree getprocessheap(), 0, pserverbuf
end if

end function



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表