ASP.NET – Use ASHX handler to protect your email from spam

What happens to your email after a week of publishing into internet, I am sure you will get some of spam mails in your inbox. You may be curious how does the spammer knows your email because you just post it in a website and give it your friends, for example my email at my about page http://hintdesk.com/about/ ? The answer lies at email bot. This bot will crawl through out a web site and use its technique, for example regular expression, to detect only email addresses in content and insert these emails into database for spamming later.
So how can we solve this problem? I would like to publish my email for contact over internet but I want to keep it safe apart from spammers. Converting email address into image to avoid spammer must be an intelligent idea which is already used in many web applications. In the example below I would like to show a simple way to generate image from text with ASP.NET and show it in our website.

Continue reading ASP.NET – Use ASHX handler to protect your email from spam

AJAX – Build a RSS reader module with javascript and ASP/PHP

WARNING: THE CODE OF PROXY.PHP BELOW IS VULNERABLE. A HACKER CAN VIEW YOUR LOCALHOST FILES AND MORE (I AM ON RESEARCHING).PLEASE DO NOT USE IT UNTIL I FIX IT.
Today I read an interesting post about creating a RSS reader module with Javascript and ASP at following link http://dev.meotom.net/2010/3/5/Tao-box-doc-tin-RSS-don-gian-voi-Javascript-32C.htm . I test myself this module and its source code to see if it works. And below is my copy-and-paste product. The module works perfectly and is pretty beautiful.

Continue reading AJAX – Build a RSS reader module with javascript and ASP/PHP

C# – Get all files being accessed by a process in 64 bits

Today I would like to end my series working with some system APIs which should run on 64 bits operating system. This last post in this series will discuss how we get all files being accessed by a given process. For example, if I would like to find out which files Yahoo Messenger are accessing, I can use this code snippet as following

static void Main(string[] args)
{
	string strTemp = "";
	m_pTarget = Process.GetProcessesByName("YahooMessenger")[0];
	m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.DupHandle, false, m_pTarget.Id);
	List<Win32API.SYSTEM_HANDLE_INFORMATION> lstHandles = CustomAPI.GetHandles(m_pTarget);
	for (int nIndex = 0; nIndex < lstHandles.Count; nIndex++)
	{
		strTemp = GetFileDetails(lstHandles[nIndex]).Name;
		if (strTemp != "") Console.WriteLine(strTemp);
	}
	Console.ReadLine();
}

You can see that I used the code snippet from the previous post http://hintdesk.com/c-get-all-handles-of-a-given-process-in-64-bits/ to get all handles of given process. After getting all handles I will see if that handle is relevant to file-handle. If it is a file-handle, I will give the path of that file back. To get these information, I follow 3 steps below.

1. Get basic information of handle

ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic));
Win32API.NtQueryObject(ipHandle, (int) Win32API.ObjectInformationClass.ObjectBasicInformation, ipBasic, Marshal.SizeOf(objBasic), ref nLength);
objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType());
Marshal.FreeHGlobal(ipBasic);

2. Get object type of handle

ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength);
nLength = objBasic.TypeInformationLength;
while ((uint)(nReturn = Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH)
{
	Marshal.FreeHGlobal(ipObjectType);
	ipObjectType = Marshal.AllocHGlobal(nLength);
}

3. Get name of object type and check if it is a file-handle

objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType());
if (Is64Bits())
{
	ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32);             
}
else
{
	ipTemp = objObjectType.Name.Buffer;          
}

strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1);
Marshal.FreeHGlobal(ipObjectType);
if (strObjectTypeName != "File") return fd;


4. Get file name and path to it

nLength = objBasic.NameInformationLength;
ipObjectName = Marshal.AllocHGlobal(nLength);
while ((uint)(nReturn = Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation, ipObjectName, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH)
{
	Marshal.FreeHGlobal(ipObjectName);
	ipObjectName = Marshal.AllocHGlobal(nLength);
}
objObjectName = (Win32API.OBJECT_NAME_INFORMATION) Marshal.PtrToStructure(ipObjectName, objObjectName.GetType());

if (Is64Bits())
{
	ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32);               
}
else
{
	ipTemp = objObjectName.Name.Buffer;           
}

byte[] baTemp = new byte[nLength];
Win32API.CopyMemory(baTemp, ipTemp, (uint)nLength);

