/* sgen 0.31337a

  A VERY quick and dirty hack that originated from an 
  upcoming badc0ded project. sgen.c compiles shellcodes 
  from nasm source. Tested on FreeBSD and Linux but 
  should work with most unix like systems with nasm. 

  dim@badc0ded.com

DISCLAIMER
  
  We claim no responsibility for anything, not for the code or
  if it does not work for you. If you do not understand the code
  you probably are a retard and we do not take any responsibility 
  for that either, I mean why would we ?! Blame your parents.


Shouts 
  Zen-parse, lockdown, sloth, ^ChAoS, elfan, 
  Albert E., S. Hawking, Ali G, Jenna Jameson
  flur, FreeBSD Security Officers, the guy that
  cleans our desks every morning, QNX developers,
  Jon Lasser (col/67), ourselves and Jenna again.


FreeBSD sample code - ./sgen sample.asm 
BITS 32 ; nasm seems to default to 16bits with flat format binaries.
global main
main:
  jmp short bottom
top:
  pop ebx
  xor eax,eax
  mov al,17
  push eax
  push ebx
  mov al,1
  push eax
  push eax
  mov al,4
  int 0x80
  mov al,1
  int 0x80
bottom:
call top
bleh db "www.badc0ded.com",0xa

*/

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define NASM_PATH "/usr/local/bin/nasm"
main (int argc, char *argv[])
{
  char *shellcode,*nasmv[10];
  int fd,i,pid;
  struct stat sb; 
  nasmv[0]="nasm";
  nasmv[1]="-f";
  nasmv[2]="bin"; 
  nasmv[3]="-o";
  nasmv[4]="shellcode.gen";
  nasmv[5]=argv[1];
  nasmv[6]=NULL;
  pid=fork();
  if (pid==0)
  {
    execv(NASM_PATH,nasmv);
    exit(0);
  }
  wait(0);
  stat("shellcode.gen",&sb);
  if ((fd=open("shellcode.gen",O_RDONLY))<0)
  {
    perror("open");
    exit(-1);
  }
  shellcode=malloc(sb.st_size);
  read(fd,shellcode,sb.st_size);
  printf("main(){\nchar s[]=\"");
  for(i=0;i<=sb.st_size;i++)
    printf("\\x%x",(u_char )shellcode[i]);
  printf("\";\nvoid (*f)();\nf=(void *)s;\nf();\n}\n");
  
}
