#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # smbinfo is a cmdline tool to query SMB-specific file and fs # information on a Linux SMB mount (cifs.ko). # # Copyright (C) 2019 Aurelien Aptel # Copyright (C) 2019 Ronnie Sahlberg # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os import re import argparse import fcntl import struct import stat import datetime import calendar VERBOSE = False # ioctl ctl codes CIFS_QUERY_INFO = 0xc018cf07 CIFS_ENUMERATE_SNAPSHOTS = 0x800ccf06 CIFS_DUMP_KEY = 0xc03acf08 CIFS_DUMP_FULL_KEY = 0xc011cf0a # large enough input buffer length INPUT_BUFFER_LENGTH = 16384 # length of a @GMT- token in bytes GMT_TOKEN_LEN_IN_BYTES = 24 * 2 # GMT format string GMT_FORMAT = "@GMT-%Y.%m.%d-%H.%M.%S" # cifs query flags PASSTHRU_QUERY_INFO = 0x00000000 PASSTHRU_FSCTL = 0x00000001 DIR_ACCESS_FLAGS = [ (0x00000001, "LIST_DIRECTORY"), (0x00000002, "ADD_FILE"), (0x00000004, "ADD_SUBDIRECTORY"), (0x00000008, "READ_EA"), (0x00000010, "WRITE_EA"), (0x00000020, "TRAVERSE"), (0x00000040, "DELETE_CHILD"), (0x00000080, "READ_ATTRIBUTES"), (0x00000100, "WRITE_ATTRIBUTES"), (0x00010000, "DELETE"), (0x00020000, "READ_CONTROL"), (0x00040000, "WRITE_DAC"), (0x00080000, "WRITE_OWNER"), (0x00100000, "SYNCHRONIZER"), (0x01000000, "ACCESS_SYSTEM_SECURITY"), (0x02000000, "MAXIMUM_ALLOWED"), (0x10000000, "GENERIC_ALL"), (0x20000000, "GENERIC_EXECUTE"), (0x40000000, "GENERIC_WRITE"), (0x80000000, "GENERIC_READ"), ] FILE_ACCESS_FLAGS = [ (0x00000001, "READ_DATA"), (0x00000002, "WRITE_DATA"), (0x00000004, "APPEND_DATA"), (0x00000008, "READ_EA"), (0x00000010, "WRITE_EA"), (0x00000020, "EXECUTE"), (0x00000040, "DELETE_CHILD"), (0x00000080, "READ_ATTRIBUTES"), (0x00000100, "WRITE_ATTRIBUTES"), (0x00010000, "DELETE"), (0x00020000, "READ_CONTROL"), (0x00040000, "WRITE_DAC"), (0x00080000, "WRITE_OWNER"), (0x00100000, "SYNCHRONIZER"), (0x01000000, "ACCESS_SYSTEM_SECURITY"), (0x02000000, "MAXIMUM_ALLOWED"), (0x10000000, "GENERIC_ALL"), (0x20000000, "GENERIC_EXECUTE"), (0x40000000, "GENERIC_WRITE"), (0x80000000, "GENERIC_READ"), ] FILE_ATTR_FLAGS = [ (0x00000001, "READ_ONLY"), (0x00000002, "HIDDEN"), (0x00000004, "SYSTEM"), (0x00000010, "DIRECTORY"), (0x00000020, "ARCHIVE"), (0x00000080, "NORMAL"), (0x00000100, "TEMPORARY"), (0x00000200, "SPARSE_FILE"), (0x00000400, "REPARSE_POINT"), (0x00000800, "COMPRESSED"), (0x00001000, "OFFLINE"), (0x00002000, "NOT_CONTENT_INDEXED"), (0x00004000, "ENCRYPTED"), (0x00008000, "INTEGRITY_STREAM"), (0x00020000, "NO_SCRUB_DATA"), ] FILE_MODE_FLAGS = [ (0x00000002, "WRITE_THROUGH"), (0x00000004, "SEQUENTIAL_ONLY"), (0x00000008, "NO_INTERMEDIATE_BUFFERING"), (0x00000010, "SYNCHRONOUS_IO_ALERT"), (0x00000020, "SYNCHRONOUS_IO_NONALERT"), (0x00001000, "DELETE_ON_CLOSE"), ] ALIGN_TYPES = [ (0, "BYTE_ALIGNMENT"), (1, "WORD_ALIGNMENT"), (3, "LONG_ALIGNMENT"), (7, "QUAD_ALIGNMENT"), (15, "OCTA_ALIGNMENT"), (31, "32_bit_ALIGNMENT"), (63, "64_bit_ALIGNMENT"), (127, "128_bit_ALIGNMENT"), (255, "254_bit_ALIGNMENT"), (511, "512_bit_ALIGNMENT"), ] COMPRESSION_TYPES = [ (0x0000, "NONE"), (0x0002, "LZNT1"), ] CONTROL_FLAGS = [ (0x8000, "SR"), (0x4000, "RM"), (0x2000, "PS"), (0x1000, "PD"), (0x0800, "SI"), (0x0400, "DI"), (0x0200, "SC"), (0x0100, "DC"), (0x0080, "DT"), (0x0040, "SS"), (0x0020, "SD"), (0x0010, "SP"), (0x0008, "DD"), (0x0004, "DP"), (0x0002, "GD"), (0x0001, "OD"), ] ACE_TYPES = [ (0x00, "ALLOWED"), (0x01, "DENIED"), (0x02, "AUDIT"), (0x03, "ALARM"), (0x04, "ALLOWED_COMPOUND"), (0x05, "ALLOWED_OBJECT"), (0x06, "DENIED_OBJECT"), (0x07, "AUDIT_OBJECT"), (0x08, "ALARM_OBJECT"), (0x09, "ALLOWED_CALLBACK"), (0x0a, "DENIED_CALLBACK"), (0x0b, "ALLOWED_CALLBACK_OBJECT"), (0x0c, "DENIED_CALLBACK_OBJECT"), (0x0d, "AUDIT_CALLBACK"), (0x0e, "ALARM_CALLBACK"), (0x0f, "AUDIT_CALLBACK_OBJECT"), (0x10, "ALARM_CALLBACK_OBJECT"), (0x11, "MANDATORY_LABEL"), (0x12, "RESOURCE_ATTRIBUTE"), (0x13, "SCOPED_POLICY_ID"), ] ACE_FLAGS = [ (0x80, "FAILED_ACCESS"), (0x40, "SUCCESSFUL_ACCESS"), (0x10, "INHERITED"), (0x08, "INHERIT_ONLY"), (0x04, "NO_PROPAGATE_INHERIT"), (0x02, "CONTAINER_INHERIT"), (0x01, "OBJECT_INHERIT"), ] CIPHER_TYPES = [ (0x00, "AES-128-CCM"), (0x01, "AES-128-CCM"), (0x02, "AES-128-GCM"), (0x03, "AES-256-CCM"), (0x04, "AES-256-GCM"), ] def main(): # # Global options and arguments # ap = argparse.ArgumentParser(description="Display SMB-specific file information using cifs IOCTL") ap.add_argument("-V", "--verbose", action="store_true", help="verbose output") subp = ap.add_subparsers(help="sub-commands help") subp.required = True subp.dest = 'subcommand' # # To add a new sub-command xxx, add a subparser xxx complete with # help, options and/or arguments and implement cmd_xxx() # sap = subp.add_parser("fileaccessinfo", help="Prints FileAccessInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_fileaccessinfo) sap = subp.add_parser("filealigninfo", help="Prints FileAlignInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filealigninfo) sap = subp.add_parser("fileallinfo", help="Prints FileAllInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_fileallinfo) sap = subp.add_parser("filebasicinfo", help="Prints FileBasicInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filebasicinfo) sap = subp.add_parser("fileeainfo", help="Prints FileEAInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_fileeainfo) sap = subp.add_parser("filefsfullsizeinfo", help="Prints FileFsFullSizeInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filefsfullsizeinfo) sap = subp.add_parser("fileinternalinfo", help="Prints FileInternalInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_fileinternalinfo) sap = subp.add_parser("filemodeinfo", help="Prints FileModeInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filemodeinfo) sap = subp.add_parser("filepositioninfo", help="Prints FilePositionInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filepositioninfo) sap = subp.add_parser("filestandardinfo", help="Prints FileStandardInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filestandardinfo) sap = subp.add_parser("filestreaminfo", help="Prints FileStreamInfo for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_filestreaminfo) sap = subp.add_parser("fsctl-getobjid", help="Prints the objectid of the file and GUID of the underlying volume.") sap.add_argument("file") sap.set_defaults(func=cmd_fsctl_getobjid) sap = subp.add_parser("getcompression", help="Prints the compression setting for the file") sap.add_argument("file") sap.set_defaults(func=cmd_getcompression) sap = subp.add_parser("setcompression", help="Sets the compression level for the file") sap.add_argument("type", choices=['no','default','lznt1']) sap.add_argument("file") sap.set_defaults(func=cmd_setcompression) sap = subp.add_parser("list-snapshots", help="List the previous versions of the volume that backs this file") sap.add_argument("file") sap.set_defaults(func=cmd_list_snapshots) sap = subp.add_parser("quota", help="Prints the quota for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_quota) sap = subp.add_parser("secdesc", help="Prints the security descriptor for a cifs file") sap.add_argument("file") sap.set_defaults(func=cmd_secdesc) sap = subp.add_parser("keys", help="Prints the decryption information needed to view encrypted network traces") sap.add_argument("file") sap.set_defaults(func=cmd_keys) # parse arguments args = ap.parse_args() # act on any global options if args.verbose: global VERBOSE VERBOSE = True # call subcommand function args.func(args) class QueryInfoStruct: def __init__(self, info_type=0, file_info_class=0, additional_information=0, flags=0, input_buffer_length=0, output_buffer_length=0): self.info_type = info_type self.file_info_class = file_info_class self.additional_information = additional_information self.flags = flags self.input_buffer_length = input_buffer_length self.output_buffer_length = output_buffer_length buf_size = max(self.input_buffer_length, self.output_buffer_length) self.input_buffer = bytearray(buf_size) def pack_input(self, fmt, offset, *vals): struct.pack_into(fmt, self.input_buffer, offset, *vals) def ioctl(self, fd, out_fmt=None): buf = bytearray() buf.extend(struct.pack("IIIIII", self.info_type, self.file_info_class, self.additional_information, self.flags, self.input_buffer_length, self.output_buffer_length)) in_len = len(buf) buf.extend(self.input_buffer) fcntl.ioctl(fd, CIFS_QUERY_INFO, buf, True) if out_fmt: return struct.unpack_from(out_fmt, buf, in_len) else: return buf[in_len:] def flags_to_str(flags, bitlist, verbose=None): if verbose is None: verbose = VERBOSE if not verbose: return "0x%08x"%flags out = [] for bit, name in bitlist: if flags & bit: out.append(name) return "0x%08x (%s)"%(flags, ",".join(out)) def type_to_str(typ, typelist, verbose=None): if verbose is None: verbose = VERBOSE if not verbose: return "0x%08x"%typ s = "Unknown" for val, name in typelist: if typ == val: s = name return "0x%08x (%s)"%(typ, s) def cmd_fileaccessinfo(args): qi = QueryInfoStruct(info_type=0x1, file_info_class=8, input_buffer_length=4) try: fd = os.open(args.file, os.O_RDONLY) info = os.fstat(fd) buf = qi.ioctl(fd) except Exception as e: print("syscall failed: %s"%e) return False print_fileaccessinfo(buf, info) def print_fileaccessinfo(buf, info): flags = struct.unpack_from('= 2**32 else "%d")%self.auth return "S-%d-%s-%s"%(self.rev, auth, '-'.join(["%d"%x for x in self.subauth])) class ACE: def __init__(self, buf, off=0, is_dir=False): self.typ, self.flags, self.size = struct.unpack_from('"%(" ".join(["%02x"%x for x in self.buf]))) else: s.append("Mask: %s"%flags_to_str(self.mask, (DIR_ACCESS_FLAGS if self.is_dir else FILE_ACCESS_FLAGS))) s.append("SID: %s"%self.sid) return ", ".join(s) def cmd_quota(args): qi = QueryInfoStruct(info_type=0x04, input_buffer_length=INPUT_BUFFER_LENGTH) qi.pack_input('BBI', 0, 0, # return single 1, # restart scan 0, # sid list length ) qi.output_buffer_length = 16 buf = None try: fd = os.open(args.file, os.O_RDONLY) buf = qi.ioctl(fd) except Exception as e: print("syscall failed: %s"%e) return False off = 0 while off < len(buf): next_off = struct.unpack_from(' 0): stream_size = struct.unpack_from(' 0): print() if (stream_name=="::$DATA"): print("Name: %s"% stream_name) else: print("Name: %s"% stream_name[stream_name.find(":") + 1 : stream_name.rfind(':$DATA')]) print("Size: %d bytes"% stream_size) print("Allocation size: %d bytes "% stream_alloc_size) if (next_offset == 0): break offset+=next_offset class KeyDebugInfoStruct: def __init__(self): self.suid = bytearray() self.cipher = 0 self.session_key = bytearray() self.enc_key = bytearray() self.dec_key = bytearray() def ioctl(self, fd): buf = bytearray() buf.extend(struct.pack("= 8s H 16s 16s 16s", self.suid, self.cipher, self.session_key, self.enc_key, self.dec_key)) fcntl.ioctl(fd, CIFS_DUMP_KEY, buf, True) (self.suid, self.cipher, self.session_key, self.enc_key, self.dec_key) = struct.unpack_from('= 8s H 16s 16s 16s', buf, 0) class FullKeyDebugInfoStruct: def __init__(self): # lets pick something large to be future proof # 17 + 3*32 would be strict minimum as of linux 5.13 self.in_size = 1024 self.suid = bytearray() self.cipher = 0 self.session_key_len = 0 self.server_in_key_len = 0 self.server_out_key_len = 0 def ioctl(self, fd): fmt = "= I 8s H B B B" size = struct.calcsize(fmt) buf = bytearray() buf.extend(struct.pack(fmt, self.in_size, self.suid, self.cipher, self.session_key_len, self.server_in_key_len, self.server_out_key_len)) buf.extend(bytearray(self.in_size-size)) fcntl.ioctl(fd, CIFS_DUMP_FULL_KEY, buf, True) (self.in_size, self.suid, self.cipher, self.session_key_len, self.server_in_key_len, self.server_out_key_len) = struct.unpack_from(fmt, buf, 0) end = size self.session_key = buf[end:end+self.session_key_len] end += self.session_key_len self.server_in_key = buf[end:end+self.server_in_key_len] end += self.server_in_key_len self.server_out_key = buf[end:end+self.server_out_key_len] def bytes_to_hex(buf): return " ".join(["%02x"%x for x in buf]) def cmd_keys(args): fd = os.open(args.file, os.O_RDONLY) kd = FullKeyDebugInfoStruct() try: # try new call first kd.ioctl(fd) except Exception as e: # new failed, try old call kd = KeyDebugInfoStruct() try: kd.ioctl(fd) except Exception as e: # both new and old call failed print("syscall failed: %s"%e) return False print("Session Id: %s"%bytes_to_hex(kd.suid)) print("Cipher: %s"%type_to_str(kd.cipher, CIPHER_TYPES, verbose=True)) print("Session Key: %s"%bytes_to_hex(kd.session_key)) print("Encryption key: %s"%bytes_to_hex(kd.enc_key)) print("Decryption key: %s"%bytes_to_hex(kd.dec_key)) else: # no exception, new call succeeded print("Session Id: %s"%bytes_to_hex(kd.suid)) print("Cipher: %s"%type_to_str(kd.cipher, CIPHER_TYPES, verbose=True)) print("Session Key: %s"%bytes_to_hex(kd.session_key)) print("ServerIn Key: %s"%bytes_to_hex(kd.server_in_key)) print("ServerOut key: %s"%bytes_to_hex(kd.server_out_key)) if __name__ == '__main__': main()