我可以使用PHP从.exe文件获取信息吗?

时间:2014-08-15 08:36:27

标签: php exe

我需要从.EXE文件中读出信息。这可以用PHP吗?

我将.EXE文件上传到服务器后,我想要检索:应用程序名称,版本号和&图标。还有更多信息要收集吗?

是否可以使用PHP执行此操作?

如果没有,还有其他方法可以获得这些信息吗?

1 个答案:

答案 0 :(得分:1)

<?php

// Code snippet to extract the icon from an exe file on a Linux system -- tested on Debian Wheezy

// Install icoutils on your system e.g. sudo apt-get install icoutils
// Web process must have write privileges to /tmp

///** Config **///

$input_file = '/path/to/program.exe';
$dest_file = '/path/to/image.png';
$dest_size = 64; // Will get the best quality icon at or below this size.

///** End Config **///


$temp_dir_base = '/tmp/iconextract/';
$tmp_offset = 0;
while(file_exists($temp_dir_base.$tmp_offset.'/')){ //Ensure working directory is empty
    $tmp_offset += 1;
}

$tmp_dir = $temp_dir_base.$tmp_offset.'/';

mkdir($tmp_dir,0777,true);  //Create a temporary folder to work in
exec('wrestool -x '.str_replace(' ','\\',$input_file).' -o '.$tmp_dir); //Extract ico files
$icon_files = glob($tmp_dir.'*.ico'); //Find all ico files

$max = 0;
$file = 0;
$index = 0;
foreach($icon_files as $file_offset=>$ico){ //loop each match
    exec('icotool -l '.$ico.' --icon',$list); //get each icon inside the file
    foreach($list as $i){ //Loop through each icon
        preg_match_all('/--index=(?P<index>\d+) --width=(?P<width>\d+) --height=(?P<height>\d+)/',$i,$a);
        if($a['width'][0] > $max and $a['width'][0] <= $dest_size){
            $max = $a['width'][0];
            $index = $a['index'][0];
            $file = $file_offset;
        }
    }
}

if($max > 0){ //If we found one, extract it to the destination
    exec('icotool -x '.$icon_files[$file].' -i '.$index.' -o '.str_replace(' ','\\',$dest_file));
}
else{
    exec('rm '.$tmp_dir.' -r'); //Clean out tmp files
    $error = "Could not find a sutiable icon file";
    throw new Exception($error);
}
exec('rm /tmp/iconextract -r'); //Clean out tmp files

?>

来源:https://gist.github.com/mrkmg/4568896

他正在使用名为ioutils的实用程序来获取图标并使用php来调用utillity

而且:

function GetFileVersion($FileName) {

$handle=fopen($FileName,'rb');
if (!$handle) return FALSE;
$Header=fread ($handle,64);
if (substr($Header,0,2)!='MZ') return FALSE;
$PEOffset=unpack("V",substr($Header,60,4));
if ($PEOffset[1]<64) return FALSE;
fseek($handle,$PEOffset[1],SEEK_SET);
$Header=fread ($handle,24);
if (substr($Header,0,2)!='PE') return FALSE;
$Machine=unpack("v",substr($Header,4,2));
if ($Machine[1]!=332) return FALSE;
$NoSections=unpack("v",substr($Header,6,2));
$OptHdrSize=unpack("v",substr($Header,20,2));
fseek($handle,$OptHdrSize[1],SEEK_CUR);
$ResFound=FALSE;
for ($x=0;x<$NoSections[1];$x++) {
    $SecHdr=fread($handle,40);
    if (substr($SecHdr,0,5)=='.rsrc') {         //resource section
        $ResFound=TRUE;
        break;
    }
}
if (!$ResFound) return FALSE;
$InfoVirt=unpack("V",substr($SecHdr,12,4));
$InfoSize=unpack("V",substr($SecHdr,16,4));
$InfoOff=unpack("V",substr($SecHdr,20,4));
fseek($handle,$InfoOff[1],SEEK_SET);
$Info=fread($handle,$InfoSize[1]);
$NumDirs=unpack("v",substr($Info,14,2));
$InfoFound=FALSE;
for ($x=0;$x<$NumDirs[1];$x++) {
    $Type=unpack("V",substr($Info,($x*8)+16,4));
    if($Type[1]==16) {                          //FILEINFO resource
        $InfoFound=TRUE;
        $SubOff=unpack("V",substr($Info,($x*8)+20,4));
        break;
    }
}
if (!$InfoFound) return FALSE;
$SubOff[1]&=0x7fffffff;
$InfoOff=unpack("V",substr($Info,$SubOff[1]+20,4)); //offset of first FILEINFO
$InfoOff[1]&=0x7fffffff;
$InfoOff=unpack("V",substr($Info,$InfoOff[1]+20,4));    //offset to data
$DataOff=unpack("V",substr($Info,$InfoOff[1],4));
$DataSize=unpack("V",substr($Info,$InfoOff[1]+4,4));
$CodePage=unpack("V",substr($Info,$InfoOff[1]+8,4));
$DataOff[1]-=$InfoVirt[1];
$Version=unpack("v4",substr($Info,$DataOff[1]+48,8));
$x=$Version[2];
$Version[2]=$Version[1];
$Version[1]=$x;
$x=$Version[4];
$Version[4]=$Version[3];
$Version[3]=$x;
return $Version;
}

来源:Get Version of exe via PHP