php ã GDB ã§ãããã°ããã®ã便å©ã 以ä¸ã®ã¨ã³ããªã§ææ³ã説æããã¦ãã
以éãç§ãæ¸ãã®ã¯ãããã®ã¨ã³ããªã«æ¸ããã¦ããæé ãçä¼¼ããã®ãªã®ã§ã詳細ãç¥ãã«ããã£ã¦ã¯æ¯éãªã³ã¯å ãèªãã§ã»ãã
æ¤è¨¼: GDB ã§å®è¡ä¸ã® php ããã»ã¹ãè¦ãè¦ã
GDB ã¯å®è¡ä¸ã®ããã»ã¹ã解æã®å¯¾è±¡ã¨ãããã¨ãã§ããããã®ã¨ã³ããªã§ã¯ãç¡éã«ã¼ããããã°ã£ã php ã GDB ã§è¦ãã¦ã¿ãã
<?php function third($bar = "") { for (;;) { } } function second() { third(); } function first() { second(); } first();
æ¤è¨¼ç°å¢
- CentOS7.4 (1708)
- php-5.4.16-42.el7.x86_64
- httpd-2.4.6-67.el7.centos.2.x86_64
- gdb-7.6.1-100.el7.x86_64
å
ã«æ¸ããç¡éã«ã¼ãããã³ã¼ãã /var/www/html/index.php
ã«ç½®ãã¦æ¤è¨¼ãã
1..gdbinit ãæã«å ¥ãã
.gdbinit
ãæã«å
¥ããé㯠å®è¡ä¸ã® php ã¨ãã¼ã¸ã§ã³ãåãããã㨠ð
wget https://raw.githubusercontent.com/php/php-src/PHP-5.4.16/.gdbinit
2. æ¤è¨¼ç¨ã®ã³ã¼ãã«ãªã¯ã¨ã¹ããåºã
curl 'http://localhost?username=hiboma'
3. gdb ã§ã¢ã¿ãããã
$ sudo gdb -q --init-command=/home/vagrant/.gdbinit -p <ãªã¯ã¨ã¹ããåãã httpd ã® pid>
4. gdb ã§ãããããã¦ä¸èº«ãè¦ã
å ã® Qiita ã®ã¨ã³ããªã®ã³ãã³ããã½ã¡ã½ã¡ãã¦ãããããã¯ãã¬ã¼ã¹ãåããã®ãããã便å©
(gdb) zbacktrace [0x7f3edef8c458] third() /var/www/html/index.php:5 [0x7f3edef8c3a0] third() /var/www/html/index.php:10 [0x7f3edef8c2e8] second() /var/www/html/index.php:14 [0x7f3edef8c230] first() /var/www/html/index.php:17
ããããã¨ã¯ã©ã¯ã©ã¨è¡¨ç¤ºããããç¾å¨ã®é¢æ°åãã¨ãã
(gdb) set print pretty on (gdb) print *executor_globals->active_op_array $2 = { type = 2 '\002', function_name = 0x7f3edefc2de8 "third", â scope = 0x0, fn_flags = 134217728, prototype = 0x0, num_args = 1, required_num_args = 0, arg_info = 0x7f3edefc45f8, refcount = 0x7f3edefc2e08, opcodes = 0x7f3edefc2e28, last = 6, vars = 0x7f3edefc41d8, last_var = 2, T = 1, brk_cont_array = 0x7f3edefc3a38, last_brk_cont = 1, try_catch_array = 0x0, last_try_catch = 0, static_variables = 0x0, this_var = 4294967295, filename = 0x7f3edefc1b98 "/var/www/html/index.php", â line_start = 3, line_end = 7, doc_comment = 0x0, doc_comment_len = 0, early_binding = 4294967295, literals = 0x7f3edefc4368, last_literal = 4, run_time_cache = 0x0, last_cache_slot = 0, reserved = {0x0, 0x0, 0x0, 0x0} }
hmhm … HTTP ãªã¯ã¨ã¹ãå¨ãã®ããããå½¹ã«ç«ã¡ãããªãã¼ã¿ãã¨ãã
(gdb) print sapi_globals->request_info $3 = { request_method = 0x55cc687545f0 "GET", â query_string = 0x55cc6875af80 "username=hiboma", â post_data = 0x0, raw_post_data = 0x0, cookie_data = 0x0, content_length = 0, post_data_length = 0, raw_post_data_length = 0, path_translated = 0x55cc6875afa0 "/var/www/html/index.php", â request_uri = 0x55cc6875af90 "/index.php", content_type = 0x0, headers_only = 0 '\000', no_headers = 0 '\000', headers_read = 0 '\000', post_entry = 0x0, content_type_dup = 0x0, auth_user = 0x0, auth_password = 0x0, auth_digest = 0x0, argv0 = 0x0, current_user = 0x0, current_user_length = 0, argc = 0, argv = 0x0, proto_num = 1000 }
(gdb) print_ht executor_globals->symbol_table->pListHead [0x7f3edefbea90] { "_POST\0" => [0x7f3edefbeae8] (refcount=2) array(0): "_COOKIE\0" => [0x7f3edefbebc8] (refcount=2) array(0): "_FILES\0" => [0x7f3edefbeca8] (refcount=2) array(0): "_SERVER\0" => [0x7f3edefbee48] (refcount=2) array(27): "_REQUEST\0" => [0x7f3edefc1920] (refcount=1) array(1): }
ã»ãã·ã§ã³ã使ã£ã¦ãªãã®ã§ ps_globals.http_session_vars
ã¯åç
§ã§ããªãã®ããª
(gdb) printzv ps_globals.http_session_vars [(nil)] Cannot access memory at address 0x14
ã¨ãããããããªæãã
ã©ããã¸ãã®å¤æ°ãè¦ãè¦ãããããã¯ãç¶æ³ã«å¿ãã¦å¤ããã ãããGDB ã使ããã¨ã§ãã®ããã«ã¢ããã¼ããã§ãããã¨ãå¿ã«çãã¦ããã®ãèè¦ã¨æã
ææ³
- Web ãµã¼ãã¹ããã£ã¦ãã¨ãproductoin ãµã¼ã㧠ããã㨠CPU ã ã¡ã¢ãªãé£ãç¶ãã¦ãããã»ã¹ããããã ãã©ãã©ã®ã¡ã½ãããåãããªã ð² ãã®ãããªã±ã¼ã¹ã«ééããã ããããããããã¯ããã¯ãç¥ã£ã¦ããã¨èª¿æ»ã»åå ã®ç©¶æã«å½¹ã«ç«ã¤ã ãã
- å®éãããããã±ã¼ã¹ã®è§£æ±ºã«ç¨ããã®ã§ ããã°ã«ã¾ã¨ãã¦ãããã¨æã£ã次第