From 7f9d891ab760e6c8fdba48ed922521159e0fdaea Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Thu, 21 Jul 2022 21:17:45 -0500 Subject: [PATCH] Import MachoManic Magics to determine bits --- helpers/scanner/parsers/macho-node/parser.js | 38 +++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/helpers/scanner/parsers/macho-node/parser.js b/helpers/scanner/parsers/macho-node/parser.js index 0bd7903..fd16d82 100644 --- a/helpers/scanner/parsers/macho-node/parser.js +++ b/helpers/scanner/parsers/macho-node/parser.js @@ -2,6 +2,18 @@ import Reader from 'endian-reader' import { constants } from './constants' +import { MAGIC } from '~/helpers/macho/macho.magic.js' + +function determineMagicBits ( magic ) { + for ( const [ key, value ] of Object.entries( MAGIC ) ) { + if ( value === magic ) { + return key.includes( '64' ) ? 64 : 32 + } + } + + return null +} + export class Parser extends Reader { constructor () { super() @@ -9,6 +21,9 @@ export class Parser extends Reader { execute (buf) { var hdr = this.parseHead(buf); + + // console.log( 'hdr', hdr ) + if (!hdr) throw new Error('File not in a mach-o format'); @@ -33,23 +48,28 @@ export class Parser extends Reader { if (buf.length < 7 * 4) return false; - var magic = buf.readUInt32LE(0); - var bits; - if (magic === 0xfeedface || magic === 0xcefaedfe) - bits = 32; - else if (magic === 0xfeedfacf || magic == 0xcffaedfe) - bits = 64; - else - return false; + // console.log( 'Has proper length' ) - if (magic & 0xff == 0xfe) + var magic = buf.readUInt32LE(0); + var bits = determineMagicBits( magic ) + + if ( !bits ) + return false + + // console.log( 'Has proper bits' ) + + if ( magic & 0xff == 0xfe ) + // Big Endian this.setEndian('be'); else + // Little Endian this.setEndian('le'); if (bits === 64 && buf.length < 8 * 4) return false; + // console.log( 'Has proper length' ) + var cputype = constants.cpuType[this.readInt32(buf, 4)]; var cpusubtype = this.readInt32(buf, 8); var filetype = this.readUInt32(buf, 12);