Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
XP - Blocked file
Message
From
18/11/2008 06:15:08
 
 
To
16/11/2008 11:31:40
General information
Forum:
Windows
Category:
Security
Miscellaneous
Thread ID:
01361699
Message ID:
01362501
Views:
7
>>I certainly can't open the stream in .Net as simply as with Notepad !
>>Something like:
StreamReader file = new StreamReader("D:\\Tmp\\Alternate.XML:Zone.Identifier");
gives exception:
>>"The given path's format is not supported."
>>
>>There's a solution here with source code:http://www.codeproject.com/KB/cs/ntfsstreams.aspx
>>Haven't looked at it closely but it's not trivial......
>
>Viv,
>
>I've made a class. The code for the stream enum comes from http://msdn.microsoft.com/en-us/magazine/cc163677.aspx
>
>I can enum the streams. I have not made any attempt at opening/modifying the streams.
>
>For the detection and deletion of streams - I have taken the short road - using api
>
>ps: TabStops are set at 4
>
>
>Class : PlatformFileStreams
>
>// not really needed for this purpose
>public static List<PlatformStreamInfo> GetStreams(string fileName)
>  returns a list of the streams
>
>public static bool HasSecurityStream(string path)
>   tells whether a file has a security stream attached to it
>
>public static bool DeleteSecurityStream(string path)
>   deletes the security stream (restores the file readonly flag when necessary
>
>
>
>Class FileBits
>collection of methods on files
>
>
>
>
>
>// test
>namespace GregoryAdam.Test
>{
>
>	class testx
>	{
>		static void Main(string[] args)
>		{
>			
>			ShowStreams(@"d:\tmp\in\Adam9.exe");
>
>			Console.WriteLine("");
>
>			Console.WriteLine(" {0}", PlatformFileStreams.HasSecurityStream(@"d:\tmp\in\Adam9.exe"));
>			Console.WriteLine(" {0}", PlatformFileStreams.DeleteSecurityStream(@"d:\tmp\in\Adam9.exe"));
>			Console.WriteLine(" {0}", PlatformFileStreams.HasSecurityStream(@"d:\tmp\in\Adam9.exe"));
>
>			Console.ReadLine();
>		}
>		static void ShowStreams(string fileName)
>		{
>			Console.WriteLine(" >>> {0}", fileName);
>			var si = PlatformFileStreams.GetStreams(fileName);
>			foreach (PlatformStreamInfo xx in si)
>			{
>				Console.WriteLine(" {0} {1} {2}", xx.Name == null ? "(no name)" : xx.Name , xx.Type, xx.Size);
>
>			}
>
>		}
>
>	}
>}
>
>
>
>using System;
>using System.Collections.Generic;
>using System.Linq;
>using System.Text;
>using System.IO;
>
>namespace GregoryAdam.Base.IO
>{
>	public static class FileBits
>	{
>		//----------------------------------------------------------------------
>		public static bool ReadOnly(string path)
>		{
>			if (!File.Exists(path))
>				return false;
>
>			return ( (File.GetAttributes(path) & FileAttributes.ReadOnly) != 0);
>		}
>		//----------------------------------------------------------------------
>		public static bool Hidden(string path)
>		{
>			if (!File.Exists(path))
>				return false;
>
>			return ((File.GetAttributes(path) & FileAttributes.Hidden) != 0);
>		}
>		//----------------------------------------------------------------------
>		//----------------------------------------------------------------------
>		public static bool ResetReadOnly(string path)
>		{
>			if (!FileBits.ReadOnly(path))
>				return false;
>
>			try
>			{
>				File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
>				return true;
>			}
>			catch
>			{
>				return false;
>			}
>		}
>		//----------------------------------------------------------------------
>		public static bool SetReadOnly(string path)
>		{
>			if (FileBits.ReadOnly(path))
>				return false;
>
>			try
>			{
>				File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
>				return true;
>			}
>			catch
>			{
>				return false;
>			}
>		}
>		//----------------------------------------------------------------------
>	
>	}
>}
>
>
>
>// a big ugly program
>using System;
>using System.Collections.Generic;
>using System.Linq;
>using System.Text;
>using System.Runtime.InteropServices;
>using System.Security.Permissions;
>using Microsoft.Win32.SafeHandles;
>using System.Runtime.ConstrainedExecution;
>using System.IO;
>
>namespace GregoryAdam.Base.IO
>{
>
>	[StructLayout(LayoutKind.Sequential, Pack = 1)]
>	public struct Win32StreamID
>	{
>		public PlatformStreamType dwStreamId;
>		public int dwStreamAttributes;
>		public long Size;
>		public int dwStreamNameSize;
>		// WCHAR cStreamName[1]; 
>	}
>
>
>	public enum PlatformStreamType
>	{
>		Data = 1,
>		ExternalData = 2,
>		SecurityData = 3,
>		AlternateData = 4,
>		Link = 5,
>		PropertyData = 6,
>		ObjectID = 7,
>		ReparseData = 8,
>		SparseDock = 9
>	}
>
>	public struct PlatformStreamInfo
>	{
>		public PlatformStreamInfo(string name, PlatformStreamType type, long size)
>		{
>			Name = name;
>			Type = type;
>			Size = size;
>		}
>		public readonly string Name;
>		public readonly PlatformStreamType Type;
>		public readonly long Size;
>	}
>
>	public static class PlatformFileStreams
>	{
>		public const string PlatformStreamSecurityStreamName = @":Zone.Identifier:$DATA";
>
>		[DllImport("kernel32.dll")]
>		[return: MarshalAs(UnmanagedType.Bool)]
>		private static extern bool BackupRead(
>				SafeFileHandle hFile, 
>				IntPtr lpBuffer,
>				uint nNumberOfBytesToRead, 
>				out uint lpNumberOfBytesRead,
>				[MarshalAs(UnmanagedType.Bool)] bool bAbort,
>				[MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity,
>				ref IntPtr lpContext
>			);
>
>		[DllImport("kernel32.dll")]
>		[return: MarshalAs(UnmanagedType.Bool)]
>		private static extern bool BackupSeek( 
>				SafeFileHandle hFile,
>				uint dwLowBytesToSeek, 
>				uint dwHighBytesToSeek,
>				out uint lpdwLowByteSeeked, 
>				out uint lpdwHighByteSeeked,
>				ref IntPtr lpContext
>			);
>
>		/// <summary>
>		/// returns a List of PlatformStreamInfo for a fileName, null if any error
>		/// </summary>
>		/// <param name="fileName"></param>
>		/// <returns></returns>
>		public static List<PlatformStreamInfo> GetStreams(string fileName)
>		{
>			if (fileName == null)
>				return null;
>
>			FileInfo fileInfo = null;
>
>			try
>			{
>				fileInfo = new FileInfo(fileName);
>			}
>			catch
>			{
>				return  null ;
>			}
>
>			var streamList = new List<PlatformStreamInfo>();
>			const int bufferSize = 4096;
>
>			IntPtr context = IntPtr.Zero;
>			IntPtr buffer = Marshal.AllocHGlobal(bufferSize);
>			uint numRead;
>			Win32StreamID streamID;
>			string name;
>
>			using (FileStream fs = fileInfo.OpenRead())
>			{
>				try
>				{
>
>					while (true)
>					{
>						if (!BackupRead(fs.SafeFileHandle,
>											buffer,
>											(uint)Marshal.SizeOf(typeof(Win32StreamID)), 
>											out numRead,
>											false,
>											true,
>											ref context
>											)
>							)
>							return null;
>
>						if (numRead <= 0)
>							break;
>
>
>						streamID = (Win32StreamID) Marshal.PtrToStructure(buffer, typeof(Win32StreamID));
>						name = null;
>						if (streamID.dwStreamNameSize > 0)
>						{
>							if (!BackupRead(fs.SafeFileHandle,
>												buffer,
>												(uint)Math.Min(bufferSize, streamID.dwStreamNameSize),
>												out numRead,
>												false,
>												true,
>												ref context
>											)
>								)
>								return null;
>
>							name = Marshal.PtrToStringUni(buffer, (int)numRead / 2);
>						}
>						
>						
>
>						streamList.Add(new PlatformStreamInfo(name, streamID.dwStreamId, streamID.Size));
>
>						if (streamID.Size > 0)
>						{
>							uint lo, hi;
>							BackupSeek(	fs.SafeFileHandle, 
>										uint.MaxValue,
>										int.MaxValue, 
>										out lo, 
>										out hi, 
>										ref context
>										);
>						}
>					}
>				}
>				finally
>				{
>					Marshal.FreeHGlobal(buffer);
>
>					BackupRead(	fs.SafeFileHandle, 
>								IntPtr.Zero, 
>								0, 
>								out numRead,
>								true, 
>								false, 
>								ref context
>							);
>				}
>
>			} //using
>
>
>			return streamList;
>	
>		}
>		//----------------------------------------------------------------------
>		[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
>		private static extern int GetFileAttributes(string lpFileName);
>
>		public static bool HasSecurityStream(string path)
>		{
>			return GetFileAttributes(path + PlatformStreamSecurityStreamName) != -1;
>			
>		}
>		//----------------------------------------------------------------------
>		[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
>		private static extern bool DeleteFile(string lpFileName);
>
>		public static bool DeleteSecurityStream(string path)
>		{
>
>			if (!PlatformFileStreams.HasSecurityStream(path))
>				return false;
>
>			if( !FileBits.ReadOnly(path) )
>				return DeleteFile(path + PlatformStreamSecurityStreamName) ;
>
>			if (!FileBits.ResetReadOnly(path))
>				return false;
>
>			if (!DeleteFile(path + PlatformStreamSecurityStreamName))
>				return false;
>
>			if (!FileBits.SetReadOnly(path))
>				return false;
>
>			return true;
>		}
>	}
>}
>
Hi,

Looks good (but I haven't had a chance to try it). I must say I'm a bit ambivalent about the possibilities that alternate streams open up. On the one hand I love the idea of using it to do neat things. On the other I don't think I'd want someone else using it on my files.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform