1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
3 # Copyright (C) Catalyst IT Ltd. 2017
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Tests that exercise auth logging for unsuccessful netlogon attempts.
22 NOTE: netlogon is only done once per session, so this file should only
23 test failed logons. Adding a successful case will potentially break
24 the other tests, depending on the order of execution.
29 from samba import NTSTATUSError
30 from samba.samdb import SamDB
31 import samba.tests.auth_log_base
32 from samba.credentials import Credentials
33 from samba.dcerpc import netlogon
34 from samba.dcerpc.dcerpc import AS_SYSTEM_MAGIC_PATH_TOKEN
35 from samba.auth import system_session
36 from samba.tests import delete_force
37 from samba.dsdb import UF_WORKSTATION_TRUST_ACCOUNT, UF_PASSWD_NOTREQD
38 from samba.dcerpc.misc import SEC_CHAN_WKSTA
39 from samba.dcerpc.netlogon import NETLOGON_NEG_STRONG_KEYS
42 class AuthLogTestsNetLogonBadCreds(samba.tests.auth_log_base.AuthLogTestBase):
45 super(AuthLogTestsNetLogonBadCreds, self).setUp()
46 self.lp = samba.tests.env_loadparm()
47 self.creds = Credentials()
49 self.session = system_session()
51 session_info=self.session,
52 credentials=self.creds,
55 self.domain = os.environ["DOMAIN"]
56 self.netbios_name = "NetLogonBad"
57 self.machinepass = "abcdefghij"
58 self.remoteAddress = AS_SYSTEM_MAGIC_PATH_TOKEN
59 self.base_dn = self.ldb.domain_dn()
60 self.dn = ("cn=%s,cn=users,%s" %
61 (self.netbios_name, self.base_dn))
64 '"' + self.machinepass.encode('utf-8') + '"', 'utf-8'
68 "objectclass": "computer",
69 "sAMAccountName": "%s$" % self.netbios_name,
71 str(UF_WORKSTATION_TRUST_ACCOUNT | UF_PASSWD_NOTREQD),
72 "unicodePwd": utf16pw})
75 super(AuthLogTestsNetLogonBadCreds, self).tearDown()
76 delete_force(self.ldb, self.dn)
78 def _test_netlogon(self, name, pwd, status, checkFunction):
80 def isLastExpectedMessage(msg):
82 msg["type"] == "Authentication" and
83 msg["Authentication"]["serviceDescription"] == "NETLOGON" and
84 msg["Authentication"]["authDescription"] ==
85 "ServerAuthenticate" and
86 msg["Authentication"]["status"] == status)
88 machine_creds = Credentials()
89 machine_creds.guess(self.get_loadparm())
90 machine_creds.set_secure_channel_type(SEC_CHAN_WKSTA)
91 machine_creds.set_password(pwd)
92 machine_creds.set_username(name + "$")
95 netlogon.netlogon("ncalrpc:[schannel]",
98 self.fail("NTSTATUSError not raised")
102 messages = self.waitForMessages(isLastExpectedMessage)
103 checkFunction(messages)
105 def netlogon_check(self, messages):
107 expected_messages = 4
108 self.assertEquals(expected_messages,
110 "Did not receive the expected number of messages")
112 # Check the first message it should be an Authorization
114 self.assertEquals("Authorization", msg["type"])
115 self.assertEquals("DCE/RPC",
116 msg["Authorization"]["serviceDescription"])
117 self.assertEquals("ncalrpc", msg["Authorization"]["authType"])
118 self.assertEquals("NONE", msg["Authorization"]["transportProtection"])
119 self.assertTrue(self.is_guid(msg["Authorization"]["sessionId"]))
121 def test_netlogon_bad_machine_name(self):
122 self._test_netlogon("bad_name",
124 "NT_STATUS_NO_TRUST_SAM_ACCOUNT",
127 def test_netlogon_bad_password(self):
128 self._test_netlogon(self.netbios_name,
130 "NT_STATUS_ACCESS_DENIED",
133 def test_netlogon_password_DES(self):
134 """Logon failure that exercises the "DES" passwordType path.
136 def isLastExpectedMessage(msg):
138 msg["type"] == "Authentication" and
139 msg["Authentication"]["serviceDescription"] == "NETLOGON" and
140 msg["Authentication"]["authDescription"] ==
141 "ServerAuthenticate" and
142 msg["Authentication"]["passwordType"] == "DES")
144 c = netlogon.netlogon("ncalrpc:[schannel]", self.get_loadparm())
145 creds = netlogon.netr_Credential()
146 c.netr_ServerReqChallenge(self.server, self.netbios_name, creds)
148 c.netr_ServerAuthenticate3(self.server,
154 except NTSTATUSError:
156 self.waitForMessages(isLastExpectedMessage)
158 def test_netlogon_password_HMAC_MD5(self):
159 """Logon failure that exercises the "HMAC-MD5" passwordType path.
161 def isLastExpectedMessage(msg):
163 msg["type"] == "Authentication" and
164 msg["Authentication"]["serviceDescription"] == "NETLOGON" and
165 msg["Authentication"]["authDescription"] ==
166 "ServerAuthenticate" and
167 msg["Authentication"]["passwordType"] == "HMAC-MD5")
168 c = netlogon.netlogon("ncalrpc:[schannel]", self.get_loadparm())
169 creds = netlogon.netr_Credential()
170 c.netr_ServerReqChallenge(self.server, self.netbios_name, creds)
172 c.netr_ServerAuthenticate3(self.server,
177 NETLOGON_NEG_STRONG_KEYS)
178 except NTSTATUSError:
180 self.waitForMessages(isLastExpectedMessage)