[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[OFF-TOPIC] simple y trivial detector de gestores en el MBR



Os mando esta somera chorrada.
Se que a muchos os parecerá una bobada y una somera trivialidad, pero al final a mi me ha ido de perlas crearme esta tontería para que los usuarios de mi red puedan detectar si tienen GRUB o LILO sin necesidad de conocer el uso de dd ni lo de los 512 primeros bytes del MBR.

Para compilar:
gcc checkboot.c -o checkboot

¡¡agur!!

P.D: os adjunto 3 mbr extraidos con dd para que probeis :

./checkboot grub.mbr
./checkboot lilo.mbr
./checkboot xp.mbr

Como es lógico, también se puede lanzar (como root) contra el MBR directamente del disco:

./checkboot /dev/hda
./checkboot /dev/md0 ....

agur !!
//
// checkboot
// Comprobacion del MBR para determinar si se encuentra LILO o GRUB instalado
//	
// 2005 by HellOnNET.TK
// http://www.hellonnet.tk
//


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <errno.h>

#include <locale.h>

#define VERSION	"checkboot 0.1a by HellOnNET.TK\n" \
		"http://www.hellonnet.tk\n";

// El tamaño del MBR es de 512 bytes
#define MBR_SIZE 512

// LILO :
// Lilo sempre desa a la posicio 6 del MBR la cadena "LILO". 
//
// NOTA: Extraído del código fuente "common.h" de lilo :
// typedef struct {
// 	char jump[6]; /* jump over the data */
//      char signature[4]; /* "LILO" */
//      unsigned short stage,version; /* stage is 0x10 */
//      unsigned short offset; /* partition entry offset */
//      unsigned char drive; /* BIOS drive code */
//      unsigned char head; /* head; always 0 */
//      unsigned short drvmap; /* offset of drive map */
//     unsigned char ptable[PARTITION_ENTRY*PARTITION_ENTRIES]; /* part. table */
// } BOOT_PARAMS_C; /* chain loader */
//
#define LILO_STRING "LILO"
#define LILO_STRING_OFFSET 6

//
// GRUB no es desa a una posicio concreta....al menys segons versions...
// Al fitxer stage1.S del GRUB:
// notification_string:    .string "GRUB "
//
const char * GRUB_STRING = "GRUB";

//
// BOOT_SIGNATURE sempre esta en la posicio 510d, ocupa 2 bytes y val 0x55aa
//
#define BOOT_SIGNATURE_OFFSET 510
const char BOOT_SIGNATURE[3] = {85,170,0};	/* 0x55AA !! */
//
// checkboot
// Comprobacion del MBR para determinar si se encuentra LILO o GRUB instalado
//      
// 2005 by HellOnNET.TK
// http://www.hellonnet.tk
//
// USO: checkbox device 
// NOTA: el "device" puede ser /dev/md0 , /dev/sda ... o bien un archivo de
// MBR volcado con dd
//
//
// TODO: Detectar "CORRECTAMENTE" al gestor GRUB.

#include "checkboot.h"

int main (int argc, char **argv) {

	int hd;
	char * mbr;				/* MBR */
	char boot_string[5];			/* LILO o GRUB */
	char boot_signature[3];			/* 0xAA55 */
	char * curLocale;

	char * bf;

	curLocale = setlocale(LC_ALL,"es_ES");

	printf("%s", VERSION);
	if(argc<2 || argc>2) { printf("Us: %s device\n", *argv); return -1; }

	// Intentar obrir el device indicat per llegir :
	hd = open(*(argv+1), O_RDONLY|O_NONBLOCK);
	if(hd==-1) { perror("OPEN"); return errno; }
	mbr = (void *)malloc(MBR_SIZE+1);

	// Llegir MBR a buffer :
	if(read(hd,mbr,MBR_SIZE)!=MBR_SIZE){
		close(hd); free(mbr);
		printf("No s\'han llegit %d bytes.", MBR_SIZE);
		return -1;
	}*(mbr+MBR_SIZE) = '\0';
	close(hd);

	// Comprobar si hay codigo de arranque :
	memcpy(boot_signature,mbr+BOOT_SIGNATURE_OFFSET,2);
	boot_signature[2]='\0';
	if(strcmp(boot_signature,BOOT_SIGNATURE)!=0){
		setlocale(LC_ALL,curLocale);
		free(mbr);
		printf("GESTOR ENCONTRADO: MBR sin codigo stage1!!\n");
		return -1;
	}

	// Recoger posible cadena "LILO" :
	boot_string[4]='\0';
	memcpy(boot_string,mbr+LILO_STRING_OFFSET,4);
	if(strcmp(boot_string,LILO_STRING)==0)
		printf("GESTOR ENCONTRADO: %s\n", boot_string);
	else {
		// La búsqueda de GRUB se realiza buscando la cadena
		// de GRUB dentro del MBR en cualquier posicion :
		if(memmem(mbr,MBR_SIZE,GRUB_STRING,4)==NULL)
			printf("GESTOR ENCONTRADO: ?¿\n");
		else    printf("GESTOR ENCONTRADO: GRUB\n");
	}

	// Liberar y salir:
	setlocale(LC_ALL,curLocale);
	free(mbr);
	return 0;
}

Attachment: grub.mbr
Description: Binary data

Attachment: lilo.mbr
Description: Binary data

Attachment: xp.mbr
Description: Binary data


Reply to: