[utils] Strings in binaries - strings.exe
A while ago, I was searching through IIS DLL's looking for a specific error message. Like most things, there's my way and the easy way.
My way: a tiny SnippetCompiler special that creates a notepad friendly copy of any binary file. Posting the code in case it saves someone some time on a similar task.
Download [here].
Source [show]
using System;
using System.Collections;
using System.IO;
public class StripNonAscii
{
public static void Main()
{
Console.WriteLine("This strips non-ASCII characters from a binary file so you can search for text strings.");
Console.WriteLine("");
Console.WriteLine("Enter the full path and filename to convert:");
string sourceFile = Console.ReadLine();
while(sourceFile.Length>0)
{
FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
FileStream fsOut = new FileStream(sourceFile + ".txt", FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fsOut);
// Read data from Test.data.
for (int i = 0; i < r.BaseStream.Length; i++)
{
byte val = r.ReadByte();
if(val==10 || val ==13 || (val>31 && val<127))
{
w.Write(val);
}
else if(val>0)
{
w.Write((byte)32);
}
}
r.Close();
w.Close();
Console.WriteLine("File written to: " + sourceFile + ".txt");
Console.WriteLine("Enter the full path and filename to convert, or ENTER to exit:");
sourceFile = Console.ReadLine();
}
}
}
using System.Collections;
using System.IO;
public class StripNonAscii
{
public static void Main()
{
Console.WriteLine("This strips non-ASCII characters from a binary file so you can search for text strings.");
Console.WriteLine("");
Console.WriteLine("Enter the full path and filename to convert:");
string sourceFile = Console.ReadLine();
while(sourceFile.Length>0)
{
FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
FileStream fsOut = new FileStream(sourceFile + ".txt", FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fsOut);
// Read data from Test.data.
for (int i = 0; i < r.BaseStream.Length; i++)
{
byte val = r.ReadByte();
if(val==10 || val ==13 || (val>31 && val<127))
{
w.Write(val);
}
else if(val>0)
{
w.Write((byte)32);
}
}
r.Close();
w.Close();
Console.WriteLine("File written to: " + sourceFile + ".txt");
Console.WriteLine("Enter the full path and filename to convert, or ENTER to exit:");
sourceFile = Console.ReadLine();
}
}
}
The right way was probably to use strings.exe from Sysinternals, which can rip through a whole folder detecting string (ANSI and Unicode) of a minimum character length.