命令列傳遞密碼會被 ps 指令看到,造成安全風險。MySQL 透過覆寫 argv 實現隱藏。
argv[1]memset(argv[1], 'x', strlen(argv[1])) 將密碼字元逐一覆蓋為 'x'getc(stdin) 阻塞進程,保持運行狀態供 ps 觀察#include
#include
#include
#include
int main(int argc, char *argv[]) {
int i = 0;
pid_t mypid = getpid();
if (argc == 1) return 1;
printf("argc = %d and arguments are:\n", argc);
for (i = 0; i < argc; i++)
printf("%d = %s\n", i, argv[i]);
printf("Replacing first argument with x's... Now open another terminal and run: ps p %d\n", (int)mypid);
fflush(stdout);
memset(argv[1], 'x', strlen(argv[1])); // 核心:覆蓋密碼
getc(stdin); // 阻塞,保持進程
return 0;
}
$ gcc -o hide pwhide.c
$ ./hide dabu.info
argc = 2 and arguments are:
0 = ./hide
1 = dabu.info
Replacing first argument with x's... Now open another terminal and run: ps p 15585
$ ps p 15585
PID TTY STAT TIME COMMAND
15585 pts/0 S+ 0:00 ./hide xxxxxxxxx # 9個字元全變 x
setproctitle()此技術不僅適用 MySQL,所有命令列工具均可參考實現敏感參數隱藏。