examples_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package examples
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "gopkg.in/jcmturner/rpc.v1/mstypes"
  9. "gopkg.in/jcmturner/rpc.v1/ndr"
  10. )
  11. const (
  12. KerbValidationInfoMS = "01100800cccccccca00400000000000000000200d186660f656ac601ffffffffffffff7fffffffffffffff7f17d439fe784ac6011794a328424bc601175424977a81c60108000800040002002400240008000200120012000c0002000000000010000200000000001400020000000000180002005410000097792c00010200001a0000001c000200200000000000000000000000000000000000000016001800200002000a000c002400020028000200000000000000000010000000000000000000000000000000000000000000000000000000000000000d0000002c0002000000000000000000000000000400000000000000040000006c007a00680075001200000000000000120000004c0069007100690061006e00670028004c006100720072007900290020005a00680075000900000000000000090000006e0074006400730032002e0062006100740000000000000000000000000000000000000000000000000000000000000000000000000000001a00000061c433000700000009c32d00070000005eb4320007000000010200000700000097b92c00070000002bf1320007000000ce30330007000000a72e2e00070000002af132000700000098b92c000700000062c4330007000000940133000700000076c4330007000000aefe2d000700000032d22c00070000001608320007000000425b2e00070000005fb4320007000000ca9c35000700000085442d0007000000c2f0320007000000e9ea310007000000ed8e2e0007000000b6eb310007000000ab2e2e0007000000720e2e00070000000c000000000000000b0000004e0054004400450056002d00440043002d003000350000000600000000000000050000004e0054004400450056000000040000000104000000000005150000005951b81766725d2564633b0b0d0000003000020007000000340002000700002038000200070000203c000200070000204000020007000020440002000700002048000200070000204c000200070000205000020007000020540002000700002058000200070000205c00020007000020600002000700002005000000010500000000000515000000b9301b2eb7414c6c8c3b351501020000050000000105000000000005150000005951b81766725d2564633b0b74542f00050000000105000000000005150000005951b81766725d2564633b0be8383200050000000105000000000005150000005951b81766725d2564633b0bcd383200050000000105000000000005150000005951b81766725d2564633b0b5db43200050000000105000000000005150000005951b81766725d2564633b0b41163500050000000105000000000005150000005951b81766725d2564633b0be8ea3100050000000105000000000005150000005951b81766725d2564633b0bc1193200050000000105000000000005150000005951b81766725d2564633b0b29f13200050000000105000000000005150000005951b81766725d2564633b0b0f5f2e00050000000105000000000005150000005951b81766725d2564633b0b2f5b2e00050000000105000000000005150000005951b81766725d2564633b0bef8f3100050000000105000000000005150000005951b81766725d2564633b0b075f2e0000000000"
  13. KerbValidationInfoGoKRB5 = "01100800cccccccc180200000000000000000200058e4fdd80c6d201ffffffffffffff7fffffffffffffff7fcc27969c39c6d201cce7ffc602c7d201ffffffffffffff7f12001200040002001600160008000200000000000c000200000000001000020000000000140002000000000018000200d80000005104000001020000050000001c000200200000000000000000000000000000000000000008000a002000020008000a00240002002800020000000000000000001002000000000000000000000000000000000000000000000000000000000000020000002c00020000000000000000000000000009000000000000000900000074006500730074007500730065007200310000000b000000000000000b000000540065007300740031002000550073006500720031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050000000102000007000000540400000700000055040000070000005b040000070000005c0400000700000005000000000000000400000041004400440043000500000000000000040000005400450053005400040000000104000000000005150000004c86cebca07160e63fdce8870200000030000200070000203400020007000020050000000105000000000005150000004c86cebca07160e63fdce8875a040000050000000105000000000005150000004c86cebca07160e63fdce8875704000000000000"
  14. KerbValidationInfoTrust = "01100800cccccccc000200000000000000000200c30bcc79e444d301ffffffffffffff7fffffffffffffff7fc764125a0842d301c7247c84d142d301ffffffffffffff7f12001200040002001600160008000200000000000c0002000000000010000200000000001400020000000000180002002e0000005204000001020000030000001c0002002002000000000000000000000000000000000000060008002000020008000a00240002002800020000000000000000001002000000000000000000000000000000000000000000000000000000000000010000002c00020034000200020000003800020009000000000000000900000074006500730074007500730065007200310000000b000000000000000b0000005400650073007400310020005500730065007200310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000056040000070000000102000007000000550400000700000004000000000000000300000055004400430000000500000000000000040000005500530045005200040000000104000000000005150000002057308834e7d1d0a2fb0444010000003000020007000000010000000101000000000012010000000400000001040000000000051500000062dc8db6c8705249b5459e75020000005304000007000020540400000700002000000000"
  15. )
  16. func TestExample_KerbValidationInfo(t *testing.T) {
  17. b, _ := hex.DecodeString(KerbValidationInfoMS)
  18. k := new(KerbValidationInfo)
  19. dec := ndr.NewDecoder(bytes.NewReader(b))
  20. err := dec.Decode(k)
  21. if err != nil {
  22. t.Errorf("%v", err)
  23. }
  24. assert.Equal(t, time.Date(2006, 4, 28, 1, 42, 50, 925640100, time.UTC), k.LogOnTime.Time(), "LogOnTime not as expected")
  25. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551516, time.UTC), k.LogOffTime.Time(), "LogOffTime not as expected")
  26. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551516, time.UTC), k.KickOffTime.Time(), "KickOffTime not as expected")
  27. assert.Equal(t, time.Date(2006, 3, 18, 10, 44, 54, 837147900, time.UTC), k.PasswordLastSet.Time(), "PasswordLastSet not as expected")
  28. assert.Equal(t, time.Date(2006, 3, 19, 10, 44, 54, 837147900, time.UTC), k.PasswordCanChange.Time(), "PasswordCanChange not as expected")
  29. assert.Equal(t, "lzhu", k.EffectiveName.String(), "EffectiveName not as expected")
  30. assert.Equal(t, "Liqiang(Larry) Zhu", k.FullName.String(), "EffectiveName not as expected")
  31. assert.Equal(t, "ntds2.bat", k.LogonScript.String(), "EffectiveName not as expected")
  32. assert.Equal(t, "", k.ProfilePath.String(), "EffectiveName not as expected")
  33. assert.Equal(t, "", k.HomeDirectory.String(), "EffectiveName not as expected")
  34. assert.Equal(t, "", k.HomeDirectoryDrive.String(), "EffectiveName not as expected")
  35. assert.Equal(t, uint16(4180), k.LogonCount, "LogonCount not as expected")
  36. assert.Equal(t, uint16(0), k.BadPasswordCount, "BadPasswordCount not as expected")
  37. assert.Equal(t, uint32(2914711), k.UserID, "UserID not as expected")
  38. assert.Equal(t, uint32(513), k.PrimaryGroupID, "PrimaryGroupID not as expected")
  39. assert.Equal(t, uint32(26), k.GroupCount, "GroupCount not as expected")
  40. gids := []mstypes.GroupMembership{
  41. {RelativeID: 3392609, Attributes: 7},
  42. {RelativeID: 2999049, Attributes: 7},
  43. {RelativeID: 3322974, Attributes: 7},
  44. {RelativeID: 513, Attributes: 7},
  45. {RelativeID: 2931095, Attributes: 7},
  46. {RelativeID: 3338539, Attributes: 7},
  47. {RelativeID: 3354830, Attributes: 7},
  48. {RelativeID: 3026599, Attributes: 7},
  49. {RelativeID: 3338538, Attributes: 7},
  50. {RelativeID: 2931096, Attributes: 7},
  51. {RelativeID: 3392610, Attributes: 7},
  52. {RelativeID: 3342740, Attributes: 7},
  53. {RelativeID: 3392630, Attributes: 7},
  54. {RelativeID: 3014318, Attributes: 7},
  55. {RelativeID: 2937394, Attributes: 7},
  56. {RelativeID: 3278870, Attributes: 7},
  57. {RelativeID: 3038018, Attributes: 7},
  58. {RelativeID: 3322975, Attributes: 7},
  59. {RelativeID: 3513546, Attributes: 7},
  60. {RelativeID: 2966661, Attributes: 7},
  61. {RelativeID: 3338434, Attributes: 7},
  62. {RelativeID: 3271401, Attributes: 7},
  63. {RelativeID: 3051245, Attributes: 7},
  64. {RelativeID: 3271606, Attributes: 7},
  65. {RelativeID: 3026603, Attributes: 7},
  66. {RelativeID: 3018354, Attributes: 7},
  67. }
  68. assert.Equal(t, gids, k.GroupIDs, "GroupIDs not as expected")
  69. assert.Equal(t, uint32(32), k.UserFlags, "UserFlags not as expected")
  70. assert.Equal(t, mstypes.UserSessionKey{CypherBlock: [2]mstypes.CypherBlock{{Data: [8]byte{}}, {Data: [8]byte{}}}}, k.UserSessionKey, "UserSessionKey not as expected")
  71. assert.Equal(t, "NTDEV-DC-05", k.LogonServer.Value, "LogonServer not as expected")
  72. assert.Equal(t, "NTDEV", k.LogonDomainName.Value, "LogonDomainName not as expected")
  73. assert.Equal(t, "S-1-5-21-397955417-626881126-188441444", k.LogonDomainID.String(), "LogonDomainID not as expected")
  74. assert.Equal(t, uint32(16), k.UserAccountControl, "UserAccountControl not as expected")
  75. assert.Equal(t, uint32(0), k.SubAuthStatus, "SubAuthStatus not as expected")
  76. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551616, time.UTC), k.LastSuccessfulILogon.Time(), "LastSuccessfulILogon not as expected")
  77. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551616, time.UTC), k.LastFailedILogon.Time(), "LastSuccessfulILogon not as expected")
  78. assert.Equal(t, uint32(0), k.FailedILogonCount, "FailedILogonCount not as expected")
  79. assert.Equal(t, uint32(13), k.SIDCount, "SIDCount not as expected")
  80. assert.Equal(t, int(k.SIDCount), len(k.ExtraSIDs), "SIDCount and size of ExtraSIDs list are not the same")
  81. var es = []struct {
  82. sid string
  83. attr uint32
  84. }{
  85. {"S-1-5-21-773533881-1816936887-355810188-513", uint32(7)},
  86. {"S-1-5-21-397955417-626881126-188441444-3101812", uint32(536870919)},
  87. {"S-1-5-21-397955417-626881126-188441444-3291368", uint32(536870919)},
  88. {"S-1-5-21-397955417-626881126-188441444-3291341", uint32(536870919)},
  89. {"S-1-5-21-397955417-626881126-188441444-3322973", uint32(536870919)},
  90. {"S-1-5-21-397955417-626881126-188441444-3479105", uint32(536870919)},
  91. {"S-1-5-21-397955417-626881126-188441444-3271400", uint32(536870919)},
  92. {"S-1-5-21-397955417-626881126-188441444-3283393", uint32(536870919)},
  93. {"S-1-5-21-397955417-626881126-188441444-3338537", uint32(536870919)},
  94. {"S-1-5-21-397955417-626881126-188441444-3038991", uint32(536870919)},
  95. {"S-1-5-21-397955417-626881126-188441444-3037999", uint32(536870919)},
  96. {"S-1-5-21-397955417-626881126-188441444-3248111", uint32(536870919)},
  97. }
  98. for i, s := range es {
  99. assert.Equal(t, s.sid, k.ExtraSIDs[i].SID.String(), "ExtraSID SID value not as epxected")
  100. assert.Equal(t, s.attr, k.ExtraSIDs[i].Attributes, "ExtraSID Attributes value not as epxected")
  101. }
  102. assert.Equal(t, uint8(0), k.ResourceGroupDomainSID.SubAuthorityCount, "ResourceGroupDomainSID not as expected")
  103. assert.Equal(t, 0, len(k.ResourceGroupIDs), "ResourceGroupIDs not as expected")
  104. b, _ = hex.DecodeString(KerbValidationInfoGoKRB5)
  105. k2 := new(KerbValidationInfo)
  106. dec = ndr.NewDecoder(bytes.NewReader(b))
  107. err = dec.Decode(k2)
  108. if err != nil {
  109. t.Errorf("%v", err)
  110. }
  111. assert.Equal(t, time.Date(2017, 5, 6, 15, 53, 11, 825766900, time.UTC), k2.LogOnTime.Time(), "LogOnTime not as expected")
  112. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551516, time.UTC), k2.LogOffTime.Time(), "LogOffTime not as expected")
  113. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551516, time.UTC), k2.KickOffTime.Time(), "KickOffTime not as expected")
  114. assert.Equal(t, time.Date(2017, 5, 6, 7, 23, 8, 968750000, time.UTC), k2.PasswordLastSet.Time(), "PasswordLastSet not as expected")
  115. assert.Equal(t, time.Date(2017, 5, 7, 7, 23, 8, 968750000, time.UTC), k2.PasswordCanChange.Time(), "PasswordCanChange not as expected")
  116. assert.Equal(t, "testuser1", k2.EffectiveName.String(), "EffectiveName not as expected")
  117. assert.Equal(t, "Test1 User1", k2.FullName.String(), "EffectiveName not as expected")
  118. assert.Equal(t, "", k2.LogonScript.String(), "EffectiveName not as expected")
  119. assert.Equal(t, "", k2.ProfilePath.String(), "EffectiveName not as expected")
  120. assert.Equal(t, "", k2.HomeDirectory.String(), "EffectiveName not as expected")
  121. assert.Equal(t, "", k2.HomeDirectoryDrive.String(), "EffectiveName not as expected")
  122. assert.Equal(t, uint16(216), k2.LogonCount, "LogonCount not as expected")
  123. assert.Equal(t, uint16(0), k2.BadPasswordCount, "BadPasswordCount not as expected")
  124. assert.Equal(t, uint32(1105), k2.UserID, "UserID not as expected")
  125. assert.Equal(t, uint32(513), k2.PrimaryGroupID, "PrimaryGroupID not as expected")
  126. assert.Equal(t, uint32(5), k2.GroupCount, "GroupCount not as expected")
  127. gids = []mstypes.GroupMembership{
  128. {RelativeID: 513, Attributes: 7},
  129. {RelativeID: 1108, Attributes: 7},
  130. {RelativeID: 1109, Attributes: 7},
  131. {RelativeID: 1115, Attributes: 7},
  132. {RelativeID: 1116, Attributes: 7},
  133. }
  134. assert.Equal(t, gids, k2.GroupIDs, "GroupIDs not as expected")
  135. assert.Equal(t, uint32(32), k2.UserFlags, "UserFlags not as expected")
  136. assert.Equal(t, mstypes.UserSessionKey{CypherBlock: [2]mstypes.CypherBlock{{Data: [8]byte{}}, {Data: [8]byte{}}}}, k2.UserSessionKey, "UserSessionKey not as expected")
  137. assert.Equal(t, "ADDC", k2.LogonServer.String(), "LogonServer not as expected")
  138. assert.Equal(t, "TEST", k2.LogonDomainName.String(), "LogonDomainName not as expected")
  139. assert.Equal(t, "S-1-5-21-3167651404-3865080224-2280184895", k2.LogonDomainID.String(), "LogonDomainID not as expected")
  140. assert.Equal(t, uint32(528), k2.UserAccountControl, "UserAccountControl not as expected")
  141. assert.Equal(t, uint32(0), k2.SubAuthStatus, "SubAuthStatus not as expected")
  142. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551616, time.UTC), k2.LastSuccessfulILogon.Time(), "LastSuccessfulILogon not as expected")
  143. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551616, time.UTC), k2.LastFailedILogon.Time(), "LastSuccessfulILogon not as expected")
  144. assert.Equal(t, uint32(0), k2.FailedILogonCount, "FailedILogonCount not as expected")
  145. assert.Equal(t, uint32(2), k2.SIDCount, "SIDCount not as expected")
  146. assert.Equal(t, int(k2.SIDCount), len(k2.ExtraSIDs), "SIDCount and size of ExtraSIDs list are not the same")
  147. var es2 = []struct {
  148. sid string
  149. attr uint32
  150. }{
  151. {"S-1-5-21-3167651404-3865080224-2280184895-1114", uint32(536870919)},
  152. {"S-1-5-21-3167651404-3865080224-2280184895-1111", uint32(536870919)},
  153. }
  154. for i, s := range es2 {
  155. assert.Equal(t, s.sid, k2.ExtraSIDs[i].SID.String(), "ExtraSID SID value not as expected")
  156. assert.Equal(t, s.attr, k2.ExtraSIDs[i].Attributes, "ExtraSID Attributes value not as expected")
  157. }
  158. assert.Equal(t, uint8(0), k2.ResourceGroupDomainSID.SubAuthorityCount, "ResourceGroupDomainSID not as expected")
  159. assert.Equal(t, 0, len(k2.ResourceGroupIDs), "ResourceGroupIDs not as expected")
  160. b, _ = hex.DecodeString(KerbValidationInfoTrust)
  161. k = new(KerbValidationInfo)
  162. dec = ndr.NewDecoder(bytes.NewReader(b))
  163. err = dec.Decode(k)
  164. if err != nil {
  165. t.Errorf("%v", err)
  166. }
  167. assert.Equal(t, time.Date(2017, 10, 14, 12, 03, 41, 52409900, time.UTC), k.LogOnTime.Time(), "LogOnTime not as expected")
  168. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551516, time.UTC), k.LogOffTime.Time(), "LogOffTime not as expected")
  169. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551516, time.UTC), k.KickOffTime.Time(), "KickOffTime not as expected")
  170. assert.Equal(t, time.Date(2017, 10, 10, 20, 42, 56, 220282300, time.UTC), k.PasswordLastSet.Time(), "PasswordLastSet not as expected")
  171. assert.Equal(t, time.Date(2017, 10, 11, 20, 42, 56, 220282300, time.UTC), k.PasswordCanChange.Time(), "PasswordCanChange not as expected")
  172. assert.Equal(t, "testuser1", k.EffectiveName.String(), "EffectiveName not as expected")
  173. assert.Equal(t, "Test1 User1", k.FullName.String(), "EffectiveName not as expected")
  174. assert.Equal(t, "", k.LogonScript.String(), "EffectiveName not as expected")
  175. assert.Equal(t, "", k.ProfilePath.String(), "EffectiveName not as expected")
  176. assert.Equal(t, "", k.HomeDirectory.String(), "EffectiveName not as expected")
  177. assert.Equal(t, "", k.HomeDirectoryDrive.String(), "EffectiveName not as expected")
  178. assert.Equal(t, uint16(46), k.LogonCount, "LogonCount not as expected")
  179. assert.Equal(t, uint16(0), k.BadPasswordCount, "BadPasswordCount not as expected")
  180. assert.Equal(t, uint32(1106), k.UserID, "UserID not as expected")
  181. assert.Equal(t, uint32(513), k.PrimaryGroupID, "PrimaryGroupID not as expected")
  182. assert.Equal(t, uint32(3), k.GroupCount, "GroupCount not as expected")
  183. gids = []mstypes.GroupMembership{
  184. {RelativeID: 1110, Attributes: 7},
  185. {RelativeID: 513, Attributes: 7},
  186. {RelativeID: 1109, Attributes: 7},
  187. }
  188. assert.Equal(t, gids, k.GroupIDs, "GroupIDs not as expected")
  189. assert.Equal(t, uint32(544), k.UserFlags, "UserFlags not as expected")
  190. assert.Equal(t, mstypes.UserSessionKey{CypherBlock: [2]mstypes.CypherBlock{{Data: [8]byte{}}, {Data: [8]byte{}}}}, k.UserSessionKey, "UserSessionKey not as expected")
  191. assert.Equal(t, "UDC", k.LogonServer.Value, "LogonServer not as expected")
  192. assert.Equal(t, "USER", k.LogonDomainName.Value, "LogonDomainName not as expected")
  193. assert.Equal(t, "S-1-5-21-2284869408-3503417140-1141177250", k.LogonDomainID.String(), "LogonDomainID not as expected")
  194. assert.Equal(t, uint32(528), k.UserAccountControl, "UserAccountControl not as expected")
  195. assert.Equal(t, uint32(0), k.SubAuthStatus, "SubAuthStatus not as expected")
  196. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551616, time.UTC), k.LastSuccessfulILogon.Time(), "LastSuccessfulILogon not as expected")
  197. assert.Equal(t, time.Date(2185, 7, 21, 23, 34, 33, 709551616, time.UTC), k.LastFailedILogon.Time(), "LastSuccessfulILogon not as expected")
  198. assert.Equal(t, uint32(0), k.FailedILogonCount, "FailedILogonCount not as expected")
  199. assert.Equal(t, uint32(1), k.SIDCount, "SIDCount not as expected")
  200. assert.Equal(t, int(k.SIDCount), len(k.ExtraSIDs), "SIDCount and size of ExtraSIDs list are not the same")
  201. es = []struct {
  202. sid string
  203. attr uint32
  204. }{
  205. {"S-1-18-1", uint32(7)},
  206. }
  207. for i, s := range es {
  208. assert.Equal(t, s.sid, k.ExtraSIDs[i].SID.String(), "ExtraSID SID value not as epxected")
  209. assert.Equal(t, s.attr, k.ExtraSIDs[i].Attributes, "ExtraSID Attributes value not as epxected")
  210. }
  211. assert.Equal(t, uint8(4), k.ResourceGroupDomainSID.SubAuthorityCount, "ResourceGroupDomainSID not as expected")
  212. assert.Equal(t, "S-1-5-21-3062750306-1230139592-1973306805", k.ResourceGroupDomainSID.String(), "ResourceGroupDomainSID value not as expected")
  213. assert.Equal(t, 2, len(k.ResourceGroupIDs), "ResourceGroupIDs not as expected")
  214. rgids := []mstypes.GroupMembership{
  215. {RelativeID: 1107, Attributes: 536870919},
  216. {RelativeID: 1108, Attributes: 536870919},
  217. }
  218. assert.Equal(t, rgids, k.ResourceGroupIDs, "ResourceGroupIDs not as expected")
  219. //groupSids := []string{"S-1-5-21-2284869408-3503417140-1141177250-1110",
  220. // "S-1-5-21-2284869408-3503417140-1141177250-513",
  221. // "S-1-5-21-2284869408-3503417140-1141177250-1109",
  222. // "S-1-18-1",
  223. // "S-1-5-21-3062750306-1230139592-1973306805-1107",
  224. // "S-1-5-21-3062750306-1230139592-1973306805-1108"}
  225. //assert.Equal(t, groupSids, k.GetGroupMembershipSIDs(), "GroupMembershipSIDs not as expected")
  226. }