def create_smb( smb_client, treeId, fileName, desiredAccess, shareMode, creationOptions, creationDisposition, fileAttributes, impersonationLevel=SMB2_IL_IMPERSONATION, oplockLevel=SMB2_OPLOCK_LEVEL_NONE, createContexts=None, ): packet = smb_client.getSMBServer().SMB_PACKET() packet["Command"] = SMB2_CREATE packet["TreeID"] = treeId if smb_client._SMBConnection._Session["TreeConnectTable"][treeId][ "IsDfsShare"] is True: packet["Flags"] = SMB2_FLAGS_DFS_OPERATIONS smb2Create = SMB2Create() smb2Create["SecurityFlags"] = 0 smb2Create["RequestedOplockLevel"] = oplockLevel smb2Create["ImpersonationLevel"] = impersonationLevel smb2Create["DesiredAccess"] = desiredAccess smb2Create["FileAttributes"] = fileAttributes smb2Create["ShareAccess"] = shareMode smb2Create["CreateDisposition"] = creationDisposition smb2Create["CreateOptions"] = creationOptions smb2Create["NameLength"] = len(fileName) * 2 if fileName != "": smb2Create["Buffer"] = fileName.encode("utf-16le") else: smb2Create["Buffer"] = b"\x00" if createContexts is not None: smb2Create["Buffer"] += createContexts smb2Create["CreateContextsOffset"] = (len(SMB2Packet()) + SMB2Create.SIZE + smb2Create["NameLength"]) smb2Create["CreateContextsLength"] = len(createContexts) else: smb2Create["CreateContextsOffset"] = 0 smb2Create["CreateContextsLength"] = 0 packet["Data"] = smb2Create packetID = smb_client.getSMBServer().sendSMB(packet) ans = smb_client.getSMBServer().recvSMB(packetID) if ans.isValidAnswer(STATUS_SUCCESS): createResponse = SMB2Create_Response(ans["Data"]) # The client MUST generate a handle for the Open, and it MUST # return success and the generated handle to the calling application. # In our case, str(FileID) return str(createResponse["FileID"])
def create(self, treeId, fileName, desiredAccess, shareMode, creationOptions, creationDisposition, fileAttributes, impersonationLevel=SMB2_IL_IMPERSONATION, securityFlags=0, oplockLevel=SMB2_OPLOCK_LEVEL_NONE, createContexts=None): packet = self.__smbClient.getSMBServer().SMB_PACKET() packet['Command'] = SMB2_CREATE packet['TreeID'] = treeId if self.__smbClient._SMBConnection._Session['TreeConnectTable'][ treeId]['IsDfsShare'] is True: packet['Flags'] = SMB2_FLAGS_DFS_OPERATIONS smb2Create = SMB2Create() smb2Create['SecurityFlags'] = 0 smb2Create['RequestedOplockLevel'] = oplockLevel smb2Create['ImpersonationLevel'] = impersonationLevel smb2Create['DesiredAccess'] = desiredAccess smb2Create['FileAttributes'] = fileAttributes smb2Create['ShareAccess'] = shareMode smb2Create['CreateDisposition'] = creationDisposition smb2Create['CreateOptions'] = creationOptions smb2Create['NameLength'] = len(fileName) * 2 if fileName != '': smb2Create['Buffer'] = fileName.encode('utf-16le') else: smb2Create['Buffer'] = b'\x00' if createContexts is not None: smb2Create['Buffer'] += createContexts smb2Create['CreateContextsOffset'] = len( SMB2Packet()) + SMB2Create.SIZE + smb2Create['NameLength'] smb2Create['CreateContextsLength'] = len(createContexts) else: smb2Create['CreateContextsOffset'] = 0 smb2Create['CreateContextsLength'] = 0 packet['Data'] = smb2Create packetID = self.__smbClient.getSMBServer().sendSMB(packet) ans = self.__smbClient.getSMBServer().recvSMB(packetID) if ans.isValidAnswer(STATUS_SUCCESS): createResponse = SMB2Create_Response(ans['Data']) # The client MUST generate a handle for the Open, and it MUST # return success and the generated handle to the calling application. # In our case, str(FileID) return str(createResponse['FileID'])