[Mono-list] RESULT: 0 from mono, not from mint.

Peter M. Lemmen peter@lemmen.org
Thu, 28 Mar 2002 16:30:21 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_001C_01C1D675.DAB112C0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hi again,

I've written two little scripts and a small c program to wrap around the whole
binary execution thing.

The first script, binfmt_misc_mono.sh, loads the binfmt_misc module and
registers the exewrapper script as a handler for binaries with the correct
magic number. I suggest calling this from a boot-up script.

The second script, exewrapper.sh, uses the c program, ismonobin, to find out
wether the given binary is a mono binary and should be executed with the mono
JIT. This should be easily extendable for DosEmu and Wine. (But I am totally
unfamiliar with those projects.)

The c program, ismonobin, checks the full first 64 bytes of the binary against
the fingerprint I gathered yesterday.

The default locations for the scripts right now is /usr/bin/local. I hope this
is ok. After placing them in there and running binfmt_misc_mono you should be
able to try the following for yourself:

peter@storm:~$ ./hello_world
Hello World!
RESULT: 0
peter@storm:~$ ./iexplore.exe
File "./iexplore.exe" is NOT a Mono Binary.
peter@storm:~$

I'm not quite sure where to submit this, so I've just attached them to this
email. Please let me know if anything goes wrong or should work differently.
(I'm a VB programmer by trade, so this all included a lot of guesswork on my
part...)

Regards,

Peter.


------=_NextPart_000_001C_01C1D675.DAB112C0
Content-Type: application/octet-stream;
	name="ismonobin.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="ismonobin.c"

//_______________________________________________________________________=
______
//
// ismonobin v0.1, written by Peter M. Lemmen, 2002
//_______________________________________________________________________=
______
//
// Includes.
//
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//_______________________________________________________________________=
______
//
// The 'magic' bytes starting the Mono binaries.
//
unsigned char aucMonoMagic[] =3D {
  0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00
, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00
, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00
};
//
#define CMP_SIZE 64
//_______________________________________________________________________=
______
//
int display_help() {
  printf( "ismonobin v0.1, written by Peter M. Lemmen, 2002\n" );
  printf( "Usage: ismonobin <executable>\n" );
  printf( " ismonobin tries to determine wether the passed executable is =
a Mono\n" );
  printf( " compiled .NET binary or not.\n" );
  return 0;
}
//_______________________________________________________________________=
______
//
// Program Entrypoint.
//
int main (int intArgumentCount, char *acrArgumentValues []){
  int intFileDescriptor, intBytesRead;
  unsigned char aucBuffer[CMP_SIZE];

  // Check if we're being passed anything other then a single parameter.
	if ((intArgumentCount < 2)
  ||  (intArgumentCount > 2)) {
    return display_help();
  // If not, check if it isn't -h.
  } else if (strcmp( "-h", acrArgumentValues[1] ) =3D=3D 0) {
    return display_help();
  }

  // Try to open the file given on the command line.
  intFileDescriptor =3D open( acrArgumentValues[1], O_RDONLY );

  // Check if successful.
  if (intFileDescriptor < 1) {
    fprintf( stderr, "Unabled to open file %s.\n", acrArgumentValues[1] =
);
    return 0;
  }

  // Read the first CMP_SIZE bytes.
  intBytesRead =3D read( intFileDescriptor, &aucBuffer, (size_t)64 );

  // Check if the read was succesful.
  if (intBytesRead < CMP_SIZE) {
    fprintf( stderr, "Unabled to read first %d bytes from file %s.\n", =
CMP_SIZE, acrArgumentValues[1] );
    return 0;
  }

  // Ok, compare the first CMP_SIZE bytes.
  if (bcmp( aucBuffer, aucMonoMagic, (size_t)CMP_SIZE ) !=3D 0) {
    return 0;
  }

  // Looks like a Mono binary.
  return 1;
}
//_______________________________________________________________________=
______
//

------=_NextPart_000_001C_01C1D675.DAB112C0
Content-Type: application/octet-stream;
	name="exewrapper.sh"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="exewrapper.sh"

#!/bin/bash=0A=
# exewrapper.sh v0.1, written by Peter M. Lemmen, 2002=0A=
=0A=
# Define the mono and ismonobin executables.=0A=
MONO=3D/usr/local/bin/mono=0A=
ISMONOBIN=3D/usr/local/bin/ismonobin=0A=
=0A=
# Check for at least one argument.=0A=
if [ $# -lt "1" ]=0A=
then=0A=
  echo "Usage: `basename $0` <executable> [arguments]"=0A=
  exit 0=0A=
fi=0A=
=0A=
# Test of the passed argument is a Mono Binary.=0A=
$ISMONOBIN $1=0A=
=0A=
# If so, run it with the Mono JIT.=0A=
if [ $? -eq 1 ]=0A=
then=0A=
  $MONO $1 $*=0A=
  exit 0=0A=
fi=0A=
=0A=
# If not, well, do something else I suppose.=0A=
# TODO: Wine and DOSEMU support.=0A=
echo "File \"$1\" is NOT a Mono Binary."=0A=
=0A=
exit 0=0A=

------=_NextPart_000_001C_01C1D675.DAB112C0
Content-Type: application/octet-stream;
	name="binfmt_misc_mono.sh"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="binfmt_misc_mono.sh"

#!/bin/bash=0A=
# binfmt_misc_mono.sh v0.1, written by Peter M. Lemmen, 2002=0A=
=0A=
# Define the register and exewrapper location.=0A=
REGISTER=3D/proc/sys/fs/binfmt_misc/register=0A=
EXEWRAPPER=3D/usr/local/bin/exewrapper.sh=0A=
MODPROBE=3D/sbin/modprobe=0A=
ECHO=3D/bin/echo=0A=
=0A=
# Create a Registration String.=0A=
REGISTRATION=3D:ExeWrapper:M::MZ\\x90\\x00\\x03\\x00::$EXEWRAPPER:=0A=
=0A=
# Load the binfmt_misc module=0A=
$MODPROBE binfmt_misc=0A=
=0A=
# Register the exewrapper =0A=
$ECHO $REGISTRATION > $REGISTER=0A=
=0A=
exit 0=0A=

------=_NextPart_000_001C_01C1D675.DAB112C0--