=====================================================================
LAB> c 언어에서 명령행 인자를 이용한 프로그램 실행
# vi argc.c
-- argc.c --
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char cmd[100];
// 디버그 모드이면 argc 변수의 값을 출력해라.
#ifdef DEBUG
printf("argc = %d \n", argc);
#endif
if(argc != 2)
{
fprintf(stderr, "Usage : %s username\n", argv[0]);
exit(1);
}
// 명령어 문자열을 완성한다.
// e.g. useradd linux
sprintf(cmd, "useradd %s", argv[1]);
system(cmd);
// 명령어 문자열을 완성한다.
// e.g. passwd linux
sprintf(cmd, "passwd %s", argv[1]);
system(cmd);
return 0;
}
-- argc.c --
# ./argc bbb
Changing password for user bbb.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
# ssh bbb@localhost
bbb@localhost's password:
$ id
uid=510(bbb) gid=510(bbb) groups=510(bbb) context=user_u:system_r:unconfined_t:s0
# gcc -g -o argc argc.c
[root@fw c]# gdb -q argc
Reading symbols from /root/programming/c/argc...done.
(gdb) b main
Breakpoint 1 at 0x804846b: file argc.c, line 13.
(gdb) r
Starting program: /root/programming/c/argc
Breakpoint 1, main (argc=1, argv=0xbfffea94) at argc.c:13
13 if(argc != 2)
(gdb) n
15 fprintf(stderr, "Usage : %s username\n", argv[0]);
(gdb) n
Usage : /root/programming/c/argc username
16 exit(1);
(gdb) n
Program exited with code 01.
(gdb) n
The program is not being run.
(gdb) n
The program is not being run.
(gdb) n
The program is not being run.
(gdb) r
Starting program: /root/programming/c/argc
warning: .dynamic section for "/lib/libc.so.6" is not at the expected address
warning: difference appears to be caused by prelink, adjusting expectations
Breakpoint 1, main (argc=1, argv=0xbfffea94) at argc.c:13
13 if(argc != 2)
(gdb)
(gdb)
(gdb)
(gdb) r ccc
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/programming/c/argc ccc
Breakpoint 1, main (argc=2, argv=0xbfffea94) at argc.c:13
13 if(argc != 2)
(gdb) info registers
eax 0xbfffea94 -1073747308
ecx 0xbfffea10 -1073747440
edx 0x2 2
ebx 0x87dff4 8904692
esp 0xbfffe970 0xbfffe970
ebp 0xbfffe9f8 0xbfffe9f8
esi 0x721ca0 7478432
edi 0x0 0
eip 0x804846b 0x804846b <main+23>
eflags 0x200282 [ SF IF ID ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
(gdb) x/16xw $ebp
0xbfffe9f8: 0xbfffea68 0x0073aebc 0x00721ca0 0x08048520
0xbfffea08: 0xbfffea68 0x0073aebc 0x00000002 0xbfffea94
0xbfffea18: 0xbfffeaa0 0x00722828 0x00000000 0x00000001
0xbfffea28: 0x00000001 0x00000000 0x0087dff4 0x00721ca0
(gdb) x/s 0xbfffea94
0xbfffea94: "\252\353\377\277\303\353\377\277"
(gdb) x/x 0xbfffea94
0xbfffea94: 0xbfffebaa
(gdb) x/16xw 0xbfffea94
0xbfffea94: 0xbfffebaa 0xbfffebc3 0x00000000 0xbfffebc7
0xbfffeaa4: 0xbfffebe1 0xbfffebf1 0xbfffebfc 0xbfffec0a
0xbfffeab4: 0xbfffec2c 0xbfffec3f 0xbfffec49 0xbfffee0c
0xbfffeac4: 0xbfffee18 0xbfffee9d 0xbfffeeb7 0xbfffeec6
(gdb) x/s 0xbfffebaa
0xbfffebaa: "/root/programming/c/argc"
(gdb) x/s 0xbfffebc3
0xbfffebc3: "ccc"
(gdb)
(gdb) n
21 sprintf(cmd, "useradd %s", argv[1]);
(gdb) display cmd
1: cmd = "h\202\004\b", '\000' <repeats 12 times>, ".N=\366", '\000' <repeats 24 times>"\260, \203\004\b\000\000\000\000`\227\004\b\330\351\377\277\r\203\004\b\364\337\207\000\f\302\207\000\b\352\377\277\071\205\004\b\205\017u\000\240\352\377\277\b\352\377\277\364\337\207\000\200Hq"
(gdb) print argv[1]
$1 = 0xbfffebc3 "ccc"
(gdb) n
22 system(cmd);
1: cmd = "useradd ccc\000\000\000\000\000.N=\366", '\000' <repeats 24 times>"\260, \203\004\b\000\000\000\000`\227\004\b\330\351\377\277\r\203\004\b\364\337\207\000\f\302\207\000\b\352\377\277\071\205\004\b\205\017u\000\240\352\377\277\b\352\377\277\364\337\207\000\200Hq"
(gdb) n
Detaching after fork from child process 9544.
26 sprintf(cmd, "passwd %s", argv[1]);
1: cmd = "useradd ccc\000\000\000\000\000.N=\366", '\000' <repeats 24 times>"\260, \203\004\b\000\000\000\000`\227\004\b\330\351\377\277\r\203\004\b\364\337\207\000\f\302\207\000\b\352\377\277\071\205\004\b\205\017u\000\240\352\377\277\b\352\377\277\364\337\207\000\200Hq"
(gdb) n
27 system(cmd);
1: cmd = "passwd ccc\000\000\000\000\000\000.N=\366", '\000' <repeats 24 times>"\260, \203\004\b\000\000\000\000`\227\004\b\330\351\377\277\r\203\004\b\364\337\207\000\f\302\207\000\b\352\377\277\071\205\004\b\205\017u\000\240\352\377\277\b\352\377\277\364\337\207\000\200Hq"
(gdb) n
Detaching after fork from child process 9549.
Changing password for user ccc.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
29 return 0;
1: cmd = "passwd ccc\000\000\000\000\000\000.N=\366", '\000' <repeats 24 times>"\260, \203\004\b\000\000\000\000`\227\004\b\330\351\377\277\r\203\004\b\364\337\207\000\f\302\207\000\b\352\377\277\071\205\004\b\205\017u\000\240\352\377\277\b\352\377\277\364\337\207\000\200Hq"
(gdb) c
Continuing.
=====================================================================