首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Linux 命令列隱藏 MySQL 密碼實作:C 語言 memset 覆蓋 argv[1] 防 ps 洩露、getc 保持進程與免 setproctitle 替代方案

     編輯:MySQL 資料庫相關知識

Linux 命令列隱藏 MySQL 密碼實作指南


問題背景

命令列傳遞密碼會被 ps 指令看到,造成安全風險。MySQL 透過覆寫 argv 實現隱藏。


實作原理 (C 語言)

  • 程式啟動時密碼在 argv[1]
  • 使用 memset(argv[1], 'x', strlen(argv[1])) 將密碼字元逐一覆蓋為 'x'
  • getc(stdin) 阻塞進程,保持運行狀態供 ps 觀察

完整程式碼 (pwhide.c)

#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

跨平台替代方案

  • FreeBSD/macOS:使用系統呼叫 setproctitle()
  • 核心關鍵:重寫 argv 讓 ps 看到偽裝參數

此技術不僅適用 MySQL,所有命令列工具均可參考實現敏感參數隱藏。


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved