public static bool WriteStringToFile(string filePath, string str, File_Attributes flags = 0) { IntPtr hStream = CreateFileFromApp(filePath, GENERIC_WRITE, 0, IntPtr.Zero, CREATE_ALWAYS, (uint)(File_Attributes.BackupSemantics | flags), IntPtr.Zero); if (hStream.ToInt64() == -1) { return(false); } byte[] buff = Encoding.UTF8.GetBytes(str); int dwBytesWritten; unsafe { fixed(byte *pBuff = buff) { WriteFile(hStream, pBuff, buff.Length, &dwBytesWritten, IntPtr.Zero); } } CloseHandle(hStream); return(true); }
public Smb2CreateResponsePacket CreateCreateResponse( Smb2Endpoint endpoint, ulong messageId, OplockLevel_Values oplockLevel, CreateAction_Values createAction, _FILETIME creationTime, _FILETIME lastAccessTime, _FILETIME lastWriteTime, _FILETIME changeTime, ulong allocationSize, ulong endofFile, File_Attributes fileAttributes, FILEID fileId, params CREATE_CONTEXT_Values[] createContexts ) { Smb2CreateResponsePacket packet = new Smb2CreateResponsePacket(); SetHeader(packet, endpoint, messageId); packet.PayLoad.StructureSize = CREATE_Response_StructureSize_Values.V1; packet.PayLoad.OplockLevel = oplockLevel; packet.PayLoad.Reserved = 0; packet.PayLoad.CreateAction = createAction; packet.PayLoad.CreationTime = creationTime; packet.PayLoad.LastAccessTime = lastAccessTime; packet.PayLoad.LastWriteTime = lastWriteTime; packet.PayLoad.ChangeTime = changeTime; packet.PayLoad.AllocationSize = allocationSize; packet.PayLoad.EndofFile = endofFile; packet.PayLoad.FileAttributes = fileAttributes; packet.PayLoad.Reserved2 = 0; packet.PayLoad.FileId = fileId; if (createContexts == null) { packet.PayLoad.CreateContextsOffset = 0; packet.PayLoad.CreateContextsLength = 0; packet.PayLoad.Buffer = new byte[0]; } else { packet.PayLoad.CreateContextsOffset = Smb2Consts.CreateContextOffsetInCreateResponse; using (MemoryStream ms = new MemoryStream()) { for (int i = 0; i < createContexts.Length; i++) { byte[] createContext = TypeMarshal.ToBytes(createContexts[i]); if (i != (createContexts.Length - 1)) { int alignedLen = Smb2Utility.AlignBy8Bytes(createContext.Length); byte[] nextValue = BitConverter.GetBytes(alignedLen); Array.Copy(nextValue, createContext, nextValue.Length); ms.Write(createContext, 0, createContext.Length); //write the padding 0; for (int j = 0; j < (alignedLen - createContext.Length); j++) { ms.WriteByte(0); } } else { ms.Write(createContext, 0, createContext.Length); } } packet.PayLoad.Buffer = ms.ToArray(); packet.PayLoad.CreateContextsLength = (uint)packet.PayLoad.Buffer.Length; } } packet.Sign(); return packet; }
public Smb2CreateResponsePacket CreateCreateResponseAsync( Smb2Endpoint endpoint, ulong asyncId, ulong messageId, OplockLevel_Values oplockLevel, CreateAction_Values createAction, _FILETIME creationTime, _FILETIME lastAccessTime, _FILETIME lastWriteTime, _FILETIME changeTime, ulong allocationSize, ulong endofFile, File_Attributes fileAttributes, FILEID fileId, params CREATE_CONTEXT_Values[] contexts ) { Smb2CreateResponsePacket packet = CreateCreateResponse(endpoint, messageId, oplockLevel, createAction, creationTime, lastAccessTime, lastWriteTime, changeTime, allocationSize, endofFile, fileAttributes, fileId, contexts); ModifyAsyncHeader(packet, endpoint, asyncId); packet.Sign(); return packet; }
public Smb2CloseResponsePacket CreateCloseResponse( Smb2Endpoint endpoint, ulong messageId, CLOSE_Response_Flags_Values flags, _FILETIME creationTime, _FILETIME lastAccessTime, _FILETIME lastWriteTime, _FILETIME changeTime, ulong allocationSize, ulong endofFile, File_Attributes fileAttributes ) { Smb2CloseResponsePacket packet = new Smb2CloseResponsePacket(); SetHeader(packet, endpoint, messageId); packet.PayLoad.AllocationSize = allocationSize; packet.PayLoad.ChangeTime = changeTime; packet.PayLoad.CreationTime = creationTime; packet.PayLoad.EndofFile = endofFile; packet.PayLoad.FileAttributes = fileAttributes; if (fileAttributes == File_Attributes.NONE) { packet.PayLoad.Flags = CLOSE_Response_Flags_Values.NONE; } else { packet.PayLoad.Flags = CLOSE_Response_Flags_Values.V1; } packet.PayLoad.LastAccessTime = lastAccessTime; packet.PayLoad.LastWriteTime = lastWriteTime; packet.PayLoad.Reserved = CLOSE_Response_Reserved_Values.V1; packet.PayLoad.StructureSize = CLOSE_Response_StructureSize_Values.V1; packet.Sign(); return packet; }