Click here to Skip to main content
15,888,802 members
Articles / Web Development / ASP.NET

Exploiting MD5 collisions (in C#)

Rate me:
Please Sign up or sign in to vote.
4.73/5 (39 votes)
20 Sep 20055 min read 357.6K   3.9K   80  
This article shows how the MD5 collisions can be used to tamper software distribution schemas.
using System;
using System.IO;

namespace Md5Extractor
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Extractor
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			if (args.Length == 0)
				Usage();
			ExtractFile(args[0], args[1]);
		}

		private static void ExtractFile (string sfilename, string soutputfile)
		{
			using (BinaryReader reader = new BinaryReader(File.OpenRead (sfilename)))
			{
				byte[] vec = reader.ReadBytes (128);
				int goodSize = reader.ReadInt32 ();
				int evilSize  = reader.ReadInt32 ();
				/// open evil file
				if (vec[123] == 0xab)
				{
					reader.ReadBytes (goodSize);
					byte[] evil = reader.ReadBytes (evilSize);
					using (BinaryWriter writer = new BinaryWriter(File.OpenWrite (soutputfile)))
					{
						writer.Write (evil);
						writer.Close ();	
					}
				}
				else
				{
					byte[] good = reader.ReadBytes (goodSize);
					using (BinaryWriter writer = new BinaryWriter(File.OpenWrite (soutputfile)))
					{
						writer.Write (good);
						writer.Close ();
					}
				}
				Console.WriteLine ("File written on {0}", soutputfile);
			}
		}

		private static void Usage ()
		{
			Console.WriteLine("Usage: md5extract file.bin output_file.exe");
			Environment.Exit (-1);
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Chile Chile
Eduardo Diaz
personal blog

Comments and Discussions