473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

[Win32] Regexing a pattern from a binary file?

Hi,

I'm a PHP newbie, and am stuck as to why I can't find a pattern in a
Win32 binary file.

I'm actually trying to extract the FileVersion information myself
since PHP under Unix doesn't seem to offer support for the PE file
format:

-------------
<?php
$file = "C:\\temp\\test.exe";
$fp = fopen($file, "rb");
$contents = fread($fp, filesize($file));
fclose($fp);

//Unicode?
//If (eregi('F.i.l.e.V.e.r.s.i.o.n', $contents)) {
//if (eregi('FileVersion', $contents)) {

if (eregi("This program cannot be run in DOS mode",
$contents)) {
print "OK!";
} else {
print "Outta luck...";
}
exit;

?>
-------------

Any idea?

Thank you for any tip
Fred.
Jul 17 '05 #1
2 1828

"Fred the man" <fr**@acme.com> wrote in message
news:le********************************@4ax.com...
Hi,

I'm a PHP newbie, and am stuck as to why I can't find a pattern in a
Win32 binary file.

I'm actually trying to extract the FileVersion information myself
since PHP under Unix doesn't seem to offer support for the PE file
format:

-------------
<?php
$file = "C:\\temp\\test.exe";
$fp = fopen($file, "rb");
$contents = fread($fp, filesize($file));
fclose($fp);

//Unicode?
//If (eregi('F.i.l.e.V.e.r.s.i.o.n', $contents)) {
//if (eregi('FileVersion', $contents)) {

if (eregi("This program cannot be run in DOS mode",
$contents)) {
print "OK!";
} else {
print "Outta luck...";
}
exit;


Hmmm, maybe the Posix RegExp library isn't binary safe? Use the Perl 5 set
of functions instead:

$fv = 'F\0i\0l\0e\0V\0e\0r\0s\0i\0o\0n\0';
$tz = '\0\0\0\0';

if(preg_match("/$fv$tz(.*?)$tz/", $contents, $matches)) {
echo str_replace("\0", "", $matches[1]);
}
Jul 17 '05 #2
On Thu, 17 Jun 2004 19:44:31 -0400, "Chung Leong"
<ch***********@hotmail.com> wrote:
Hmmm, maybe the Posix RegExp library isn't binary safe? Use the Perl 5 set
of functions instead:


Thx Chung :-) Indeed, it seems erg() doesn't like non-printable
characters.

For those interested in searching a pattern that may contain ASCII
0's, here's the code:

-----------------
$file = "c:\\temp\\test.exe";
$fp = fopen($file, "rb");
$contents = fread($fp, filesize($file));
fclose($fp);

$fv = 'F\0i\0l\0e\0V\0e\0r\0s\0i\0o\0n\0\0\0\0\0';
$tz = "\0\0";
if(preg_match("/$fv(.*?)$tz/", $contents, $matches)) {
//Looking for eg. 1.000 with 0's in between
if (preg_match("/(\\d)\\0\.\\0(\\d+)\\0(\\d+)\\0
(\\d+)\\0/",$matches[1],$version)) {
print "Version1=$version[1]<p>";
print "Version2=$version[2]<p>";
print "Version3=$version[3]<p>";
print "Version4=$version[4]<p>";
} else {
print "Sorry";
} else {
print "BAD";
}
---------------------

If you wish to replace ASCII 0's with eg. #, here's the code:
---------------
//\x = by default, means \x0, ie. match any ASCII 0 :-)
$output=preg_replace('/\x00/','#',$matches[1]);
---------------

I haven't found how to replace a character with a TAB (\t, or \\t, or
\\\\t don't work.)

Thx again
Fred.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
0
by: Nigel Leach | last post by:
Hi, I have a script that runs perfectly natively. However, when compiled (using perlapp from ActiveState's Dev Kit) it fails to run from a DOS command window, and fails with … Can't locate...
1
by: kittykat | last post by:
Hi, I want to read the input variables from a user, and then compare this with data in the text file. If the input variables and the data in the text file match, then i would like to create a...
8
by: Charles Law | last post by:
I am implementing the command pattern in VB.NET, where the commands have been serialised. That is, I have several classes that all inherit from my base Command class, that implements ICommand...
0
by: Mythran | last post by:
I wrote some code that is supposed to enumerate through the specified file's win32 resources and return a string-array of all icon names. When it runs, it returns a string-array with a bunch of...
13
by: dumpingrounds | last post by:
Hi, I must be going nutty, but I can't seem to find anywhere where I can get just the binaries for, say, 2.3.5 for win32. I've googled high and low and all I come up with is installer (MSI/EXE)...
3
by: somuchh8 | last post by:
Hi, I'm having a lot of trouble with the Win32::Spawn module in perl. Here is my situation, I have a Win32::Spawn call which looks like this: my $success = undef; my $cmdline =...
16
by: vizzz | last post by:
Hi there, i need to find an hex pattern like 0x650A1010 in a binary file. i can make a small algorithm that fetch all the file for the match, but this file is huge, and i'm scared about...
1
by: kenone | last post by:
I have loaded a large binary file into memory and now I want to search for 10101. I was using file.get to return the next hex number and see if it was equal to 0x15. This is not correct as part of my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.