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

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

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 the 64-bit operating system. I would like to share it with 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 returns 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

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

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