Sunday, January 23, 2005

From GAS to Flat

(For the curious, this post was sent from my commandline, using a combination of printf,cat and mail in one pipeline. nifty huh? linux rocks so does google so does

Recently, I have been dabbling in shellcode.

One frustration I had was getting at flat binaries while working with GAS. Well, while there may exist other solutions, I was sort of too lazy to find out, so i made this script, that uses objdump to print out hexcode of gas produced binaries.

As a side kick, it can also write this hexcode into a file hence producing a flat binary.

This script is part of a larger project i'm working on anyway, I guess I'll have a release ready sometime next week if work permits the time.

For the poetic, I embedded a small poem (should I say bad poem?) at the bottom of the program. It inspires me to always approach programming with the same joyfull fantasy I first had when I started, and to always strive to produce beautifull looking code (at least beautiful to me).

Enjoy :)

#! /bin/bash

# obj2hex, obj2bin

# Copyright (c) 2005 by Essien Ita Essien
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.

# Calling this program as obj2hex ouputs the hexcode representation of the binary file to stdout

# Calling this program as obj2bin creates a flat binary of the specified file into another specified file.

function usage()

[[ $programname == "obj2hex" ]] && echo "Usage: $programname " && exit
[[ $programname == "obj2bin" ]] && echo "Usage: $programname " && exit


programname=`basename $0`

[[ $programname == "obj2hex" ]] && [[ $# != 1 ]] && usage $programname
[[ $programname == "obj2bin" ]] && [[ $# != 2 ]] && usage $programname


all=`objdump -d $filename | grep '^ [0-9a-f]'| cut -f2`

for a in $all;
b=$b`echo -n '\x'$a`

[[ $programname == "obj2bin" ]] && printf "$b" > $outputfile && exit
echo "$b"

# There is an art in programming.
# A poem in every function,
# A tune in every loop,
# I hope is exists here too.
# -Essien Ita Essien


Post a Comment

<< Home