IT 그리고 정보보안/Knowledge base

strace, ltrace

plummmm 2021. 4. 18. 20:05
반응형

strace (SystemCall Trace)

시스템 콜을 추적하는 명령어이다.

리눅스 상에서 작동하는 모든 프로그램은 시스템콜을 이용한다.(당연한;;)

[succubus@localhost succubus]$ strace ./nightmara

execve("./nightmara", ["./nightmara"], [/* 20 vars */]) = 0

brk(0)                                  = 0x8049938

old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0                   x40014000

open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)

open("/etc/ld.so.cache", O_RDONLY)      = 3

fstat(3, {st_mode=S_IFREG|0644, st_size=12210, ...}) = 0

old_mmap(NULL, 12210, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40015000

close(3)                                = 0

open("/lib/libc.so.6", O_RDONLY)        = 3

fstat(3, {st_mode=S_IFREG|0755, st_size=4101324, ...}) = 0

read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\210\212"..., 4096) = 40                   96

old_mmap(NULL, 1001564, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x40018000

mprotect(0x40105000, 30812, PROT_NONE)  = 0

old_mmap(0x40105000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0xec                   000) = 0x40105000

old_mmap(0x40109000, 14428, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANON                   YMOUS, -1, 0) = 0x40109000

close(3)                                = 0

mprotect(0x40018000, 970752, PROT_READ|PROT_WRITE) = 0

mprotect(0x40018000, 970752, PROT_READ|PROT_EXEC) = 0

munmap(0x40015000, 12210)               = 0

personality(PER_LINUX)                  = 0

getpid()                                = 1289

fstat64(0x1, 0xbffff378)                = -1 ENOSYS (Function not implemented)

fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0

old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0                   x40015000

ioctl(1, TCGETS, {B9600 opost isig icanon echo ...}) = 0

write(1, "argv error\n", 11argv error

)            = 11

munmap(0x40015000, 4096)                = 0

_exit(0)                                = ?

 

strace를 실행한 화면이다.

 

execve("./nightmara", ["./nightmara"], [/* 20 vars */]) = 0 ​만 따로 떼어내서 설명하자면,

인자로 ./nightmara를 주었고, 20개의 환경변수를 넘겼다는 것을 알 수 있다.

주요 옵션으로는.

* strace -p [PID]  : 현재 실행중인 프로세스를 알아보는 옵션이다.

* strace -p -f [PID] : 현재 실행중인 프로세스에서 fork되어 나오는 프로세스까지 트레이스하는 옵션이다.

 

주로 네트워크 프로그램들을 추적할 때 많이 쓰인다. daemon에서 fork를 많이 하기 때문에.

소캣 서버가 제대로 동작하는지, 텔넷서버에 접속했는데 inetd 가 올바르게 작동하는지 등등.

 

ltrace (Library Call Trace)

strace가 시스템콜을 추적하는 툴이었다면,

ltrace는 라이브러리 호출을 추적하는 툴이다.

[succubus@localhost succubus]$ ltrace ls

__libc_start_main(0x08049500, 1, 0xbffffb64, 0x08048f40, 0x0804f3bc <unfinished ...>

__register_frame_info(0x0805319c, 0x08053398, 0xbffffb24, 0x08048f65, 0x4010c1ec) = 0x4001bb60

setlocale(6, "")                                  = "en_US"

bindtextdomain("fileutils", "/usr/share/locale")  = "/usr/share/locale"

textdomain("fileutils")                           = "fileutils"

time(NULL)                                        = 1407903495

isatty(1)                                         = 1

getenv("QUOTING_STYLE")                           = NULL

getenv("LS_BLOCK_SIZE")                           = NULL

getenv("BLOCK_SIZE")                              = NULL

getenv("POSIXLY_CORRECT")                         = NULL

getenv("COLUMNS")                                 = NULL

ioctl(1, 21523, 0xbffffad8)                       = 0

getenv("POSIXLY_CORRECT")                         = NULL

getenv("TABSIZE")                                 = NULL

getopt_long(1, 0xbffffb64, "abcdefghiklmnopqrstuvw:xABCDFGHI"..., 0x0804f4cc, NULL) = -1

malloc(36)                                        = 0x08054958

malloc(36)                                        = 0x08054980

malloc(11600)                                     = 0x080549a8

malloc(12)                                        = 0x08057700

malloc(2)                                         = 0x08057710

strcpy(0x08057710, ".")                           = 0x08057710

__errno_location()                                = 0x4010cd60

opendir(".")                                      = 0x08057720

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x4010cd60) = 0x08057750

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x08057750) = 0x08057764

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x08057764) = 0x0805777c

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x0805777c) = 0x08057798

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x08057798) = 0x080577b8

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x080577b8) = 0x080577d8

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x080577d8) = 0x080577f4

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x080577f4) = 0x08057810

