3 # create a domain trust
6 from optparse
import OptionParser
8 sys
.path
.insert(0, "bin/python")
11 import samba
.getopt
as options
12 from samba
.dcerpc
import lsa
, security
, drsblobs
13 from samba
.ndr
import ndr_pack
16 def arcfour_encrypt(key
, data
):
17 from Crypto
.Cipher
import ARC4
19 return c
.encrypt(data
)
21 def string_to_array(string
):
22 blob
= [0] * len(string
)
24 for i
in range(len(string
)):
25 blob
[i
] = ord(string
[i
])
29 ########### main code ###########
30 if __name__
== "__main__":
31 parser
= OptionParser("createtrust [options] server")
32 sambaopts
= options
.SambaOptions(parser
)
33 credopts
= options
.CredentialsOptionsDouble(parser
)
34 parser
.add_option_group(credopts
)
36 (opts
, args
) = parser
.parse_args()
38 lp
= sambaopts
.get_loadparm()
39 creds
= credopts
.get_credentials(lp
)
42 parser
.error("You must supply a server")
44 if not creds
.authentication_requested():
45 parser
.error("You must supply credentials")
49 binding_str
= "ncacn_np:%s[print]" % server
51 lsaconn
= lsa
.lsarpc(binding_str
, lp
, creds
)
53 objectAttr
= lsa
.ObjectAttribute()
54 objectAttr
.sec_qos
= lsa
.QosInfo()
56 pol_handle
= lsaconn
.OpenPolicy2(''.decode('utf-8'),
57 objectAttr
, security
.SEC_FLAG_MAXIMUM_ALLOWED
)
60 name
.string
= "sub2.win2k3.obed.home.abartlet.net"
62 info
= lsaconn
.QueryTrustedDomainInfoByName(pol_handle
, name
, lsa
.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO
)
64 lsaconn
.DeleteTrustedDomain(pol_handle
, info
.info_ex
.sid
)
68 info
= lsa
.TrustDomainInfoInfoEx()
69 info
.domain_name
.string
= "sub2.win2k3.obed.home.abartlet.net"
70 info
.netbios_name
.string
= "sub2"
71 info
.sid
= security
.dom_sid("S-1-5-21-538090388-3760119675-95745416")
72 info
.trust_direction
= lsa
.LSA_TRUST_DIRECTION_INBOUND | lsa
.LSA_TRUST_DIRECTION_OUTBOUND
73 info
.trust_type
= lsa
.LSA_TRUST_TYPE_UPLEVEL
74 info
.trust_attributes
= lsa
.LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
76 password_blob
= string_to_array("password".encode('utf-16-le'))
78 clear_value
= drsblobs
.AuthInfoClear()
79 clear_value
.size
= len(password_blob
)
80 clear_value
.password
= password_blob
82 clear_authentication_information
= drsblobs
.AuthenticationInformation()
83 clear_authentication_information
.LastUpdateTime
= 0
84 clear_authentication_information
.AuthType
= lsa
.TRUST_AUTH_TYPE_CLEAR
85 clear_authentication_information
.AuthInfo
= clear_value
87 version_value
= drsblobs
.AuthInfoVersion()
88 version_value
.version
= 1
90 version
= drsblobs
.AuthenticationInformation()
91 version
.LastUpdateTime
= 0
92 version
.AuthType
= lsa
.TRUST_AUTH_TYPE_VERSION
93 version
.AuthInfo
= version_value
95 authentication_information_array
= drsblobs
.AuthenticationInformationArray()
96 authentication_information_array
.count
= 2
97 authentication_information_array
.array
= [clear_authentication_information
, version
]
99 outgoing
= drsblobs
.trustAuthInOutBlob()
101 outgoing
.current
= authentication_information_array
103 trustpass
= drsblobs
.trustDomainPasswords()
104 confounder
= [3] * 512
107 confounder
[i
] = random
.randint(0, 255)
109 trustpass
.confounder
= confounder
111 # print "confounder: ", trustpass.confounder
113 trustpass
.outgoing
= outgoing
114 trustpass
.incoming
= outgoing
116 trustpass_blob
= ndr_pack(trustpass
)
118 # print "trustpass_blob: ", list(trustpass_blob)
120 encrypted_trustpass
= arcfour_encrypt(lsaconn
.session_key
, trustpass_blob
)
122 # print "encrypted_trustpass: ", list(encrypted_trustpass)
124 auth_blob
= lsa
.DATA_BUF2()
125 auth_blob
.size
= len(encrypted_trustpass
)
126 auth_blob
.data
= string_to_array(encrypted_trustpass
)
128 auth_info
= lsa
.TrustDomainInfoAuthInfoInternal()
129 auth_info
.auth_blob
= auth_blob
132 # print "auth_info.auth_blob.data: ", auth_info.auth_blob.data
134 trustdom_handle
= lsaconn
.CreateTrustedDomainEx2(pol_handle
,
137 security
.SEC_STD_DELETE
)