Devilzc0de Forum Follow @devilzc0de
  • Home
  • Hacking
  • Networking
  • Programming
  • O.S
  • Server
  • Tweets
  • Search
  • Member List
  • Calendar
Current time: 05-26-2013, 04:04 AM Hello There, Guest! (Login — Register)
Devilzc0de Forum › Information Technology › Programming › C / C++ v
« Previous 1 ... 5 6 7 8 9 ... 15 Next »

c quiz - find the bug of this c code

Home General Computer Multimedia Business Lounge

Pages (2): 1 2 Next »
Post Reply 
Tweet
Threaded Mode | Linear Mode
c quiz - find the bug of this c code
03-21-2012, 10:56 PM (This post was last modified: 03-22-2012 04:35 PM by ev1lut10n.)
Post: #1
ev1lut10n Offline
./Devilz Officer
Posts: 239
Joined: Aug 2011
Reputation: 82
c quiz - find the bug of this c code
given sample gethostbyname c source file which contains a bug, try to find the bug :
buggy_code.c:
====
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
int main(int argc,char *argv[])
{
struct in_addr ipx;
struct hostent *he;
char buf[]="";
int argl=strlen(argv[1]);
strncpy(buf,argv[1],argl);
he=gethostbyname(buf);
printf("\nhost :%s\n",he->h_name);
bcopy(*he->h_addr_list++, (char *) &ipx, sizeof(ipx));
printf("address: %s\n", inet_ntoa(ipx));
return 0;
}
===========
Find all posts by this user
Quote this message in a reply
03-21-2012, 11:40 PM
Post: #2
alessandra Offline
i'm so lonely broken angel
Posts: 196
Joined: Feb 2012
Reputation: 41
RE: c quiz - find the bug of this c code
ketemu om wawa
Code:
alessandra@hatimu:~$ gcc -g -fno-stack-protector -z execstack -o odon odon.c
alessandra@hatimu:~$ ./odon
Segmentation fault
alessandra@hatimu:~$ gdb odon
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/alessandra /odon...done.
(gdb) list
2    #include <stdlib.h>
3    #include <netinet/in.h>
4    #include <arpa/inet.h>
5    #include <netdb.h>
6    #include <string.h>
7    int main(int argc,char *argv[])
8    {
9    struct in_addr ipx;
10    struct hostent *he;
11    char host[]="";
(gdb) r odon
Starting program: /home/alessandra /odon odon
[Image: alessa2.png]
Code:
Program received signal SIGSEGV, Segmentation fault.
0x0804852b in main (argc=2, argv=0xbffff454) at odon.c:16
16    printf("\nhost :%s\n",he->h_name);
(gdb) break 16
Breakpoint 1 at 0x8048527: file odon.c, line 16.
(gdb) ir
Undefined command: "ir".  Try "help".
(gdb) i r
eax            0x0    0
ecx            0xb7e6f6c0    -1209600320
edx            0xfffffff4    -12
ebx            0xb7fc8ff4    -1208184844
esp            0xbffff380    0xbffff380
ebp            0xbffff3a8    0xbffff3a8
esi            0x0    0
edi            0x0    0
eip            0x804852b    0x804852b <main+103>
eflags         0x10286    [ PF SF IF RF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x33    51
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/alessandra /odon odon

Breakpoint 1, main (argc=2, argv=0xbffff454) at odon.c:16
16    printf("\nhost :%s\n",he->h_name);
(gdb) list
11    char host[]="";
12    char buf[]="";
13    int argl=strlen(argv[1]);
14    strncpy(buf,argv[1],argl);
15    he=gethostbyname(host);
16    printf("\nhost :%s\n",he->h_name);
17    bcopy(*he->h_addr_list++, (char *) &ipx, sizeof(ipx));
18    printf("address: %s\n", inet_ntoa(ipx));
19    return 0;
20    }
(gdb)
[Image: alessa2.png]
Find all posts by this user
Quote this message in a reply
 Reputed by :  ditatompel(+1)
03-22-2012, 12:39 AM
Post: #3
ditatompel Offline
Administrator
*******
Administrators
Posts: 2,168
Joined: Dec 2010
Reputation: 367
RE: c quiz - find the bug of this c code
(03-21-2012 11:40 PM)alessandra Wrote:  ketemu om wawa
Code:
alessandra@hatimu:~$ gcc -g -fno-stack-protector -z execstack -o odon odon.c
alessandra@hatimu:~$ ./odon
Segmentation fault
alessandra@hatimu:~$ gdb odon
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/alessandra /odon...done.
(gdb) list
2    #include <stdlib.h>
3    #include <netinet/in.h>
4    #include <arpa/inet.h>
5    #include <netdb.h>
6    #include <string.h>
7    int main(int argc,char *argv[])
8    {
9    struct in_addr ipx;
10    struct hostent *he;
11    char host[]="";
(gdb) r odon
Starting program: /home/alessandra /odon odon
[Image: alessa2.png]
Code:
Program received signal SIGSEGV, Segmentation fault.
0x0804852b in main (argc=2, argv=0xbffff454) at odon.c:16
16    printf("\nhost :%s\n",he->h_name);
(gdb) break 16
Breakpoint 1 at 0x8048527: file odon.c, line 16.
(gdb) ir
Undefined command: "ir".  Try "help".
(gdb) i r
eax            0x0    0
ecx            0xb7e6f6c0    -1209600320
edx            0xfffffff4    -12
ebx            0xb7fc8ff4    -1208184844
esp            0xbffff380    0xbffff380
ebp            0xbffff3a8    0xbffff3a8
esi            0x0    0
edi            0x0    0
eip            0x804852b    0x804852b <main+103>
eflags         0x10286    [ PF SF IF RF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x33    51
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/alessandra /odon odon

Breakpoint 1, main (argc=2, argv=0xbffff454) at odon.c:16
16    printf("\nhost :%s\n",he->h_name);
(gdb) list
11    char host[]="";
12    char buf[]="";
13    int argl=strlen(argv[1]);
14    strncpy(buf,argv[1],argl);
15    he=gethostbyname(host);
16    printf("\nhost :%s\n",he->h_name);
17    bcopy(*he->h_addr_list++, (char *) &ipx, sizeof(ipx));
18    printf("address: %s\n", inet_ntoa(ipx));
19    return 0;
20    }
(gdb)
[Image: alessa2.png]

Maksudnya gimana nih tante? bingung
Jelasin dong, ane jg pingin belajar.. smangat
Find all posts by this user
Quote this message in a reply
03-22-2012, 12:47 AM
Post: #4
alessandra Offline
i'm so lonely broken angel
Posts: 196
Joined: Feb 2012
Reputation: 41
RE: c quiz - find the bug of this c code
@ditatompel
itu bugnya dit jadi di
Code:
printf("address: %s\n", inet_ntoa(ipx));
nampilin output segmentation fault kl gak di tambahin beberapa char piss
Find all posts by this user
Quote this message in a reply
03-22-2012, 01:14 AM
Post: #5
ditatompel Offline
Administrator
*******
Administrators
Posts: 2,168
Joined: Dec 2010
Reputation: 367
RE: c quiz - find the bug of this c code
(03-22-2012 12:47 AM)alessandra Wrote:  @ditatompel
itu bugnya dit jadi di
Code:
printf("address: %s\n", inet_ntoa(ipx));
nampilin output segmentation fault kl gak di tambahin beberapa char piss

Owh.. fungsi convert ip inet_ntoa klo ipx valuenya null ya?
Ijin belajar omtan sandra dan om dom.. belajar
Find all posts by this user
Quote this message in a reply
03-22-2012, 05:25 AM
Post: #6
ev1lut10n Offline
./Devilz Officer
Posts: 239
Joined: Aug 2011
Reputation: 82
RE: c quiz - find the bug of this c code
@alessandra:
mantap mbak sandra hampir ketemu penyebab bugnya dikit lagi

btw boleh kenalan ga
Find all posts by this user
Quote this message in a reply
03-22-2012, 09:44 AM
Post: #7
alessandra Offline
i'm so lonely broken angel
Posts: 196
Joined: Feb 2012
Reputation: 41
RE: c quiz - find the bug of this c code
(03-22-2012 05:25 AM)ev1lut10n Wrote:  @alessandra:
mantap mbak sandra hampir ketemu penyebab bugnya dikit lagi

btw boleh kenalan ga

boleh bang malu trus bang kok dikit lagi.... ngemis
ajarin dong prustasi
(03-22-2012 01:14 AM)ditatompel Wrote:  
(03-22-2012 12:47 AM)alessandra Wrote:  @ditatompel
itu bugnya dit jadi di
Code:
printf("address: %s\n", inet_ntoa(ipx));
nampilin output segmentation fault kl gak di tambahin beberapa char piss

Owh.. fungsi convert ip inet_ntoa klo ipx valuenya null ya?
Ijin belajar omtan sandra dan om dom.. belajar

begitulah dit cium anyway gw juga belajar ama bang mywisdom smangat
Find all posts by this user
Quote this message in a reply
03-22-2012, 11:51 AM (This post was last modified: 03-22-2012 12:07 PM by ack_attack.)
Post: #8
ack_attack Offline
adiknya syn-attack
Posts: 27
Joined: Feb 2012
Reputation: 13
RE: c quiz - find the bug of this c code
kakak.. aku coba jawab ya kak... dan kalau ada yang salah maafin aku ya kak.... ^_^

bug I : panjang array dari variabel "char buf[]" tidak diketahui, dan pada pendeklarasian variabel, variabel "char buf[]" telah diisi dengan nilai "" (string kosong). jadi kalau menurut aku tidak mungkin kalau kita mengkopi string dari "argv[1]" sepanjang "strlen(argv[1])" ke dalam variabel tersebut.

solusi :
- panjang array dari "char buf[]" harus diinisialisasi terlebih dahulu. misal "char buf[256]"
- nilai string awal dari "char buf[256]" tidak boleh dideklarasikan langsung, melainkan dari ekspresi berikut --> "strncpy(buf, argv[1], strlen(argv[1]));"

bug II : variabel "char host[]" pada awal pendeklarasian sudah berisikan "" (string kosong). jadi pada ekspresi berikut --> "he = gethostbyname(host);" akan menghasilkan segmentation fault apabila kita akan mengkopi nilai dari "*he->h_addr_list++" ke "(char *)&ipx" sebanyak "sizeof(ipx)".

solusi : "char *host" atau "char host[]" tidak boleh berisi string kosong.

# sekali lagi maafkan ya kak kalau ada yang salah.... ^_^
Find all posts by this user
Quote this message in a reply
03-22-2012, 11:55 AM
Post: #9
alessandra Offline
i'm so lonely broken angel
Posts: 196
Joined: Feb 2012
Reputation: 41
RE: c quiz - find the bug of this c code
(03-22-2012 11:51 AM)ack_attack Wrote:  kakak.. aku coba jawab ya kak... dan kalau ada yang salah maafin aku ya kak.... ^_^

bug I : panjang array dari variabel "char buf[]" tidak diketahui, dan pada pendeklarasian variabel, variabel "char buf[]" te...lah diisi dengan nilai "" (string kosong). jadi kalau menurut aku tidak mungkin kalau kita mengkopi string dari "argv[1]" sepanjang "strlen(argv[1])" ke dalam variabel tersebut.

solusi :
- panjang array dari "char buf[]" harus diinisialisasi terlebih dahulu. misal "char buf[256]"
- nilai string awal dari "char buf[256]" tidak boleh dideklarasikan langsung, melainkan dari ekspresi berikut --> "strlen(buf, argv[1], strlen(argv[1]));"

bug II : variabel "char host[]" pada awal pendeklarasian sudah berisikan "" (string kosong). jadi pada ekspresi berikut --> "he = gethostbyname(host);" akan menghasilkan segmentation fault apabila kita akan mengkopi nilai dari "*he->h_addr_list++" ke "(char *)&ipx" sebanyak "sizeof(ipx)".

solusi : "char *host" atau "char host[]" tidak boleh berisi string kosong.

# sekali lagi maafkan ya kak kalau ada yang salah.... ^_^

oh gitu ya smangat makasih ack_attack lebih rinci
aku cuma ngertinya karena buff kosong trus segmentation fault
dan bisa menyebabkan buffer malu
salam kenal bang love
Find all posts by this user
Quote this message in a reply
03-22-2012, 11:56 AM
Post: #10
ack_attack Offline
adiknya syn-attack
Posts: 27
Joined: Feb 2012
Reputation: 13
RE: c quiz - find the bug of this c code
salam kenal juga kakak alessandra.... ^_^
Find all posts by this user
Quote this message in a reply
« Next Oldest | Next Newest »
Pages (2): 1 2 Next »
Post Reply 


Topic Tools
Topic Link :
BBCode :
HTML Code :
View a Printable Version Send Thread to a Friend Subscribe to this thread
Submit Google Submit Face book Submit to Digg Submit to Reddit Submit to Furl Submit to Del.icio.us Submit to Jeqq

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  [Ask] Need Help, inefficient c source code cr0security 0 89 10-26-2012 05:00 AM
Last Post: cr0security
  Shell Code Generator wenkhairu 15 4,046 08-11-2012 12:20 AM
Last Post: pistol-air
  "USE CASTING IN YOUR C CODE BITCH" garfield 2 245 05-11-2012 01:41 PM
Last Post: wendyaja
  (ASK) Cara menggunakan Code::Blocks IDE on UBUNTU Black.exe 3 721 03-11-2012 10:48 AM
Last Post: DC_Zulfikar
  [Tutor] Obfuscate source code C (bikin source code susah dibaca) ketek 14 931 02-04-2012 11:34 AM
Last Post: ack_attack

Users Browsing

  • Contact Us
  • devilzc0de
  • Return to Top
  • Mobile Version
  • RSS Syndication
  • Help
Current time: 05-26-2013, 04:04 AM Powered By MyBB, © 2002-2013 MyBB Group. Theme created by Justin S. | Mixed By Chaer.Newbie | Fixed By Aditya

USING THIS SITE INDICATES THAT YOU HAVE READ AND ACCEPT OUR TERMS. IF YOU DO NOT ACCEPT THESE TERMS, YOU ARE NOT AUTHORIZED TO USE THIS SITE