10 Oct 2010 @ 4:02 PM 

Few days earlier one of my friend was bragging about OOPS concepts and how it protects private variables and sensitive data from outside world. I tried to explain that once it is being loaded to memory, we can access and change whatever you want with a suitable debugger. As per the saying “people won’t believe until they see”, I had to write a little bit of code to show that it is fairly easy to use reflection and there is no need for good old WinDbg for this simple thing :).

class MySecretClass
{
	private string mySecretinfo;
	public MySecretClass()
	{
		mySecretinfo = "dead-c0de";
	}

	public string GetMySecretInfo()
	{
		return mySecretinfo;
	}
}

private void btnNormal_Click(object sender, EventArgs e)
{
	MySecretClass mySecretClass = new MySecretClass();
	MessageBox.Show(mySecretClass.GetMySecretInfo());
}

private void btnReflection_Click(object sender, EventArgs e)
{
	MySecretClass mySecretClass = new MySecretClass();

	FieldInfo fi = typeof(MySecretClass).GetField("mySecretinfo", BindingFlags.NonPublic | BindingFlags.Instance);
	fi.SetValue(mySecretClass, "You are dead!");
	MessageBox.Show(mySecretClass.GetMySecretInfo());
}

Posted By: Dan
Last Edit: 10 Oct 2010 @ 04:14 PM

EmailPermalink
Tags
Categories: C#


 

Responses to this post » (One Total)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


 Last 50 Posts
Change Theme...
  • Users » 1
  • Posts/Pages » 19
  • Comments » 41
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.