Utilizzando JCIFS in Jython per sito di accesso utilizzando la protezione NTLM
Domanda
Per un po 'di tempo ho cercato di trovare un modo per Jython al sito di accesso tramite NTLM. Ho conoscenza solo di base di Python e accanto a nessuno in java, così ho potuto usare un certo aiuto (o un esempio) come fare la richiesta di utilizzo NTLM in questa parte di script che ho trovato. Sto usando questo con l'open smerigliatrice applicazione di origine.
Per prima cosa iniziare con l'importazione di JCIFS nello script insieme ad altri usati per smerigliatrice:
from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair
from jcifs.ntlmssp import Type1Message
from jcifs.ntlmssp import Type2Message, Type3Message
from jcifs.util import Base64
Questa parte del codice è stato fornito nell'esempio che ho trovato. Era la cosa si chiude sono riuscito a trovare, che si adatterebbe alle mie esigenze, dal momento che ho solo bisogno di ottenere la risposta completa alla richiesta.
def NTLMAuthentication1(url, request, info, NTLMfield):
token_type1 = info.token_type1()
params = (NVPair("Authorization", "NTLM "+token_type1), )
result = request.GET(url, None, params)
NTLMfield = result.getHeader("WWW-Authenticate")
return NTLMAuthentication2(url, request, info, NTLMfield)
def NTLMAuthentication2(url, request, info, NTLMfield):
if NTLMfield.startswith("Negotiate"):
token_type2 = NTLMfield[len("Negotiate "):]
else:
token_type2 = NTLMfield[5:]
token_type3 = info.token_type3(token_type2)
params = (NVPair("Cookie", "WSS_KeepSessionAuthenticated=80"),
NVPair("Authorization", "NTLM " + token_type3), )
result = request.GET(url, None, params)
return result
# this function validate request and its result to see if the NTLM authentication is required
def NTLMAuthentication(lastResult, request, info):
# get last http request's url
url = lastResult.getEffectiveURI().toString()[len(request.getUrl()):]
# The result is ask for authentication
if lastResult.statusCode != 401 and lastResult.statusCode != 407:
return lastResult
NTLMfield = lastResult.getHeader("WWW-Authenticate")
if NTLMfield == None:
return lastResult
# check it is the first shakehands
if NTLMfield == "Negotiate, NTLM" or NTLMfield == "NTLM":
return NTLMAuthentication1(url, request, info, NTLMfield)
# check it is the second shakehands
elif len(NTLMfield) > 4 and NTLMfield[:4] == "NTLM":
return NTLMAuthentication2(url, request, info, NTLMfield)
else:
return lastResult
class NTLMAuthenticationInfo:
def __init__(self, domain, host, user, passwd):
self.domain = 'domain'
self.host = 'host'
self.user = 'user'
self.passwd = 'password'
def token_type1(self):
msg = Type1Message(Type1Message.getDefaultFlags(), self.domain, self.host)
return Base64.encode(msg.toByteArray())
def token_type3(self, token_type2):
msg2 = Type2Message(Base64.decode(token_type2))
#if jcifs 1.3.7 using msg3 = Type3Message(msg2, self.passwd, self.domain, self.user, self.host)
msg3 = Type3Message(msg2, self.passwd, self.domain, self.user, self.host)
return Base64.encode(msg3.toByteArray())
Nella parte principale della richiesta simile a questa:
result = request101.GET('/')
dove request101 è stata predefinita con l'URL e l'intestazione. Quindi, in sostanza, non ho la minima idea di come implementare la
Ho provato questo
result = request101.GET('/')
print str(NTLMAuthentication(result, request101, NTLMAuthenticationInfo))
così come solo questo
NTLMAuthentication(request101.GET('/'), request101, NTLMAuthenticationInfo)
, ma nessuna di queste ha funzionato. Eventuali suggerimenti su come eseguire questo?
Soluzione
Prova questo
ai = NTLMAuthenticationInfo("domain", "your host", "user", "password")
result = request101.GET('/')
result = NTLMAuthentication(result, request101, ai)