if (Is64Bits())
{
	strObjectName = Marshal.PtrToStringUni(new IntPtr(ipTemp.ToInt64()));
}
else 
{
	strObjectName = Marshal.PtrToStringUni(new IntPtr(ipTemp.ToInt32()));
}

Marshal.FreeHGlobal(ipObjectName);

fd.Name = GetRegularFileNameFromDevice(strObjectName);
return fd;

I think it’s pretty easy to understand the code if you’ve read 2 posts before.
http://hintdesk.com/c-get-all-handles-of-a-given-process-in-64-bits/
http://hintdesk.com/c-get-object-type-number-in-windows-64-bits/
The complete source code you can download here “Get file details from handle

UPDATE 09.08.2010
If you want to know if the function of system libraries exists or not, you can use CFF Explorer to open the library and look at the Export Section to find out the function you want. For example, in the image below you’ll see the RtlCopyMemory is exported from kernel32.dll and therefore can be called with DllImport.

CFF Explorer

C# – Get all handles of a given process in 64 bits

I am trying to convert some code snippet in C++ to C# which should run on 64 bit operating system. I would like to share it to you so that if you need you can use it immediately without losing time to implement yourself. Today I would like to introduce a code snippet which return all handles of a given process and can run on 64 bits operating system. The core API of this code snippet is NtQuerySystemInformation which retrieves the specified system information. Using this code snippet I can get list of handles like following

Console.WriteLine(GetHandles(Process.GetProcessesByName("YahooMessenger")[0]).Count);

I will use NtQuerySystemInformation in loop to query handle information. NtQuerySystemInformation does not give the correct buffer size back so I guess by using a while-loop to check if it returns status of STATUS_INFO_LENGTH_MISMATCH. If not, then I have a correct buffer size and allocate my pointer with received length.

 while ((nStatus = NtQuerySystemInformation(CNST_SYSTEM_HANDLE_INFORMATION, ipHandlePointer, nHandleInfoSize, ref nLength)) == STATUS_INFO_LENGTH_MISMATCH)
{
	nHandleInfoSize = nLength;
	Marshal.FreeHGlobal(ipHandlePointer);
	ipHandlePointer = Marshal.AllocHGlobal(nLength);
}

The received pointer will point to memory section which has structure as following

Reading data from the pointer, I can get count of handles which are running on computer

long lHandleCount = 0;
if (Is64Bits())
{
	lHandleCount = Marshal.ReadInt64(ipHandlePointer);
	ipHandle = new IntPtr(ipHandlePointer.ToInt64() + 8);
}
else
{
	lHandleCount = Marshal.ReadInt32(ipHandlePointer);
	ipHandle = new IntPtr(ipHandlePointer.ToInt32() + 4);
}

Looping through this handles pool using the pointer and count of handles we can figure out which handle is created by given process

SYSTEM_HANDLE_INFORMATION shHandle;
List<SYSTEM_HANDLE_INFORMATION> lstHandles = new List<SYSTEM_HANDLE_INFORMATION>();

for (long lIndex = 0; lIndex < lHandleCount; lIndex++)
{
	shHandle = new SYSTEM_HANDLE_INFORMATION();
	if (Is64Bits())
	{
		shHandle = (SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
		ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle) + 8);
	}
	else
	{
		ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle));
		shHandle = (SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
	}
	if (shHandle.ProcessID != process.Id) continue;
	lstHandles.Add(shHandle);
}
return lstHandles;

You can see that the size of SYSTEM_HANDLE_INFORMATION in 64 bits OS is 8 bytes larger than 32 bits OS. Therefore I must add more 8 bytes when jumping to another handle struct. I do not know why this struct is larger in 64 bits OS. Maybe there is an undocumented field of this struct in 64 bits OS or the GrantedAccess filed has size of Int64 in 64 bits OS (not as Int32 in 32 bits OS).
The complete source code you can download here “Get all handles of given process“.

C# – Get object type number in Windows 64 bits

I am trying to port some C++ code into C# which then should run on a 64 bits operating system. It’s not easy for me to do that because all of functions which I need, are relevant to system API with little documentations and examples because only system programmers works with these API. The fact that it may be simple when implementing on OS 32 bits. However on OS 64 bits there are difference at the size of pointer and therefore I must adjust code so that it can run smoothly. One of the code snippets which I want to port is getting object type number of a defined type through API NtQueryObject so that I can get type number of a object type like following

