smb_packets.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //---------------------------------------------------------------------------
  2. // __________________ _________ _____ _____ .__ ._.
  3. // \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. // | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. // | | \| ` \/ / Y \ / | | \ ___/ \|
  6. // li|______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. // \/ \/ \/ \/ )/ \/ \/ \/
  8. //
  9. // This file is part of libdsm. Copyright © 2014 VideoLabs SAS
  10. //
  11. // Author: Julien 'Lta' BALLET <contact@lta.io>
  12. //
  13. // This program is free software. It comes without any warranty, to the extent
  14. // permitted by applicable law. You can redistribute it and/or modify it under
  15. // the terms of the Do What The Fuck You Want To Public License, Version 2, as
  16. // published by Sam Hocevar. See the COPYING file for more details.
  17. //----------------------------------------------------------------------------
  18. #ifndef __BSDM_SMB_PACKETS_H_
  19. #define __BSDM_SMB_PACKETS_H_
  20. #include <stdint.h>
  21. #define SMB_ANDX_MEMBERS \
  22. uint8_t andx; /* 0xff when no other command (do this :)*/ \
  23. uint8_t andx_reserved; /* 0x00 */ \
  24. uint16_t andx_offset; /* 0x00 when no other command */
  25. ////////////////////////////////////////////////////////////////////////////////
  26. // Main structures for holding packet data and building packets
  27. typedef struct
  28. {
  29. uint8_t magic[4]; // { 0xff, 0x53, 0x4d, 0x42 } "\xffSMB"
  30. uint8_t command; // The actual SMB command
  31. uint32_t status; // 'NT Status'
  32. uint8_t flags; // Packet flags
  33. uint16_t flags2; // More flags ? (lol)
  34. uint16_t pid_high; // Unused ?
  35. uint64_t signature; // Unused ?
  36. uint16_t reserved; // More usuned bit (we have so much BW :)
  37. uint16_t tid; // A kind of fd for share. (tree_connect)
  38. uint16_t pid; // Process ID.
  39. uint16_t uid; // User ID.
  40. uint16_t mux_id; // Multiplex ID. Increment it sometimes.
  41. } __attribute__((packed)) smb_header;
  42. typedef struct
  43. {
  44. smb_header header; // A packet header full of gorgeous goodness.
  45. uint8_t payload[]; // Ze yummy data inside. Eat 5 fruits/day !
  46. } __attribute__((packed)) smb_packet;
  47. ////////////////////////////////////////////////////////////////////////////////
  48. // Individual SMB command payload description
  49. //-> Negotiate Protocol
  50. typedef struct
  51. {
  52. uint8_t wct; // zero
  53. uint16_t bct;
  54. char dialects[];
  55. } __attribute__((packed)) smb_nego_req;
  56. #define SMB_NEGO_RESP_COMMON \
  57. uint8_t wct; /* +-17 :) */ \
  58. uint16_t dialect_index; \
  59. uint8_t security_mode; /* Share/User. Plaintext/Challenge */ \
  60. uint32_t diplodocus; \
  61. uint32_t max_bufsize; /* Max buffer size requested by server. */ \
  62. uint32_t max_rawbuffer; /* Max raw buffer size requested by serv. */ \
  63. uint32_t session_key; /* 'MUST' be returned to server */ \
  64. uint32_t caps; \
  65. uint64_t ts; /* I don't give a fuck (or do i?) */ \
  66. uint16_t tz; /* Even less fuck given */ \
  67. uint8_t key_length; /* Size of challenge key // GSS blob */ \
  68. uint16_t bct;
  69. //<- Negotiate Protocol
  70. typedef struct
  71. {
  72. SMB_NEGO_RESP_COMMON
  73. uint64_t challenge; // Normally 8 bytes, if not then wtf monkey
  74. uint8_t payload[]; // The rest isn't really meaningfull for us
  75. } __attribute__((packed)) smb_nego_resp;
  76. //<- Negotiate Protocol
  77. typedef struct
  78. {
  79. SMB_NEGO_RESP_COMMON
  80. uint8_t srv_guid[16];
  81. uint8_t gssapi[];
  82. } __attribute__((packed)) smb_nego_xsec_resp;
  83. #define SMB_SESSION_REQ_COMMON \
  84. uint8_t wct; /* +-13 :) */ \
  85. SMB_ANDX_MEMBERS \
  86. uint16_t max_buffer; /* Maximum size we can receive */ \
  87. uint16_t mpx_count; /* maximum multiplexed session */ \
  88. uint16_t vc_count; /* Virtual ciruits -> 1! */ \
  89. uint32_t session_key; /* 0x00000000 */
  90. //-> Session Setup
  91. typedef struct
  92. {
  93. SMB_SESSION_REQ_COMMON
  94. uint16_t oem_pass_len; // Length of LM2 response
  95. uint16_t uni_pass_len; // Length of NTLM2 response
  96. uint32_t reserved2; // 0x00000000
  97. uint32_t caps; // Capabilities
  98. uint16_t payload_size;
  99. uint8_t payload[];
  100. } __attribute__((packed)) smb_session_req;
  101. //-> Session Setup
  102. typedef struct
  103. {
  104. SMB_SESSION_REQ_COMMON
  105. uint16_t xsec_blob_size; // Length of GSSAPI/SPNEGO blob
  106. uint32_t reserved2; // 0x00000000
  107. uint32_t caps; // Capabilities
  108. uint16_t payload_size;
  109. uint8_t payload[];
  110. } __attribute__((packed)) smb_session_xsec_req;
  111. //<- Session Setup
  112. typedef struct
  113. {
  114. uint8_t wct;
  115. SMB_ANDX_MEMBERS
  116. uint16_t action;
  117. uint16_t bct;
  118. uint8_t bullshit[];
  119. } __attribute__((packed)) smb_session_resp;
  120. typedef struct
  121. {
  122. uint8_t wct;
  123. SMB_ANDX_MEMBERS
  124. uint16_t action;
  125. uint16_t xsec_blob_size;
  126. uint16_t payload_size;
  127. uint8_t payload[];
  128. } __attribute__((packed)) smb_session_xsec_resp;
  129. //-> Tree Connect
  130. typedef struct
  131. {
  132. uint8_t wct; // 4
  133. SMB_ANDX_MEMBERS
  134. uint16_t flags;
  135. uint16_t passwd_len; // 1 if not used. Used in Share Level Auth
  136. uint16_t bct;
  137. uint8_t payload[]; // Password | Path | Service
  138. } __attribute__((packed)) smb_tree_connect_req;
  139. //<- Tree Connect
  140. typedef struct
  141. {
  142. uint8_t wct; // 7
  143. SMB_ANDX_MEMBERS
  144. uint16_t opt_support;
  145. uint32_t max_rights;
  146. uint32_t guest_rights;
  147. uint16_t bct;
  148. uint8_t payload[];
  149. } __attribute__((packed)) smb_tree_connect_resp;
  150. //-> Create File
  151. typedef struct
  152. {
  153. uint8_t wct; // 24
  154. SMB_ANDX_MEMBERS
  155. uint8_t reserved2;
  156. uint16_t path_length;
  157. uint32_t flags;
  158. uint32_t root_fid;
  159. uint32_t access_mask;
  160. uint64_t alloc_size;
  161. uint32_t file_attr;
  162. uint32_t share_access;
  163. uint32_t disposition;
  164. uint32_t create_opts;
  165. uint32_t impersonation;
  166. uint8_t security_flags;
  167. uint16_t bct;
  168. uint8_t path[]; // UTF16 Path, starting with '\'
  169. } __attribute__((packed)) smb_create_req;
  170. //<- Create File
  171. typedef struct
  172. {
  173. uint8_t wct; // 34
  174. SMB_ANDX_MEMBERS
  175. uint8_t oplock_level;
  176. uint16_t fid;
  177. uint32_t action;
  178. uint64_t created; // File creation time
  179. uint64_t accessed; // File last access time
  180. uint64_t written; // File last write time
  181. uint64_t changed; // File last modification time
  182. uint32_t attr;
  183. uint64_t alloc_size;
  184. uint64_t size;
  185. uint16_t filetype;
  186. uint16_t ipc_state;
  187. uint8_t is_dir;
  188. uint16_t bct; // 0
  189. } __attribute__((packed)) smb_create_resp;
  190. //-> Close File
  191. typedef struct
  192. {
  193. uint8_t wct; // 3
  194. uint16_t fid;
  195. uint32_t last_write; // Not defined == 0xffffffff
  196. uint16_t bct; // 0
  197. } __attribute__((packed)) smb_close_req;
  198. //-> Read File
  199. typedef struct
  200. {
  201. uint8_t wct; // 12
  202. SMB_ANDX_MEMBERS
  203. uint16_t fid;
  204. uint32_t offset;
  205. uint16_t max_count;
  206. uint16_t min_count;
  207. uint32_t max_count_high; // Continuation of max_count field
  208. uint16_t remaining;
  209. uint32_t offset_high; // Continuation of offset field'
  210. uint16_t bct; // 0
  211. } __attribute__((packed)) smb_read_req;
  212. //<- Read File
  213. typedef struct
  214. {
  215. uint8_t wct; // 12
  216. SMB_ANDX_MEMBERS
  217. uint16_t remaining;
  218. uint16_t compact_mode;
  219. uint16_t reserved;
  220. uint16_t data_len;
  221. uint16_t data_offset;
  222. uint32_t data_len_high;
  223. uint32_t reserved2;
  224. uint16_t reserved3;
  225. uint16_t bct;
  226. uint8_t padding;
  227. uint8_t file[];
  228. } __attribute__((packed)) smb_read_resp;
  229. //-> Trans
  230. typedef struct
  231. {
  232. uint8_t wct; // 16
  233. uint16_t total_param_count;
  234. uint16_t total_data_count;
  235. uint16_t max_param_count;
  236. uint16_t max_data_count;
  237. uint8_t max_setup_count;
  238. uint8_t reserved;
  239. uint16_t flags;
  240. uint32_t timeout;
  241. uint16_t reserved2;
  242. uint16_t param_count;
  243. uint16_t param_offset;
  244. uint16_t data_count;
  245. uint16_t data_offset;
  246. uint8_t setup_count;
  247. uint8_t reserved3;
  248. uint16_t pipe_function;
  249. uint16_t fid;
  250. uint16_t bct;
  251. uint8_t payload[];
  252. } __attribute__((packed)) smb_trans_req;
  253. //-> Trans2
  254. typedef struct
  255. {
  256. uint8_t wct; // 15
  257. uint16_t total_param_count;
  258. uint16_t total_data_count;
  259. uint16_t max_param_count;
  260. uint16_t max_data_count;
  261. uint8_t max_setup_count;
  262. uint8_t reserved;
  263. uint16_t flags;
  264. uint32_t timeout;
  265. uint16_t reserve2;
  266. uint16_t param_count;
  267. uint16_t param_offset;
  268. uint16_t data_count;
  269. uint16_t data_offset;
  270. uint8_t setup_count;
  271. uint8_t reserved3;
  272. uint16_t cmd;
  273. uint16_t bct;
  274. uint8_t padding[3];
  275. uint8_t payload[];
  276. } __attribute__((packed)) smb_trans2_req;
  277. //// -> Trans2|FindFirst2
  278. typedef struct
  279. {
  280. uint16_t attrs; // Search attributes
  281. uint16_t count; // Search count
  282. uint16_t flags;
  283. uint16_t interest; // What kind of info do we want ?
  284. uint32_t storage; // ? => 0
  285. uint8_t pattern[]; // The queried pattern "\\folder\\*"
  286. } __attribute__((packed)) smb_tr2_find2;
  287. //// -> Trans2|QueryPathInfo
  288. typedef struct
  289. {
  290. uint16_t interest;
  291. uint32_t reserved;
  292. uint8_t path[];
  293. } __attribute__((packed)) smb_tr2_query;
  294. //<- Trans2
  295. typedef struct
  296. {
  297. uint8_t wct; // 10
  298. uint16_t total_param_count;
  299. uint16_t total_data_count;
  300. uint16_t reserved;
  301. uint16_t param_count;
  302. uint16_t param_offset;
  303. uint16_t param_displacement; // ??
  304. uint16_t data_count;
  305. uint16_t data_offset;
  306. uint16_t data_displacement; // ??
  307. uint8_t setup_count;
  308. uint8_t reserved2;
  309. uint16_t bct;
  310. uint8_t padding;
  311. uint8_t payload[];
  312. } __attribute__((packed)) smb_trans2_resp;
  313. //// <- Trans2|FindFirst2Params
  314. typedef struct
  315. {
  316. uint16_t id;
  317. uint16_t count;
  318. uint16_t eos;
  319. uint16_t ea_error_offset;
  320. uint16_t last_name_offset;
  321. uint16_t padding;
  322. } __attribute__((packed)) smb_tr2_find2_params;
  323. //// <- Trans2|FindFirst2FileInfo
  324. typedef struct
  325. {
  326. uint32_t next_entry;
  327. uint32_t index;
  328. uint64_t created; // File creation time
  329. uint64_t accessed; // File last access time
  330. uint64_t written; // File last write time
  331. uint64_t changed; // File last modification time
  332. uint64_t size;
  333. uint64_t alloc_size;
  334. uint32_t attr;
  335. uint32_t name_len;
  336. uint32_t ea_list_len;
  337. uint8_t short_name_len;
  338. uint8_t reserved;
  339. uint8_t short_name[24];
  340. uint8_t name[];
  341. } __attribute__((packed)) smb_tr2_find2_entry;
  342. //// <- Trans2|QueryPathInfo
  343. typedef struct
  344. {
  345. uint64_t created;
  346. uint64_t accessed;
  347. uint64_t written;
  348. uint64_t changed;
  349. uint32_t attr;
  350. uint32_t reserved;
  351. uint64_t alloc_size;
  352. uint64_t size;
  353. uint32_t link_count;
  354. uint8_t rm_pending;
  355. uint8_t is_dir;
  356. uint16_t reserved2;
  357. uint32_t ea_list_len;
  358. uint32_t name_len;
  359. uint8_t name[];
  360. } __attribute__((packed)) smb_tr2_path_info;
  361. //-> Example
  362. typedef struct
  363. {
  364. uint8_t wct; // ??
  365. SMB_ANDX_MEMBERS
  366. // Fill me
  367. uint16_t bct;
  368. //uint8_t padding;
  369. uint8_t file[];
  370. } __attribute__((packed)) smb_example_t;
  371. #endif