smb_packets.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //---------------------------------------------------------------------------
  2. // __________________ _________ _____ _____ .__ ._.
  3. // \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. // | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. // | | \| ` \/ / Y \ / | | \ ___/ \|
  6. // |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  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_negotiate_req;
  56. //<- Negotiate Protocol
  57. typedef struct
  58. {
  59. uint8_t wct; // +-17 :)
  60. uint16_t dialect_index; //
  61. uint8_t security_mode; // Share/User. Plaintext/Challenge
  62. uint32_t diplodocus;
  63. uint32_t max_bufsize; // Max buffer size requested by server.
  64. uint32_t max_rawbuffer; // Max raw buffer size requested by serv.
  65. uint32_t session_key; // 'MUST' be returned to server
  66. uint32_t caps;
  67. uint64_t ts; // I don't give a fuck (or do i?)
  68. uint16_t tz; // Even less fuck given
  69. uint8_t key_length; // Size of challenge key, if != 8 then shit
  70. uint16_t bct;
  71. uint64_t challenge; // Normally 8 bytes, if not then wtf monkey
  72. uint8_t payload[]; // The rest isn't really meaningfull for us
  73. } __attribute__((packed)) smb_negotiate_resp;
  74. //-> Session Setup
  75. typedef struct
  76. {
  77. uint8_t wct; // +-13 :)
  78. SMB_ANDX_MEMBERS
  79. uint16_t max_buffer; // Maximum size we can receive
  80. uint16_t mpx_count; // maximum multiplexed session
  81. uint16_t vc_count; // Virtual ciruits -> 1!
  82. uint32_t session_key; // 0x00000000
  83. uint16_t oem_pass_len; // Length of LM2 response
  84. uint16_t uni_pass_len; // Length of NTLM2 response
  85. uint32_t reserved2; // 0x00000000
  86. uint32_t caps; // Capabilities
  87. uint16_t payload_size;
  88. uint8_t payload[];
  89. } __attribute__((packed)) smb_session_req;
  90. //<- Session Setup
  91. typedef struct
  92. {
  93. uint8_t wct;
  94. SMB_ANDX_MEMBERS
  95. uint16_t action;
  96. uint16_t bct;
  97. uint8_t bullshit[];
  98. } __attribute__((packed)) smb_session_resp;
  99. //-> Tree Connect
  100. typedef struct
  101. {
  102. uint8_t wct; // 4
  103. SMB_ANDX_MEMBERS
  104. uint16_t flags;
  105. uint16_t passwd_len; // 1 if not used. Used in Share Level Auth
  106. uint16_t bct;
  107. uint8_t payload[]; // Password | Path | Service
  108. } __attribute__((packed)) smb_tree_connect_req;
  109. //<- Tree Connect
  110. typedef struct
  111. {
  112. uint8_t wct; // 7
  113. SMB_ANDX_MEMBERS
  114. uint16_t opt_support;
  115. uint32_t max_rights;
  116. uint32_t guest_rights;
  117. uint16_t bct;
  118. uint8_t payload[];
  119. } __attribute__((packed)) smb_tree_connect_resp;
  120. //-> Create File
  121. typedef struct
  122. {
  123. uint8_t wct; // 24
  124. SMB_ANDX_MEMBERS
  125. uint8_t reserved2;
  126. uint16_t path_length;
  127. uint32_t flags;
  128. uint32_t root_fid;
  129. uint32_t access_mask;
  130. uint64_t alloc_size;
  131. uint32_t file_attr;
  132. uint32_t share_access;
  133. uint32_t disposition;
  134. uint32_t create_opts;
  135. uint32_t impersonation;
  136. uint8_t security_flags;
  137. uint16_t bct;
  138. uint8_t path[]; // UTF16 Path, starting with '\'
  139. } __attribute__((packed)) smb_create_req;
  140. //<- Create File
  141. typedef struct
  142. {
  143. uint8_t wct; // 34
  144. SMB_ANDX_MEMBERS
  145. uint8_t oplock_level;
  146. uint16_t fid;
  147. uint32_t action;
  148. uint64_t created; // File creation time
  149. uint64_t accessed; // File last access time
  150. uint64_t written; // File last write time
  151. uint64_t changed; // File last modification time
  152. uint32_t attr;
  153. uint64_t alloc_size;
  154. uint64_t size;
  155. uint16_t filetype;
  156. uint16_t ipc_state;
  157. uint8_t is_dir;
  158. uint16_t bct; // 0
  159. } __attribute__((packed)) smb_create_resp;
  160. //-> Close File
  161. typedef struct
  162. {
  163. uint8_t wct; // 3
  164. uint16_t fid;
  165. uint32_t last_write; // Not defined == 0xffffffff
  166. uint16_t bct; // 0
  167. } __attribute__((packed)) smb_close_req;
  168. //-> Read File
  169. typedef struct
  170. {
  171. uint8_t wct; // 12
  172. SMB_ANDX_MEMBERS
  173. uint16_t fid;
  174. uint32_t offset;
  175. uint16_t max_count;
  176. uint16_t min_count;
  177. uint32_t max_count_high; // Continuation of max_count field
  178. uint16_t remaining;
  179. uint32_t offset_high; // Continuation of offset field'
  180. uint16_t bct; // 0
  181. } __attribute__((packed)) smb_read_req;
  182. //<- Read File
  183. typedef struct
  184. {
  185. uint8_t wct; // 12
  186. SMB_ANDX_MEMBERS
  187. uint16_t remaining;
  188. uint16_t compact_mode;
  189. uint16_t reserved;
  190. uint16_t data_len;
  191. uint16_t data_offset;
  192. uint32_t data_len_high;
  193. uint32_t reserved2;
  194. uint16_t reserved3;
  195. uint16_t bct;
  196. uint8_t padding;
  197. uint8_t file[];
  198. } __attribute__((packed)) smb_read_resp;
  199. //-> Trans
  200. typedef struct
  201. {
  202. uint8_t wct; // 16
  203. uint16_t total_param_count;
  204. uint16_t total_data_count;
  205. uint16_t max_param_count;
  206. uint16_t max_data_count;
  207. uint8_t max_setup_count;
  208. uint8_t reserved;
  209. uint16_t flags;
  210. uint32_t timeout;
  211. uint16_t reserved2;
  212. uint16_t param_count;
  213. uint16_t param_offset;
  214. uint16_t data_count;
  215. uint16_t data_offset;
  216. uint8_t setup_count;
  217. uint8_t reserved3;
  218. uint16_t pipe_function;
  219. uint16_t fid;
  220. uint16_t bct;
  221. uint8_t payload[];
  222. } __attribute__((packed)) smb_trans_req;
  223. //-> Trans2
  224. typedef struct
  225. {
  226. uint8_t wct; // 15
  227. uint16_t total_param_count;
  228. uint16_t total_data_count;
  229. uint16_t max_param_count;
  230. uint16_t max_data_count;
  231. uint8_t max_setup_count;
  232. uint8_t reserved;
  233. uint16_t flags;
  234. uint32_t timeout;
  235. uint16_t reserve2;
  236. uint16_t param_count;
  237. uint16_t param_offset;
  238. uint16_t data_count;
  239. uint16_t data_offset;
  240. uint8_t setup_count;
  241. uint8_t reserved3;
  242. uint16_t cmd;
  243. uint16_t bct;
  244. uint8_t padding[3];
  245. uint8_t payload[];
  246. } __attribute__((packed)) smb_trans2_req;
  247. //// -> Trans2|FindFirst2
  248. typedef struct
  249. {
  250. uint16_t attrs; // Search attributes
  251. uint16_t count; // Search count
  252. uint16_t flags;
  253. uint16_t interest; // What kind of info do we want ?
  254. uint32_t storage; // ? => 0
  255. uint8_t pattern[]; // The queried pattern "\\folder\\*"
  256. } __attribute__((packed)) smb_tr2_find2;
  257. //// -> Trans2|QueryPathInfo
  258. typedef struct
  259. {
  260. uint16_t interest;
  261. uint32_t reserved;
  262. uint8_t path[];
  263. } __attribute__((packed)) smb_tr2_query;
  264. //<- Trans2
  265. typedef struct
  266. {
  267. uint8_t wct; // 10
  268. uint16_t total_param_count;
  269. uint16_t total_data_count;
  270. uint16_t reserved;
  271. uint16_t param_count;
  272. uint16_t param_offset;
  273. uint16_t param_displacement; // ??
  274. uint16_t data_count;
  275. uint16_t data_offset;
  276. uint16_t data_displacement; // ??
  277. uint8_t setup_count;
  278. uint8_t reserved2;
  279. uint16_t bct;
  280. uint8_t padding;
  281. uint8_t payload[];
  282. } __attribute__((packed)) smb_trans2_resp;
  283. //// <- Trans2|FindFirst2Params
  284. typedef struct
  285. {
  286. uint16_t id;
  287. uint16_t count;
  288. uint16_t eos;
  289. uint16_t ea_error_offset;
  290. uint16_t last_name_offset;
  291. uint16_t padding;
  292. } __attribute__((packed)) smb_tr2_find2_params;
  293. //// <- Trans2|FindFirst2FileInfo
  294. typedef struct
  295. {
  296. uint32_t next_entry;
  297. uint32_t index;
  298. uint64_t created; // File creation time
  299. uint64_t accessed; // File last access time
  300. uint64_t written; // File last write time
  301. uint64_t changed; // File last modification time
  302. uint64_t size;
  303. uint64_t alloc_size;
  304. uint32_t attr;
  305. uint32_t name_len;
  306. uint32_t ea_list_len;
  307. uint8_t short_name_len;
  308. uint8_t reserved;
  309. uint8_t short_name[24];
  310. uint8_t name[];
  311. } __attribute__((packed)) smb_tr2_find2_entry;
  312. //// <- Trans2|QueryPathInfo
  313. typedef struct
  314. {
  315. uint64_t created;
  316. uint64_t accessed;
  317. uint64_t written;
  318. uint64_t changed;
  319. uint32_t attr;
  320. uint32_t reserved;
  321. uint64_t alloc_size;
  322. uint64_t size;
  323. uint32_t link_count;
  324. uint8_t rm_pending;
  325. uint8_t is_dir;
  326. uint16_t reserved2;
  327. uint32_t ea_list_len;
  328. uint32_t name_len;
  329. uint8_t name[];
  330. } __attribute__((packed)) smb_tr2_path_info;
  331. //-> Example
  332. typedef struct
  333. {
  334. uint8_t wct; // ??
  335. SMB_ANDX_MEMBERS
  336. // Fill me
  337. uint16_t bct;
  338. //uint8_t padding;
  339. uint8_t file[];
  340. } __attribute__((packed)) smb_example_t;
  341. #endif