int nFileObjectTypeNumber = GetObjectTypeNumber("File");

NtQueryObject retrieves various kinds of object information. Therefore if I want to get number of a defined object, I should query information of all types and makes a loop to find out the object number of that type.

1. Query all types information

To query information of all types, I must use NtQueryObject with option of ObjectAllTypesInformation. Because NTQueryObject does not give back the correct buffer size so I guess by running loop until we do not have result of STATUS_INFO_LENGTH_MISMATCH any more

public enum ObjectInformationClass : int
{
	ObjectBasicInformation = 0,
	ObjectNameInformation = 1,
	ObjectTypeInformation = 2,
	ObjectAllTypesInformation = 3,
	ObjectHandleInformation = 4
}
public const uint STATUS_INFO_LENGTH_MISMATCH = 0xC0000004;

IntPtr ipBufferObjectType = IntPtr.Zero;
int nLength = 0;

while ((uint)NtQueryObject(0, (int) ObjectInformationClass.ObjectAllTypesInformation, ipBufferObjectType, nLength, ref nLength) == STATUS_INFO_LENGTH_MISMATCH)
{
        Marshal.FreeHGlobal(ipBufferObjectType);
	ipBufferObjectType = Marshal.AllocHGlobal(nLength);
}

After looping to get information of all types, the pointer ipBufferObjectType will point to a memory section has a following structure.

As you can see in image above, the pointer points to first field which indicates how many types are there in the operation system. To get quantity of these types we can read value at that pointer. However the pointer size of 64 bits OS is 8 bytes, therefore the size of this field is 8 bytes too. So we must read completely 8 bytes and jump to the first object type.

if (Is64Bits())
{
	lTypeCount = Marshal.ReadInt64(ipBufferObjectType, 0);
	ipTypeInfo = new IntPtr(ipBufferObjectType.ToInt64() + 8);
}
else
{
	lTypeCount = Marshal.ReadInt32(ipBufferObjectType, 0);
	ipTypeInfo = new IntPtr(ipBufferObjectType.ToInt32() + 4);
}

2. Loop and find object number of type
After define how many types are there, we make a for-loop, get name of each object type and compare it to our desired one. If we found it then give result back.

for (int nIndex = 0; nIndex < lTypeCount; nIndex++)
{
	IntPtr ipTemp = IntPtr.Zero;
	Int64 nSize = 0;
	otiTemp = (OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipTypeInfo, otiTemp.GetType());
	if (Is64Bits())
	{
		ipTemp = new IntPtr(Convert.ToInt64(otiTemp.Name.Buffer.ToString(), 10) >> 32);
		nSize =  ipTemp.ToInt64() - ipTypeInfo.ToInt64() ;
	}
	else
	{
		ipTemp = otiTemp.Name.Buffer;
		nSize = ipTemp.ToInt32() - ipTypeInfo.ToInt32();
	}

	strType = Marshal.PtrToStringUni(ipTemp, otiTemp.Name.Length >> 1);
	if (p == strType)
	{
		return nIndex + 1;
	}
	if (Is64Bits())
	{
		ipTypeInfo = new IntPtr(ipTypeInfo.ToInt64() + nSize + otiTemp.Name.MaximumLength);
		while (ipTypeInfo.ToInt64() % 8 != 0)
		{
			ipTypeInfo = new IntPtr(ipTypeInfo.ToInt64() + 1);
		}
	}
	else
	{
		ipTypeInfo = new IntPtr(ipTypeInfo.ToInt32() + nSize + otiTemp.Name.MaximumLength);
		while (ipTypeInfo.ToInt64() % 4 != 0)
		{
			ipTypeInfo = new IntPtr(ipTypeInfo.ToInt64() + 1);
		}
	}
}

In the loop, you can see that I calculate the size of OBJECT_TYPE_INFORMATION too. This task can be done with Marshal.SizeOf(otiTemp) but I found out that it lacks 4 bytes when using on 64 bits system therefore I decide to calculate it myself by substracting pointer of section name by pointer to object type. At the end, I must parse the pointer so that it jumps correctly to the next object type section.
The complete source code you can download here “Get Object Type Number