dcerpc_scmr.go.tmp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package smb
  2. import (
  3. "go-impacket/pkg/smb/smb2"
  4. )
  5. // 此文件提供访问windows服务管理封装
  6. // DCE/RPC 扩展头
  7. // 调用win ms service control api
  8. type PDUExtHeaderStruct struct {
  9. Version uint8
  10. VersionMinor uint8
  11. PacketType uint8
  12. PacketFlags uint8
  13. DataRepresentation uint32 //4字节,小端排序,0x10
  14. FragLength uint16 //2字节,整个结构的长度
  15. AuthLength uint16
  16. CallId uint32
  17. AllocHint uint32 `smb:"len:Buffer"` //Buffer的长度
  18. ContextId uint16
  19. OpNum uint16
  20. Buffer interface{}
  21. }
  22. // ms service control
  23. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/dc84adb3-d51d-48eb-820d-ba1c6ca5faf2
  24. type OpenSCManagerWStruct struct {
  25. MachineName machineName
  26. Database database
  27. AccessMask uint32
  28. }
  29. type machineName struct {
  30. ReferentId uint32 `smb:"offset:MachineName"`
  31. MaxCount uint32
  32. Offset uint32
  33. ActualCount uint32 //机器名的长度
  34. MachineName []byte //任意长度,unicode编码
  35. Reserved uint16
  36. }
  37. type database struct {
  38. ReferentId uint32 `smb:"offset:Database"`
  39. MaxCount uint32
  40. Offset uint32
  41. ActualCount uint32 //机器名的长度
  42. Database []byte //任意长度,unicode编码
  43. Reserved uint16
  44. }
  45. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/0d7a7011-9f41-470d-ad52-8535b47ac282
  46. // 安全描述符
  47. const (
  48. SERVICE_ALL_ACCESS = 0x000F01FF
  49. SC_MANAGER_CREATE_SERVICE = 0x00000002
  50. SC_MANAGER_CONNECT = 0x00000001
  51. )
  52. // // OpenSCManagerW响应结构
  53. // type OpenSCManagerWResponse struct {
  54. // smb2.ReadResponseStruct
  55. // Version uint8
  56. // VersionMinor uint8
  57. // PacketType uint8
  58. // PacketFlags uint8
  59. // DataRepresentation uint32
  60. // FragLength uint16
  61. // AuthLength uint16
  62. // CallId uint32
  63. // AllocHint uint32
  64. // ContextId uint16
  65. // CancelCount uint8
  66. // Reserved uint8
  67. // ContextHandle []byte `smb:"fixed:20"`
  68. // ReturnCode uint32
  69. // }
  70. // opnum
  71. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/0d7a7011-9f41-470d-ad52-8535b47ac282
  72. const (
  73. RCloseServiceHandle = 0
  74. RControlService = 1
  75. RDeleteService = 2
  76. RLockServiceDatabase = 3
  77. RQueryServiceObjectSecurity = 4
  78. RSetServiceObjectSecurity = 5
  79. RQueryServiceStatus = 6
  80. RSetServiceStatus = 7
  81. RUnlockServiceDatabase = 8
  82. RNotifyBootConfigStatus = 9
  83. RChangeServiceConfigW = 11
  84. RCreateServiceW = 12
  85. REnumDependentServicesW = 13
  86. REnumServicesStatusW = 14
  87. ROpenSCManagerW = 15
  88. ROpenServiceW = 16
  89. RQueryServiceConfigW = 17
  90. RQueryServiceLockStatusW = 18
  91. RStartServiceW = 19
  92. RGetServiceDisplayNameW = 20
  93. RGetServiceKeyNameW = 21
  94. RChangeServiceConfigA = 23
  95. RCreateServiceA = 24
  96. REnumDependentServicesA = 25
  97. REnumServicesStatusA = 26
  98. ROpenSCManagerA = 27
  99. ROpenServiceA = 28
  100. RQueryServiceConfigA = 29
  101. RQueryServiceLockStatusA = 30
  102. RStartServiceA = 31
  103. RGetServiceDisplayNameA = 32
  104. RGetServiceKeyNameA = 33
  105. REnumServiceGroupW = 35
  106. RChangeServiceConfig2A = 36
  107. RChangeServiceConfig2W = 37
  108. RQueryServiceConfig2A = 38
  109. RQueryServiceConfig2W = 39
  110. RQueryServiceStatusEx = 40
  111. REnumServicesStatusExA = 41
  112. REnumServicesStatusExW = 42
  113. RCreateServiceWOW64A = 44
  114. RCreateServiceWOW64W = 45
  115. RNotifyServiceStatusChange = 47
  116. RGetNotifyResults = 48
  117. RCloseNotifyHandle = 49
  118. RControlServiceExA = 50
  119. RControlServiceExW = 51
  120. RQueryServiceConfigEx = 56
  121. RCreateWowService = 60
  122. ROpenSCManager2 = 64
  123. )
  124. // func NewOpenSCManagerWResponse() OpenSCManagerWResponse {
  125. // return OpenSCManagerWResponse{
  126. // ContextHandle: make([]byte, 20),
  127. // }
  128. // }
  129. // 打开服务
  130. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/6d0a4225-451b-4132-894d-7cef7aecfd2d
  131. type ROpenServiceWRequestStruct struct {
  132. ContextHandle []byte `smb:"fixed:20"` //OpenSCManagerW 句柄
  133. ServiceName serviceName
  134. //Reserved uint8
  135. AccessMask uint32
  136. }
  137. type ROpenServiceWResponseStruct struct {
  138. smb2.ReadResponseStruct
  139. Version uint8
  140. VersionMinor uint8
  141. PacketType uint8
  142. PacketFlags uint8
  143. DataRepresentation uint32
  144. FragLength uint16
  145. AuthLength uint16
  146. CallId uint32
  147. AllocHint uint32
  148. ContextId uint16
  149. CancelCount uint8
  150. Reserved uint8
  151. ContextHandle []byte `smb:"fixed:20"`
  152. ReturnCode uint32
  153. }
  154. // func NewROpenServiceWResponse() ROpenServiceWResponseStruct {
  155. // return ROpenServiceWResponseStruct{
  156. // ContextHandle: make([]byte, 20),
  157. // }
  158. // }
  159. // 创建服务
  160. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/6a8ca926-9477-4dd4-b766-692fab07227e
  161. type RCreateServiceWRequestStruct struct {
  162. ContextHandle []byte `smb:"fixed:20"` //OpenSCManagerW 句柄
  163. ServiceName serviceName
  164. DisplayName displayName
  165. AccessMask uint32
  166. ServiceType uint32
  167. ServiceStartType uint32
  168. ServiceErrorControl uint32
  169. BinaryPathName binaryPathName
  170. NULLPointer uint32
  171. TagId uint32
  172. NULLPointer2 uint32
  173. DependSize uint32
  174. NULLPointer3 uint32
  175. NULLPointer4 uint32
  176. PasswordSize uint32
  177. }
  178. type serviceName struct {
  179. MaxCount uint32
  180. Offset uint32
  181. ActualCount uint32
  182. ServiceName []byte
  183. Reserved uint16
  184. }
  185. type displayName struct {
  186. ReferentId uint32 `smb:"offset:DisplayName"`
  187. MaxCount uint32
  188. Offset uint32
  189. ActualCount uint32
  190. DisplayName []byte
  191. Reserved uint16
  192. }
  193. type binaryPathName struct {
  194. MaxCount uint32
  195. Offset uint32
  196. ActualCount uint32
  197. BinaryPathName []byte `smb:"fixed:26"` // 长度不能超过26字节
  198. }
  199. // RCreateServiceW响应结构
  200. type RCreateServiceWResponseStruct struct {
  201. smb2.ReadResponseStruct
  202. Version uint8
  203. VersionMinor uint8
  204. PacketType uint8
  205. PacketFlags uint8
  206. DataRepresentation uint32
  207. FragLength uint16
  208. AuthLength uint16
  209. CallId uint32
  210. AllocHint uint32
  211. ContextId uint16
  212. CancelCount uint8
  213. Reserved uint8
  214. TagId uint32
  215. ContextHandle []byte `smb:"fixed:20"`
  216. ReturnCode uint32
  217. }
  218. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/6a8ca926-9477-4dd4-b766-692fab07227e
  219. // dwServiceType 类型
  220. const (
  221. SERVICE_KERNEL_DRIVER = 0x00000001
  222. SERVICE_FILE_SYSTEM_DRIVER = 0x00000002
  223. SERVICE_WIN32_OWN_PROCESS = 0x00000010
  224. SERVICE_WIN32_SHARE_PROCESS = 0x00000020
  225. SERVICE_INTERACTIVE_PROCESS = 0x00000100
  226. )
  227. // dwStartType类型
  228. const (
  229. SERVICE_BOOT_START = 0x00000000
  230. SERVICE_SYSTEM_START = 0x00000001
  231. SERVICE_AUTO_START = 0x00000002
  232. SERVICE_DEMAND_START = 0x00000003
  233. SERVICE_DISABLED = 0x00000004
  234. )
  235. // dwErrorControl类型
  236. const (
  237. SERVICE_ERROR_IGNORE = 0x00000000
  238. SERVICE_ERROR_NORMAL = 0x00000001
  239. SERVICE_ERROR_SEVERE = 0x00000002
  240. SERVICE_ERROR_CRITICAL = 0x00000003
  241. )
  242. // 启动服务
  243. // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/d9be95a2-cf01-4bdc-b30f-6fe4b37ada16
  244. type RStartServiceWRequestStruct struct {
  245. ContextHandle []byte `smb:"fixed:20"` //20字节,创建服务返回的句柄
  246. Argc uint32 //argv字符串数量
  247. Argv []byte `smb:"fixed:4"` //4字节,unicode对象指针
  248. }
  249. type RStartServiceWResponseStruct struct {
  250. smb2.ReadResponseStruct
  251. Version uint8
  252. VersionMinor uint8
  253. PacketType uint8
  254. PacketFlags uint8
  255. DataRepresentation uint32
  256. FragLength uint16
  257. AuthLength uint16
  258. CallId uint32
  259. AllocHint uint32
  260. ContextId uint16
  261. CancelCount uint8
  262. Reserved uint8
  263. StubData uint32
  264. }
  265. // 启动服务响应封装
  266. func NewRStartServiceWResponse() RStartServiceWResponseStruct {
  267. return RStartServiceWResponseStruct{}
  268. }
  269. // 关闭服务句柄
  270. type RCloseServiceHandleRequestStruct struct {
  271. ContextHandle []byte `smb:"fixed:20"`
  272. }
  273. type RCloseServiceHandleResponseStruct struct {
  274. smb2.ReadResponseStruct
  275. Version uint8
  276. VersionMinor uint8
  277. PacketType uint8
  278. PacketFlags uint8
  279. DataRepresentation uint32
  280. FragLength uint16
  281. AuthLength uint16
  282. CallId uint32
  283. AllocHint uint32
  284. ContextId uint16
  285. CancelCount uint8
  286. Reserved uint8
  287. ContextHandle []byte `smb:"fixed:20"`
  288. ReturnCode uint32
  289. }
  290. func NewRCloseServiceHandleResponse() RCloseServiceHandleResponseStruct {
  291. return RCloseServiceHandleResponseStruct{
  292. ContextHandle: make([]byte, 20),
  293. }
  294. }