malloc(12)                                        = 0x08058758

strcpy(0x08058758, "nightmare.c")                 = 0x08058758

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x08057810) = 0x08057830

malloc(10)                                        = 0x08058768

strcpy(0x08058768, "nightmare")                   = 0x08058768

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x08057830) = 0x0805784c

malloc(10)                                        = 0x08058778

strcpy(0x08058778, "nightmara")                   = 0x08058778

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x0805784c) = 0x08057868

readdir64(0x08057720, 0x08057700, 0xbffffb64, 1, 0x08057868) = 0

closedir(0x08057720)                              = 0

qsort(0x080549a8, 3, 116, 0x0804b830)             = <void>

strcmp("nightmare", "nightmara")                  = 4

strcmp("nightmare.c", "nightmara")                = 4

strcmp("nightmare.c", "nightmare")                = 46

malloc(468)                                       = 0x08057720

malloc(4)                                         = 0x080578f8

malloc(8)                                         = 0x08057908

malloc(12)                                        = 0x08057918

malloc(16)                                        = 0x08057928

malloc(20)                                        = 0x08057940

malloc(24)                                        = 0x08057958

malloc(28)                                        = 0x08057978

malloc(32)                                        = 0x08057998

malloc(36)                                        = 0x080579c0

malloc(40)                                        = 0x080579e8

malloc(44)                                        = 0x08057a18

malloc(48)                                        = 0x08057a48

malloc(52)                                        = 0x08057a80

malloc(56)                                        = 0x08057ab8

malloc(60)                                        = 0x08057af8

malloc(64)                                        = 0x08057b38

malloc(68)                                        = 0x08057b80

malloc(72)                                        = 0x08057bc8

malloc(76)                                        = 0x08057c18

malloc(80)                                        = 0x08057c68

malloc(84)                                        = 0x08057cc0

malloc(88)                                        = 0x08057d18

malloc(92)                                        = 0x08057d78

malloc(96)                                        = 0x08057dd8

malloc(100)                                       = 0x08057e40

malloc(104)                                       = 0x08057ea8

malloc(108)                                       = 0x08057f18

malloc(112)                                       = 0x08057f88

malloc(116)                                       = 0x08058000

malloc(120)                                       = 0x08058078

malloc(124)                                       = 0x080580f8

malloc(128)                                       = 0x08058178

malloc(132)                                       = 0x08058200

malloc(136)                                       = 0x08058288

malloc(140)                                       = 0x08058318

malloc(144)                                       = 0x080583a8

malloc(148)                                       = 0x08058440

malloc(152)                                       = 0x080584d8

malloc(156)                                       = 0x08058578

fwrite("nightmara", 1, 9, 0x4010a980)             = 9

_IO_putc(' ', 0x4010a980)                         = 32

_IO_putc(' ', 0x4010a980)                         = 32

fwrite("nightmare", 1, 9, 0x4010a980)             = 9

_IO_putc(' ', 0x4010a980)                         = 32

_IO_putc(' ', 0x4010a980)                         = 32

fwrite("nightmare.c", 1, 11, 0x4010a980)          = 11

_IO_putc('\n', 0x4010a980nightmara  nightmare  nightmare.c

)                        = 10

free(0x08057710)                                  = <void>

free(0x08057700)                                  = <void>

ferror(0x4010a980)                                = 0

fclose(0x4010a980)                                = 0

exit(0)                                           = <void>

__deregister_frame_info(0x0805319c, 0xbffffaec, 0x0804f3d1, 0x4010c1ec, 0xbffffb00) = 0x08053398

+++ exited (status 0) +++

 

사용되는 함수들과 시그널을 출력해주는 것을 알 수 있다.

역시 마찬가지로 -p 옵션을 주면 실행중인 프로세스에 attach 할 수 있다.

반응형