| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# smbinfo is a cmdline tool to query SMB-specific file and fs |
| 5 |
# information on a Linux SMB mount (cifs.ko). |
| 6 |
# |
| 7 |
# Copyright (C) 2019 Aurelien Aptel <[email protected]> |
| 8 |
# Copyright (C) 2019 Ronnie Sahlberg <[email protected]> |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation; either version 2 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program; if not, write to the Free Software |
| 20 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
|
| 22 |
import os |
| 23 |
import re |
| 24 |
import argparse |
| 25 |
import fcntl |
| 26 |
import struct |
| 27 |
import stat |
| 28 |
import datetime |
| 29 |
import calendar |
| 30 |
|
| 31 |
VERBOSE = False |
| 32 |
|
| 33 |
# ioctl ctl codes |
| 34 |
CIFS_QUERY_INFO = 0xc018cf07 |
| 35 |
CIFS_ENUMERATE_SNAPSHOTS = 0x800ccf06 |
| 36 |
CIFS_DUMP_KEY = 0xc03acf08 |
| 37 |
CIFS_DUMP_FULL_KEY = 0xc011cf0a |
| 38 |
CIFS_GET_TCON_INFO = 0x800ccf0c |
| 39 |
|
| 40 |
# large enough input buffer length |
| 41 |
INPUT_BUFFER_LENGTH = 16384 |
| 42 |
|
| 43 |
# length of a @GMT- token in bytes |
| 44 |
GMT_TOKEN_LEN_IN_BYTES = 24 * 2 |
| 45 |
|
| 46 |
# GMT format string |
| 47 |
GMT_FORMAT = "@GMT-%Y.%m.%d-%H.%M.%S" |
| 48 |
|
| 49 |
# cifs query flags |
| 50 |
PASSTHRU_QUERY_INFO = 0x00000000 |
| 51 |
PASSTHRU_FSCTL = 0x00000001 |
| 52 |
|
| 53 |
DIR_ACCESS_FLAGS = [ |
| 54 |
(0x00000001, "LIST_DIRECTORY"), |
| 55 |
(0x00000002, "ADD_FILE"), |
| 56 |
(0x00000004, "ADD_SUBDIRECTORY"), |
| 57 |
(0x00000008, "READ_EA"), |
| 58 |
(0x00000010, "WRITE_EA"), |
| 59 |
(0x00000020, "TRAVERSE"), |
| 60 |
(0x00000040, "DELETE_CHILD"), |
| 61 |
(0x00000080, "READ_ATTRIBUTES"), |
| 62 |
(0x00000100, "WRITE_ATTRIBUTES"), |
| 63 |
(0x00010000, "DELETE"), |
| 64 |
(0x00020000, "READ_CONTROL"), |
| 65 |
(0x00040000, "WRITE_DAC"), |
| 66 |
(0x00080000, "WRITE_OWNER"), |
| 67 |
(0x00100000, "SYNCHRONIZER"), |
| 68 |
(0x01000000, "ACCESS_SYSTEM_SECURITY"), |
| 69 |
(0x02000000, "MAXIMUM_ALLOWED"), |
| 70 |
(0x10000000, "GENERIC_ALL"), |
| 71 |
(0x20000000, "GENERIC_EXECUTE"), |
| 72 |
(0x40000000, "GENERIC_WRITE"), |
| 73 |
(0x80000000, "GENERIC_READ"), |
| 74 |
] |
| 75 |
|
| 76 |
FILE_ACCESS_FLAGS = [ |
| 77 |
(0x00000001, "READ_DATA"), |
| 78 |
(0x00000002, "WRITE_DATA"), |
| 79 |
(0x00000004, "APPEND_DATA"), |
| 80 |
(0x00000008, "READ_EA"), |
| 81 |
(0x00000010, "WRITE_EA"), |
| 82 |
(0x00000020, "EXECUTE"), |
| 83 |
(0x00000040, "DELETE_CHILD"), |
| 84 |
(0x00000080, "READ_ATTRIBUTES"), |
| 85 |
(0x00000100, "WRITE_ATTRIBUTES"), |
| 86 |
(0x00010000, "DELETE"), |
| 87 |
(0x00020000, "READ_CONTROL"), |
| 88 |
(0x00040000, "WRITE_DAC"), |
| 89 |
(0x00080000, "WRITE_OWNER"), |
| 90 |
(0x00100000, "SYNCHRONIZER"), |
| 91 |
(0x01000000, "ACCESS_SYSTEM_SECURITY"), |
| 92 |
(0x02000000, "MAXIMUM_ALLOWED"), |
| 93 |
(0x10000000, "GENERIC_ALL"), |
| 94 |
(0x20000000, "GENERIC_EXECUTE"), |
| 95 |
(0x40000000, "GENERIC_WRITE"), |
| 96 |
(0x80000000, "GENERIC_READ"), |
| 97 |
] |
| 98 |
|
| 99 |
FILE_ATTR_FLAGS = [ |
| 100 |
(0x00000001, "READ_ONLY"), |
| 101 |
(0x00000002, "HIDDEN"), |
| 102 |
(0x00000004, "SYSTEM"), |
| 103 |
(0x00000010, "DIRECTORY"), |
| 104 |
(0x00000020, "ARCHIVE"), |
| 105 |
(0x00000080, "NORMAL"), |
| 106 |
(0x00000100, "TEMPORARY"), |
| 107 |
(0x00000200, "SPARSE_FILE"), |
| 108 |
(0x00000400, "REPARSE_POINT"), |
| 109 |
(0x00000800, "COMPRESSED"), |
| 110 |
(0x00001000, "OFFLINE"), |
| 111 |
(0x00002000, "NOT_CONTENT_INDEXED"), |
| 112 |
(0x00004000, "ENCRYPTED"), |
| 113 |
(0x00008000, "INTEGRITY_STREAM"), |
| 114 |
(0x00020000, "NO_SCRUB_DATA"), |
| 115 |
] |
| 116 |
|
| 117 |
FILE_MODE_FLAGS = [ |
| 118 |
(0x00000002, "WRITE_THROUGH"), |
| 119 |
(0x00000004, "SEQUENTIAL_ONLY"), |
| 120 |
(0x00000008, "NO_INTERMEDIATE_BUFFERING"), |
| 121 |
(0x00000010, "SYNCHRONOUS_IO_ALERT"), |
| 122 |
(0x00000020, "SYNCHRONOUS_IO_NONALERT"), |
| 123 |
(0x00001000, "DELETE_ON_CLOSE"), |
| 124 |
] |
| 125 |
|
| 126 |
ALIGN_TYPES = [ |
| 127 |
(0, "BYTE_ALIGNMENT"), |
| 128 |
(1, "WORD_ALIGNMENT"), |
| 129 |
(3, "LONG_ALIGNMENT"), |
| 130 |
(7, "QUAD_ALIGNMENT"), |
| 131 |
(15, "OCTA_ALIGNMENT"), |
| 132 |
(31, "32_bit_ALIGNMENT"), |
| 133 |
(63, "64_bit_ALIGNMENT"), |
| 134 |
(127, "128_bit_ALIGNMENT"), |
| 135 |
(255, "254_bit_ALIGNMENT"), |
| 136 |
(511, "512_bit_ALIGNMENT"), |
| 137 |
] |
| 138 |
|
| 139 |
COMPRESSION_TYPES = [ |
| 140 |
(0x0000, "NONE"), |
| 141 |
(0x0002, "LZNT1"), |
| 142 |
] |
| 143 |
|
| 144 |
CONTROL_FLAGS = [ |
| 145 |
(0x8000, "SR"), |
| 146 |
(0x4000, "RM"), |
| 147 |
(0x2000, "PS"), |
| 148 |
(0x1000, "PD"), |
| 149 |
(0x0800, "SI"), |
| 150 |
(0x0400, "DI"), |
| 151 |
(0x0200, "SC"), |
| 152 |
(0x0100, "DC"), |
| 153 |
(0x0080, "DT"), |
| 154 |
(0x0040, "SS"), |
| 155 |
(0x0020, "SD"), |
| 156 |
(0x0010, "SP"), |
| 157 |
(0x0008, "DD"), |
| 158 |
(0x0004, "DP"), |
| 159 |
(0x0002, "GD"), |
| 160 |
(0x0001, "OD"), |
| 161 |
] |
| 162 |
|
| 163 |
ACE_TYPES = [ |
| 164 |
(0x00, "ALLOWED"), |
| 165 |
(0x01, "DENIED"), |
| 166 |
(0x02, "AUDIT"), |
| 167 |
(0x03, "ALARM"), |
| 168 |
(0x04, "ALLOWED_COMPOUND"), |
| 169 |
(0x05, "ALLOWED_OBJECT"), |
| 170 |
(0x06, "DENIED_OBJECT"), |
| 171 |
(0x07, "AUDIT_OBJECT"), |
| 172 |
(0x08, "ALARM_OBJECT"), |
| 173 |
(0x09, "ALLOWED_CALLBACK"), |
| 174 |
(0x0a, "DENIED_CALLBACK"), |
| 175 |
(0x0b, "ALLOWED_CALLBACK_OBJECT"), |
| 176 |
(0x0c, "DENIED_CALLBACK_OBJECT"), |
| 177 |
(0x0d, "AUDIT_CALLBACK"), |
| 178 |
(0x0e, "ALARM_CALLBACK"), |
| 179 |
(0x0f, "AUDIT_CALLBACK_OBJECT"), |
| 180 |
(0x10, "ALARM_CALLBACK_OBJECT"), |
| 181 |
(0x11, "MANDATORY_LABEL"), |
| 182 |
(0x12, "RESOURCE_ATTRIBUTE"), |
| 183 |
(0x13, "SCOPED_POLICY_ID"), |
| 184 |
] |
| 185 |
|
| 186 |
ACE_FLAGS = [ |
| 187 |
(0x80, "FAILED_ACCESS"), |
| 188 |
(0x40, "SUCCESSFUL_ACCESS"), |
| 189 |
(0x10, "INHERITED"), |
| 190 |
(0x08, "INHERIT_ONLY"), |
| 191 |
(0x04, "NO_PROPAGATE_INHERIT"), |
| 192 |
(0x02, "CONTAINER_INHERIT"), |
| 193 |
(0x01, "OBJECT_INHERIT"), |
| 194 |
] |
| 195 |
|
| 196 |
CIPHER_TYPES = [ |
| 197 |
(0x00, "AES-128-CCM"), |
| 198 |
(0x01, "AES-128-CCM"), |
| 199 |
(0x02, "AES-128-GCM"), |
| 200 |
(0x03, "AES-256-CCM"), |
| 201 |
(0x04, "AES-256-GCM"), |
| 202 |
] |
| 203 |
|
| 204 |
def main(): |
| 205 |
# |
| 206 |
# Global options and arguments |
| 207 |
# |
| 208 |
|
| 209 |
ap = argparse.ArgumentParser(description="Display SMB-specific file information using cifs IOCTL") |
| 210 |
ap.add_argument("-V", "--verbose", action="store_true", help="verbose output") |
| 211 |
subp = ap.add_subparsers(help="sub-commands help") |
| 212 |
subp.required = True |
| 213 |
subp.dest = 'subcommand' |
| 214 |
|
| 215 |
# |
| 216 |
# To add a new sub-command xxx, add a subparser xxx complete with |
| 217 |
# help, options and/or arguments and implement cmd_xxx() |
| 218 |
# |
| 219 |
|
| 220 |
sap = subp.add_parser("fileaccessinfo", help="Prints FileAccessInfo for a cifs file") |
| 221 |
sap.add_argument("file") |
| 222 |
sap.set_defaults(func=cmd_fileaccessinfo) |
| 223 |
|
| 224 |
sap = subp.add_parser("filealigninfo", help="Prints FileAlignInfo for a cifs file") |
| 225 |
sap.add_argument("file") |
| 226 |
sap.set_defaults(func=cmd_filealigninfo) |
| 227 |
|
| 228 |
sap = subp.add_parser("fileallinfo", help="Prints FileAllInfo for a cifs file") |
| 229 |
sap.add_argument("file") |
| 230 |
sap.set_defaults(func=cmd_fileallinfo) |
| 231 |
|
| 232 |
sap = subp.add_parser("filebasicinfo", help="Prints FileBasicInfo for a cifs file") |
| 233 |
sap.add_argument("file") |
| 234 |
sap.set_defaults(func=cmd_filebasicinfo) |
| 235 |
|
| 236 |
sap = subp.add_parser("fileeainfo", help="Prints FileEAInfo for a cifs file") |
| 237 |
sap.add_argument("file") |
| 238 |
sap.set_defaults(func=cmd_fileeainfo) |
| 239 |
|
| 240 |
sap = subp.add_parser("filefsfullsizeinfo", help="Prints FileFsFullSizeInfo for a cifs file") |
| 241 |
sap.add_argument("file") |
| 242 |
sap.set_defaults(func=cmd_filefsfullsizeinfo) |
| 243 |
|
| 244 |
sap = subp.add_parser("fileinternalinfo", help="Prints FileInternalInfo for a cifs file") |
| 245 |
sap.add_argument("file") |
| 246 |
sap.set_defaults(func=cmd_fileinternalinfo) |
| 247 |
|
| 248 |
sap = subp.add_parser("filemodeinfo", help="Prints FileModeInfo for a cifs file") |
| 249 |
sap.add_argument("file") |
| 250 |
sap.set_defaults(func=cmd_filemodeinfo) |
| 251 |
|
| 252 |
sap = subp.add_parser("filepositioninfo", help="Prints FilePositionInfo for a cifs file") |
| 253 |
sap.add_argument("file") |
| 254 |
sap.set_defaults(func=cmd_filepositioninfo) |
| 255 |
|
| 256 |
sap = subp.add_parser("filestandardinfo", help="Prints FileStandardInfo for a cifs file") |
| 257 |
sap.add_argument("file") |
| 258 |
sap.set_defaults(func=cmd_filestandardinfo) |
| 259 |
|
| 260 |
sap = subp.add_parser("filestreaminfo", help="Prints FileStreamInfo for a cifs file") |
| 261 |
sap.add_argument("file") |
| 262 |
sap.set_defaults(func=cmd_filestreaminfo) |
| 263 |
|
| 264 |
sap = subp.add_parser("fsctl-getobjid", help="Prints the objectid of the file and GUID of the underlying volume.") |
| 265 |
sap.add_argument("file") |
| 266 |
sap.set_defaults(func=cmd_fsctl_getobjid) |
| 267 |
|
| 268 |
sap = subp.add_parser("getcompression", help="Prints the compression setting for the file") |
| 269 |
sap.add_argument("file") |
| 270 |
sap.set_defaults(func=cmd_getcompression) |
| 271 |
|
| 272 |
sap = subp.add_parser("setcompression", help="Sets the compression level for the file") |
| 273 |
sap.add_argument("type", choices=['no','default','lznt1']) |
| 274 |
sap.add_argument("file") |
| 275 |
sap.set_defaults(func=cmd_setcompression) |
| 276 |
|
| 277 |
sap = subp.add_parser("list-snapshots", help="List the previous versions of the volume that backs this file") |
| 278 |
sap.add_argument("file") |
| 279 |
sap.set_defaults(func=cmd_list_snapshots) |
| 280 |
|
| 281 |
sap = subp.add_parser("quota", help="Prints the quota for a cifs file") |
| 282 |
sap.add_argument("file") |
| 283 |
sap.set_defaults(func=cmd_quota) |
| 284 |
|
| 285 |
sap = subp.add_parser("secdesc", help="Prints the security descriptor for a cifs file") |
| 286 |
sap.add_argument("file") |
| 287 |
sap.set_defaults(func=cmd_secdesc) |
| 288 |
|
| 289 |
sap = subp.add_parser("keys", help="Prints the decryption information needed to view encrypted network traces") |
| 290 |
sap.add_argument("file") |
| 291 |
sap.set_defaults(func=cmd_keys) |
| 292 |
|
| 293 |
sap = subp.add_parser("gettconinfo", help="Prints TCON Id and Session Id for a cifs file") |
| 294 |
sap.add_argument("file") |
| 295 |
sap.set_defaults(func=cmd_gettconinfo) |
| 296 |
|
| 297 |
# parse arguments |
| 298 |
args = ap.parse_args() |
| 299 |
|
| 300 |
# act on any global options |
| 301 |
if args.verbose: |
| 302 |
global VERBOSE |
| 303 |
VERBOSE = True |
| 304 |
|
| 305 |
# call subcommand function |
| 306 |
args.func(args) |
| 307 |
|
| 308 |
class QueryInfoStruct: |
| 309 |
def __init__(self, |
| 310 |
info_type=0, file_info_class=0, additional_information=0, |
| 311 |
flags=0, input_buffer_length=0, output_buffer_length=0): |
| 312 |
self.info_type = info_type |
| 313 |
self.file_info_class = file_info_class |
| 314 |
self.additional_information = additional_information |
| 315 |
self.flags = flags |
| 316 |
self.input_buffer_length = input_buffer_length |
| 317 |
self.output_buffer_length = output_buffer_length |
| 318 |
buf_size = max(self.input_buffer_length, self.output_buffer_length) |
| 319 |
self.input_buffer = bytearray(buf_size) |
| 320 |
|
| 321 |
def pack_input(self, fmt, offset, *vals): |
| 322 |
struct.pack_into(fmt, self.input_buffer, offset, *vals) |
| 323 |
|
| 324 |
def ioctl(self, fd, out_fmt=None): |
| 325 |
buf = bytearray() |
| 326 |
buf.extend(struct.pack("IIIIII", |
| 327 |
self.info_type, |
| 328 |
self.file_info_class, |
| 329 |
self.additional_information, |
| 330 |
self.flags, |
| 331 |
self.input_buffer_length, |
| 332 |
self.output_buffer_length)) |
| 333 |
in_len = len(buf) |
| 334 |
buf.extend(self.input_buffer) |
| 335 |
fcntl.ioctl(fd, CIFS_QUERY_INFO, buf, True) |
| 336 |
if out_fmt: |
| 337 |
return struct.unpack_from(out_fmt, buf, in_len) |
| 338 |
else: |
| 339 |
return buf[in_len:] |
| 340 |
|
| 341 |
def flags_to_str(flags, bitlist, verbose=None): |
| 342 |
if verbose is None: |
| 343 |
verbose = VERBOSE |
| 344 |
|
| 345 |
if not verbose: |
| 346 |
return "0x%08x"%flags |
| 347 |
|
| 348 |
out = [] |
| 349 |
for bit, name in bitlist: |
| 350 |
if flags & bit: |
| 351 |
out.append(name) |
| 352 |
|
| 353 |
return "0x%08x (%s)"%(flags, ",".join(out)) |
| 354 |
|
| 355 |
def type_to_str(typ, typelist, verbose=None): |
| 356 |
if verbose is None: |
| 357 |
verbose = VERBOSE |
| 358 |
|
| 359 |
if not verbose: |
| 360 |
return "0x%08x"%typ |
| 361 |
|
| 362 |
s = "Unknown" |
| 363 |
for val, name in typelist: |
| 364 |
if typ == val: |
| 365 |
s = name |
| 366 |
|
| 367 |
return "0x%08x (%s)"%(typ, s) |
| 368 |
|
| 369 |
def cmd_fileaccessinfo(args): |
| 370 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=8, input_buffer_length=4) |
| 371 |
try: |
| 372 |
fd = os.open(args.file, os.O_RDONLY) |
| 373 |
info = os.fstat(fd) |
| 374 |
buf = qi.ioctl(fd) |
| 375 |
except Exception as e: |
| 376 |
print("syscall failed: %s"%e) |
| 377 |
return False |
| 378 |
|
| 379 |
print_fileaccessinfo(buf, info) |
| 380 |
|
| 381 |
def print_fileaccessinfo(buf, info): |
| 382 |
flags = struct.unpack_from('<I', buf, 0)[0] |
| 383 |
if stat.S_ISDIR(info.st_mode): |
| 384 |
print("Directory access flags:", flags_to_str(flags, DIR_ACCESS_FLAGS)) |
| 385 |
else: |
| 386 |
print("File/Printer access flags:", flags_to_str(flags, FILE_ACCESS_FLAGS)) |
| 387 |
|
| 388 |
def cmd_filealigninfo(args): |
| 389 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=17, input_buffer_length=4) |
| 390 |
try: |
| 391 |
fd = os.open(args.file, os.O_RDONLY) |
| 392 |
buf = qi.ioctl(fd) |
| 393 |
except Exception as e: |
| 394 |
print("syscall failed: %s"%e) |
| 395 |
return False |
| 396 |
|
| 397 |
print_filealigninfo(buf) |
| 398 |
|
| 399 |
def print_filealigninfo(buf): |
| 400 |
mask = struct.unpack_from('<I', buf, 0)[0] |
| 401 |
print("File alignment: %s"%type_to_str(mask, ALIGN_TYPES)) |
| 402 |
|
| 403 |
def cmd_fileallinfo(args): |
| 404 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=18, input_buffer_length=INPUT_BUFFER_LENGTH) |
| 405 |
try: |
| 406 |
fd = os.open(args.file, os.O_RDONLY) |
| 407 |
info = os.fstat(fd) |
| 408 |
buf = qi.ioctl(fd) |
| 409 |
except Exception as e: |
| 410 |
print("syscall failed: %s"%e) |
| 411 |
return False |
| 412 |
|
| 413 |
print_filebasicinfo(buf) |
| 414 |
print_filestandardinfo(buf[40:]) |
| 415 |
print_fileinternalinfo(buf[64:]) |
| 416 |
print_fileeainfo(buf[72:]) |
| 417 |
print_fileaccessinfo(buf[76:], info) |
| 418 |
print_filepositioninfo(buf[80:]) |
| 419 |
print_filemodeinfo(buf[88:]) |
| 420 |
print_filealigninfo(buf[92:]) |
| 421 |
|
| 422 |
def win_to_datetime(smb2_time): |
| 423 |
usec = (smb2_time / 10) % 1000000 |
| 424 |
sec = (smb2_time - 116444736000000000) // 10000000 |
| 425 |
return datetime.datetime.fromtimestamp(sec + usec/10000000) |
| 426 |
|
| 427 |
def cmd_filebasicinfo(args): |
| 428 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=4, input_buffer_length=40) |
| 429 |
try: |
| 430 |
fd = os.open(args.file, os.O_RDONLY) |
| 431 |
buf = qi.ioctl(fd) |
| 432 |
except Exception as e: |
| 433 |
print("syscall failed: %s"%e) |
| 434 |
return False |
| 435 |
|
| 436 |
print_filebasicinfo(buf) |
| 437 |
|
| 438 |
def print_filebasicinfo(buf): |
| 439 |
ctime, atime, wtime, mtime, attrs = struct.unpack_from('<QQQQI', buf, 0) |
| 440 |
print("Creation Time: %s"%win_to_datetime(ctime)) |
| 441 |
print("Last Access Time: %s"%win_to_datetime(atime)) |
| 442 |
print("Last Write Time: %s"%win_to_datetime(wtime)) |
| 443 |
print("Last Change Time: %s"%win_to_datetime(mtime)) |
| 444 |
print("File Attributes: %s"%flags_to_str(attrs, FILE_ATTR_FLAGS)) |
| 445 |
|
| 446 |
def cmd_fileeainfo(args): |
| 447 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=7, input_buffer_length=4) |
| 448 |
try: |
| 449 |
fd = os.open(args.file, os.O_RDONLY) |
| 450 |
buf = qi.ioctl(fd) |
| 451 |
except Exception as e: |
| 452 |
print("syscall failed: %s"%e) |
| 453 |
return False |
| 454 |
|
| 455 |
print_fileeainfo(buf) |
| 456 |
|
| 457 |
def print_fileeainfo(buf): |
| 458 |
size = struct.unpack_from('<I', buf, 0)[0] |
| 459 |
print("EA Size: %d"%size) |
| 460 |
|
| 461 |
def cmd_filefsfullsizeinfo(args): |
| 462 |
qi = QueryInfoStruct(info_type=0x2, file_info_class=7, input_buffer_length=32) |
| 463 |
try: |
| 464 |
fd = os.open(args.file, os.O_RDONLY) |
| 465 |
total, caller_avail, actual_avail, sec_per_unit, byte_per_sec = qi.ioctl(fd, '<QQQII') |
| 466 |
except Exception as e: |
| 467 |
print("syscall failed: %s"%e) |
| 468 |
return False |
| 469 |
|
| 470 |
print("Total Allocation Units: %d"%total) |
| 471 |
print("Caller Available Allocation Units: %d"%caller_avail) |
| 472 |
print("Actual Available Allocation Units: %d"%actual_avail) |
| 473 |
print("Sectors Per Allocation Unit: %d"%sec_per_unit) |
| 474 |
print("Bytes Per Sector: %d"%byte_per_sec) |
| 475 |
|
| 476 |
def cmd_fileinternalinfo(args): |
| 477 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=6, input_buffer_length=8) |
| 478 |
try: |
| 479 |
fd = os.open(args.file, os.O_RDONLY) |
| 480 |
buf = qi.ioctl(fd) |
| 481 |
except Exception as e: |
| 482 |
print("syscall failed: %s"%e) |
| 483 |
return False |
| 484 |
|
| 485 |
print_fileinternalinfo(buf) |
| 486 |
|
| 487 |
def print_fileinternalinfo(buf): |
| 488 |
index = struct.unpack_from('<Q', buf, 0)[0] |
| 489 |
print("Index Number: %d"%index) |
| 490 |
|
| 491 |
|
| 492 |
def cmd_filemodeinfo(args): |
| 493 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=16, input_buffer_length=4) |
| 494 |
try: |
| 495 |
fd = os.open(args.file, os.O_RDONLY) |
| 496 |
buf = qi.ioctl(fd) |
| 497 |
except Exception as e: |
| 498 |
print("syscall failed: %s"%e) |
| 499 |
return False |
| 500 |
|
| 501 |
print_filemodeinfo(buf) |
| 502 |
|
| 503 |
def print_filemodeinfo(buf): |
| 504 |
mode = struct.unpack_from('<I', buf, 0)[0] |
| 505 |
print("Mode: %s"%flags_to_str(mode, FILE_MODE_FLAGS)) |
| 506 |
|
| 507 |
def cmd_filepositioninfo(args): |
| 508 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=14, input_buffer_length=8) |
| 509 |
try: |
| 510 |
fd = os.open(args.file, os.O_RDONLY) |
| 511 |
buf = qi.ioctl(fd) |
| 512 |
except Exception as e: |
| 513 |
print("syscall failed: %s"%e) |
| 514 |
return False |
| 515 |
|
| 516 |
print_filepositioninfo(buf) |
| 517 |
|
| 518 |
def print_filepositioninfo(buf): |
| 519 |
offset = struct.unpack_from('<Q', buf, 0)[0] |
| 520 |
print("Current Byte Offset: %d"%offset) |
| 521 |
|
| 522 |
def cmd_filestandardinfo(args): |
| 523 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=5, input_buffer_length=24) |
| 524 |
try: |
| 525 |
fd = os.open(args.file, os.O_RDONLY) |
| 526 |
buf = qi.ioctl(fd) |
| 527 |
except Exception as e: |
| 528 |
print("syscall failed: %s"%e) |
| 529 |
return False |
| 530 |
|
| 531 |
print_filestandardinfo(buf) |
| 532 |
|
| 533 |
def print_filestandardinfo(buf): |
| 534 |
nalloc, eof, nlink, del_pending, del_dir = struct.unpack_from('<QQIBB', buf, 0) |
| 535 |
print("Allocation Size: %d"%nalloc) |
| 536 |
print("End Of File: %d"%eof) |
| 537 |
print("Number of Links: %d"%nlink) |
| 538 |
print("Delete Pending: %d"%del_pending) |
| 539 |
print("Delete Directory: %d"%del_dir) |
| 540 |
|
| 541 |
def guid_to_str(buf): |
| 542 |
return "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"%struct.unpack_from('<IHHBBBBBBBB', buf, 0) |
| 543 |
|
| 544 |
def cmd_fsctl_getobjid(args): |
| 545 |
qi = QueryInfoStruct(info_type=0x9009c, file_info_class=5, flags=PASSTHRU_FSCTL, input_buffer_length=64) |
| 546 |
try: |
| 547 |
fd = os.open(args.file, os.O_RDONLY) |
| 548 |
buf = qi.ioctl(fd) |
| 549 |
except Exception as e: |
| 550 |
print("syscall failed: %s"%e) |
| 551 |
return False |
| 552 |
|
| 553 |
print("Object-ID: %s"%guid_to_str(buf)) |
| 554 |
print("Birth-Volume-ID: %s"%guid_to_str(buf[16:])) |
| 555 |
print("Birth-Object-ID: %s"%guid_to_str(buf[32:])) |
| 556 |
print("Domain-ID: %s"%guid_to_str(buf[48:])) |
| 557 |
|
| 558 |
def cmd_getcompression(args): |
| 559 |
qi = QueryInfoStruct(info_type=0x9003c, flags=PASSTHRU_FSCTL, input_buffer_length=2) |
| 560 |
try: |
| 561 |
fd = os.open(args.file, os.O_RDONLY) |
| 562 |
ctype = qi.ioctl(fd, '<H')[0] |
| 563 |
except Exception as e: |
| 564 |
print("syscall failed: %s"%e) |
| 565 |
return False |
| 566 |
|
| 567 |
ctype_name = "UNKNOWN" |
| 568 |
for val, name in COMPRESSION_TYPES: |
| 569 |
if ctype == val: |
| 570 |
ctype_name = name |
| 571 |
break |
| 572 |
print("Compression: %d (%s)"%(ctype, ctype_name)) |
| 573 |
|
| 574 |
def cmd_setcompression(args): |
| 575 |
qi = QueryInfoStruct(info_type=0x9c040, flags=PASSTHRU_FSCTL, output_buffer_length=2) |
| 576 |
type_map = {'no': 0, 'default': 1, 'lznt1': 2} |
| 577 |
qi.pack_input('<H', 0, type_map[args.type]) |
| 578 |
try: |
| 579 |
fd = os.open(args.file, os.O_RDONLY) |
| 580 |
qi.ioctl(fd) |
| 581 |
except Exception as e: |
| 582 |
print("syscall failed: %s"%e) |
| 583 |
return False |
| 584 |
|
| 585 |
class SnapshotArrayStruct: |
| 586 |
def __init__(self, |
| 587 |
nb_snapshots=0, |
| 588 |
nb_snapshots_returned=0, |
| 589 |
snapshot_array_size=12): |
| 590 |
self.nb_snapshots = nb_snapshots |
| 591 |
self.nb_snapshots_returned = nb_snapshots_returned |
| 592 |
self.snapshot_array_size = snapshot_array_size |
| 593 |
self.snapshot_array = [] |
| 594 |
|
| 595 |
def ioctl(self, fd, op): |
| 596 |
buf = bytearray() |
| 597 |
buf.extend(struct.pack("III", |
| 598 |
self.nb_snapshots, |
| 599 |
self.nb_snapshots_returned, |
| 600 |
self.snapshot_array_size)) |
| 601 |
|
| 602 |
buf.extend(bytearray(16 + self.snapshot_array_size)) |
| 603 |
fcntl.ioctl(fd, op, buf, True) |
| 604 |
|
| 605 |
out = SnapshotArrayStruct() |
| 606 |
out.nb_snapshots, out.nb_snapshots_returned, out.snapshot_array_size = struct.unpack_from('III', buf, 0) |
| 607 |
data = buf[12:] |
| 608 |
|
| 609 |
# '@\x00G\x00M\x00T\x00-\x002\x000\x001\x009\x00.\x000\x004\x00.\x000\x005\x00-\x002\x003\x00.\x001\x000\x00.\x005\x000\x00\x00\x00' |
| 610 |
index_start = 0 |
| 611 |
while index_start < len(data): |
| 612 |
gmt_start = data.find(b'@', index_start) |
| 613 |
if gmt_start == -1 or len(data) - gmt_start < GMT_TOKEN_LEN_IN_BYTES: |
| 614 |
break |
| 615 |
gmt = data[gmt_start:gmt_start + GMT_TOKEN_LEN_IN_BYTES] |
| 616 |
index_start = gmt_start + GMT_TOKEN_LEN_IN_BYTES |
| 617 |
out.snapshot_array.append(datetime.datetime.strptime(gmt.decode('utf-16'), GMT_FORMAT)) |
| 618 |
|
| 619 |
return out |
| 620 |
|
| 621 |
def datetime_to_smb(dt): |
| 622 |
ntfs_time_offset = (369*365 + 89) * 24 * 3600 * 10000000 |
| 623 |
return calendar.timegm(dt.timetuple()) * 10000000 + ntfs_time_offset |
| 624 |
|
| 625 |
def cmd_list_snapshots(args): |
| 626 |
sa1req = SnapshotArrayStruct() |
| 627 |
sa1res = None |
| 628 |
sa2req = None |
| 629 |
sa2res = None |
| 630 |
|
| 631 |
try: |
| 632 |
fd = os.open(args.file, os.O_RDONLY) |
| 633 |
sa1res = sa1req.ioctl(fd, CIFS_ENUMERATE_SNAPSHOTS) |
| 634 |
except Exception as e: |
| 635 |
print("syscall failed: %s"%e) |
| 636 |
return False |
| 637 |
|
| 638 |
if sa1res.nb_snapshots == 0: |
| 639 |
return |
| 640 |
|
| 641 |
sa2req = SnapshotArrayStruct(nb_snapshots=sa1res.nb_snapshots, snapshot_array_size=sa1res.snapshot_array_size) |
| 642 |
try: |
| 643 |
fd = os.open(args.file, os.O_RDONLY) |
| 644 |
sa2res = sa2req.ioctl(fd, CIFS_ENUMERATE_SNAPSHOTS) |
| 645 |
except Exception as e: |
| 646 |
print("syscall failed: %s"%e) |
| 647 |
return False |
| 648 |
|
| 649 |
|
| 650 |
print("Number of snapshots: %d Number of snapshots returned: %d"%(sa2res.nb_snapshots, sa2res.nb_snapshots_returned)) |
| 651 |
print("Snapshot list in GMT (Coordinated UTC Time) and SMB format (100 nanosecond units needed for snapshot mounts):") |
| 652 |
for i, d in enumerate(sa2res.snapshot_array): |
| 653 |
print("%d) GMT:%s\n SMB3:%d"%(i + 1, d.strftime(GMT_FORMAT), datetime_to_smb(d))) |
| 654 |
|
| 655 |
class SID: |
| 656 |
def __init__(self, buf, off=0): |
| 657 |
rev, sublen = struct.unpack_from('BB', buf, off+0) |
| 658 |
off += 2 |
| 659 |
auth = 0 |
| 660 |
subauth = [] |
| 661 |
for i in range(6): |
| 662 |
auth = (auth << 8)|buf[off] |
| 663 |
off += 1 |
| 664 |
for i in range(sublen): |
| 665 |
subauth.append(struct.unpack_from('<I', buf, off)) |
| 666 |
off += 4 |
| 667 |
|
| 668 |
self.rev = rev |
| 669 |
self.auth = auth |
| 670 |
self.subauth = subauth |
| 671 |
|
| 672 |
def __str__(self): |
| 673 |
auth = ("0x%x" if self.auth >= 2**32 else "%d")%self.auth |
| 674 |
return "S-%d-%s-%s"%(self.rev, auth, '-'.join(["%d"%x for x in self.subauth])) |
| 675 |
|
| 676 |
class ACE: |
| 677 |
def __init__(self, buf, off=0, is_dir=False): |
| 678 |
self.typ, self.flags, self.size = struct.unpack_from('<BBH', buf, off) |
| 679 |
self.is_dir = is_dir |
| 680 |
if self.typ not in [0,1,2]: |
| 681 |
self.buf = buf[4:] |
| 682 |
else: |
| 683 |
self.mask = struct.unpack_from('<I', buf, off+4)[0] |
| 684 |
self.sid = SID(buf, off+8) |
| 685 |
|
| 686 |
def __str__(self): |
| 687 |
s = [] |
| 688 |
s.append("Type: %s" % type_to_str(self.typ, ACE_TYPES)) |
| 689 |
s.append("Flags: %s" % flags_to_str(self.flags, ACE_FLAGS)) |
| 690 |
if self.typ not in [0,1,2]: |
| 691 |
s.append("<%s>"%(" ".join(["%02x"%x for x in self.buf]))) |
| 692 |
else: |
| 693 |
s.append("Mask: %s"%flags_to_str(self.mask, (DIR_ACCESS_FLAGS if self.is_dir else FILE_ACCESS_FLAGS))) |
| 694 |
s.append("SID: %s"%self.sid) |
| 695 |
return ", ".join(s) |
| 696 |
|
| 697 |
def cmd_quota(args): |
| 698 |
qi = QueryInfoStruct(info_type=0x04, input_buffer_length=INPUT_BUFFER_LENGTH) |
| 699 |
qi.pack_input('BBI', 0, |
| 700 |
0, # return single |
| 701 |
1, # restart scan |
| 702 |
0, # sid list length |
| 703 |
) |
| 704 |
qi.output_buffer_length = 16 |
| 705 |
buf = None |
| 706 |
|
| 707 |
try: |
| 708 |
fd = os.open(args.file, os.O_RDONLY) |
| 709 |
buf = qi.ioctl(fd) |
| 710 |
except Exception as e: |
| 711 |
print("syscall failed: %s"%e) |
| 712 |
return False |
| 713 |
|
| 714 |
off = 0 |
| 715 |
while off < len(buf): |
| 716 |
next_off = struct.unpack_from('<I', buf, off+ 0)[0] |
| 717 |
sid_len = struct.unpack_from('<I', buf, off+ 4)[0] |
| 718 |
atime = struct.unpack_from('<Q', buf, off+ 8)[0] |
| 719 |
qused = struct.unpack_from('<Q', buf, off+16)[0] |
| 720 |
qthresh = struct.unpack_from('<Q', buf, off+24)[0] |
| 721 |
qlimit = struct.unpack_from('<Q', buf, off+32)[0] |
| 722 |
sid = SID(buf, off+40) |
| 723 |
|
| 724 |
print("SID Length: %d"%sid_len) |
| 725 |
print("Change Time: %s"%win_to_datetime(atime)) |
| 726 |
print("Quota Used: %d"%qused) |
| 727 |
print("Quota Threshold:", ("NO THRESHOLD" if qthresh == 0xffffffffffffffff else "%d"%qthresh)) |
| 728 |
print("Quota Limit:", ("NO LIMIT" if qlimit == 0xffffffffffffffff else "%d"%qlimit)) |
| 729 |
print("SID: %s"%sid) |
| 730 |
|
| 731 |
if next_off == 0: |
| 732 |
break |
| 733 |
off += next_off |
| 734 |
|
| 735 |
def cmd_secdesc(args): |
| 736 |
qi = QueryInfoStruct(info_type=0x03, |
| 737 |
additional_information=0x7, # owner, group, dacl |
| 738 |
input_buffer_length=INPUT_BUFFER_LENGTH) |
| 739 |
buf = None |
| 740 |
info = None |
| 741 |
|
| 742 |
try: |
| 743 |
fd = os.open(args.file, os.O_RDONLY) |
| 744 |
info = os.fstat(fd) |
| 745 |
buf = qi.ioctl(fd) |
| 746 |
except Exception as e: |
| 747 |
print("syscall failed: %s"%e) |
| 748 |
return False |
| 749 |
|
| 750 |
is_dir = stat.S_ISDIR(info.st_mode) |
| 751 |
rev, ctrl, off_owner, off_group, off_dacl = struct.unpack_from('<BxHIIxxxxI', buf, 0) |
| 752 |
|
| 753 |
print("Revision: %d"%rev) |
| 754 |
print("Control: %s"%flags_to_str(ctrl, CONTROL_FLAGS)) |
| 755 |
if off_owner: |
| 756 |
print("Owner: %s"%SID(buf, off_owner)) |
| 757 |
if off_group: |
| 758 |
print("Group: %s"%SID(buf, off_group)) |
| 759 |
if off_dacl: |
| 760 |
print("DACL:") |
| 761 |
rev, count = struct.unpack_from('<BxxxH', buf, off_dacl) |
| 762 |
off_dacl += 8 |
| 763 |
for i in range(count): |
| 764 |
ace = ACE(buf, off_dacl, is_dir=is_dir) |
| 765 |
print(ace) |
| 766 |
off_dacl += ace.size |
| 767 |
|
| 768 |
def cmd_filestreaminfo(args): |
| 769 |
qi = QueryInfoStruct(info_type=0x1, file_info_class=22, input_buffer_length=INPUT_BUFFER_LENGTH) |
| 770 |
try: |
| 771 |
fd = os.open(args.file, os.O_RDONLY) |
| 772 |
info = os.fstat(fd) |
| 773 |
buf = qi.ioctl(fd) |
| 774 |
except Exception as e: |
| 775 |
print("syscall failed: %s"%e) |
| 776 |
return False |
| 777 |
|
| 778 |
print_filestreaminfo(buf) |
| 779 |
|
| 780 |
def print_filestreaminfo(buf): |
| 781 |
offset = 0 |
| 782 |
|
| 783 |
while offset < len(buf): |
| 784 |
|
| 785 |
next_offset = struct.unpack_from('<I', buf, offset + 0)[0] |
| 786 |
name_length = struct.unpack_from('<I', buf, offset + 4)[0] |
| 787 |
if (name_length > 0): |
| 788 |
stream_size = struct.unpack_from('<q', buf, offset + 8)[0] |
| 789 |
stream_alloc_size = struct.unpack_from('<q', buf, offset + 16)[0] |
| 790 |
stream_utf16le_name = struct.unpack_from('< %ss'% name_length, buf, offset + 24)[0] |
| 791 |
stream_name = stream_utf16le_name.decode("utf-16le") |
| 792 |
if (offset > 0): |
| 793 |
print() |
| 794 |
if (stream_name=="::$DATA"): |
| 795 |
print("Name: %s"% stream_name) |
| 796 |
else: |
| 797 |
print("Name: %s"% stream_name[stream_name.find(":") + 1 : stream_name.rfind(':$DATA')]) |
| 798 |
print("Size: %d bytes"% stream_size) |
| 799 |
print("Allocation size: %d bytes "% stream_alloc_size) |
| 800 |
|
| 801 |
if (next_offset == 0): |
| 802 |
break |
| 803 |
|
| 804 |
offset+=next_offset |
| 805 |
|
| 806 |
class KeyDebugInfoStruct: |
| 807 |
def __init__(self): |
| 808 |
self.suid = bytearray() |
| 809 |
self.cipher = 0 |
| 810 |
self.session_key = bytearray() |
| 811 |
self.enc_key = bytearray() |
| 812 |
self.dec_key = bytearray() |
| 813 |
|
| 814 |
def ioctl(self, fd): |
| 815 |
buf = bytearray() |
| 816 |
buf.extend(struct.pack("= 8s H 16s 16s 16s", self.suid, self.cipher, |
| 817 |
self.session_key, self.enc_key, self.dec_key)) |
| 818 |
fcntl.ioctl(fd, CIFS_DUMP_KEY, buf, True) |
| 819 |
(self.suid, self.cipher, self.session_key, |
| 820 |
self.enc_key, self.dec_key) = struct.unpack_from('= 8s H 16s 16s 16s', buf, 0) |
| 821 |
|
| 822 |
class FullKeyDebugInfoStruct: |
| 823 |
def __init__(self): |
| 824 |
# lets pick something large to be future proof |
| 825 |
# 17 + 3*32 would be strict minimum as of linux 5.13 |
| 826 |
self.in_size = 1024 |
| 827 |
self.suid = bytearray() |
| 828 |
self.cipher = 0 |
| 829 |
self.session_key_len = 0 |
| 830 |
self.server_in_key_len = 0 |
| 831 |
self.server_out_key_len = 0 |
| 832 |
|
| 833 |
def ioctl(self, fd): |
| 834 |
fmt = "= I 8s H B B B" |
| 835 |
size = struct.calcsize(fmt) |
| 836 |
buf = bytearray() |
| 837 |
buf.extend(struct.pack(fmt, self.in_size, self.suid, self.cipher, |
| 838 |
self.session_key_len, self.server_in_key_len, self.server_out_key_len)) |
| 839 |
buf.extend(bytearray(self.in_size-size)) |
| 840 |
fcntl.ioctl(fd, CIFS_DUMP_FULL_KEY, buf, True) |
| 841 |
(self.in_size, self.suid, self.cipher, |
| 842 |
self.session_key_len, self.server_in_key_len, |
| 843 |
self.server_out_key_len) = struct.unpack_from(fmt, buf, 0) |
| 844 |
|
| 845 |
end = size |
| 846 |
self.session_key = buf[end:end+self.session_key_len] |
| 847 |
end += self.session_key_len |
| 848 |
self.server_in_key = buf[end:end+self.server_in_key_len] |
| 849 |
end += self.server_in_key_len |
| 850 |
self.server_out_key = buf[end:end+self.server_out_key_len] |
| 851 |
|
| 852 |
def bytes_to_hex(buf): |
| 853 |
return " ".join(["%02x"%x for x in buf]) |
| 854 |
|
| 855 |
def cmd_keys(args): |
| 856 |
fd = os.open(args.file, os.O_RDONLY) |
| 857 |
kd = FullKeyDebugInfoStruct() |
| 858 |
|
| 859 |
try: |
| 860 |
# try new call first |
| 861 |
kd.ioctl(fd) |
| 862 |
except Exception as e: |
| 863 |
# new failed, try old call |
| 864 |
kd = KeyDebugInfoStruct() |
| 865 |
try: |
| 866 |
kd.ioctl(fd) |
| 867 |
except Exception as e: |
| 868 |
# both new and old call failed |
| 869 |
print("syscall failed: %s"%e) |
| 870 |
return False |
| 871 |
print("Session Id: %s"%bytes_to_hex(kd.suid)) |
| 872 |
print("Cipher: %s"%type_to_str(kd.cipher, CIPHER_TYPES, verbose=True)) |
| 873 |
print("Session Key: %s"%bytes_to_hex(kd.session_key)) |
| 874 |
print("Encryption key: %s"%bytes_to_hex(kd.enc_key)) |
| 875 |
print("Decryption key: %s"%bytes_to_hex(kd.dec_key)) |
| 876 |
else: |
| 877 |
# no exception, new call succeeded |
| 878 |
print("Session Id: %s"%bytes_to_hex(kd.suid)) |
| 879 |
print("Cipher: %s"%type_to_str(kd.cipher, CIPHER_TYPES, verbose=True)) |
| 880 |
print("Session Key: %s"%bytes_to_hex(kd.session_key)) |
| 881 |
print("ServerIn Key: %s"%bytes_to_hex(kd.server_in_key)) |
| 882 |
print("ServerOut key: %s"%bytes_to_hex(kd.server_out_key)) |
| 883 |
|
| 884 |
class SmbMntTconInfoStruct: |
| 885 |
def __init__(self): |
| 886 |
self.tid = 0 |
| 887 |
self.session_id = 0 |
| 888 |
|
| 889 |
def ioctl(self, fd): |
| 890 |
buf = bytearray() |
| 891 |
buf.extend(struct.pack("=IQ", self.tid, self.session_id)) |
| 892 |
fcntl.ioctl(fd, CIFS_GET_TCON_INFO, buf, True) |
| 893 |
(self.tid, self.session_id) = struct.unpack_from('=IQ', buf, 0) |
| 894 |
|
| 895 |
def cmd_gettconinfo(args): |
| 896 |
fd = os.open(args.file, os.O_RDONLY) |
| 897 |
tcon = SmbMntTconInfoStruct() |
| 898 |
|
| 899 |
try: |
| 900 |
tcon.ioctl(fd) |
| 901 |
except Exception as e: |
| 902 |
print("syscall failed: %s"%e) |
| 903 |
return False |
| 904 |
|
| 905 |
print("TCON Id: 0x%x"%tcon.tid) |
| 906 |
print("Session Id: 0x%x"%tcon.session_id) |
| 907 |
|
| 908 |
if __name__ == '__main__': |
| 909 |
main() |