在命令行中输入命令并不是一个好主意,会造成安全问题。但是如果你决定去写一个应用,而这个应用需要在命令行中使用密码或者其他敏感信息。那么,你能通过以下方法禁止系统的其他用户轻易的看到这些敏感数据 呢?,类似MySQL在ps命令下隐藏密码。
假设我这里系统里两个用户,一个是root ,一个是dabu 。测试系统为centos 6.5在按照下面的步骤做:
[root@dabu.info ~]#su dabu #切换到dabu这个账号 [dabu@dabu.info ~]$cd ~ #切换到dabu的home目录 [dabu@dabu.info ~]$ touch pwhide.c #创建 pwhide.c文件 [dabu@dabu.info ~]$ls
显示:
代码如下:
pwhide.c
将下面的代码保存到 pwhide.c :
#include <stdio.h> #include <unistd.h> /* unix类系统定义符号常量的头文件*/ #include <string.h> /* 字符数组的函数定义的头文件*/ #include <sys/types.h> /* Unix/Linux系统的基本系统数据类型的头文件*/ int main(int argc, char *argv[]) /*形参argc指命令行中参数的个数(包括执行文件本身)。形参argv是一个纸箱字符串的指针数组*/ { int i = 0; pid_t mypid = getpid(); /*获得该程序运行时候的pid*/ if (argc == 1) /*如果argc参数个数等于1,按要求,应该argc要为2才行*/ return 1; /*异常退出*/ printf("argc = %d and arguments are:\n", argc); /*打印argc参数个数*/ for (i ; i < argc ; i++) /*打印i序号,以及对应的argv数组指针元素*/ printf("%d = %s\n" ,i, argv[i]); /*打印i序号,以及对应的argv数组指针元素*/ printf("Replacing first argument with x:es... Now open another terminal and run: ps p %d\n", (int)mypid); /*打印该字符串和该程序是的pid*/ fflush(stdout); //*清空缓冲区,并打印其内容*/ memset(argv[1], 'x', strlen(argv[1])); /*注意,这里是本文的重点和关键点。(原文http://www.dabu.info/?p=5150)就是利用memset(void *s, int c, size_t n)函数用x来覆盖密码的每个字符*。你也可以将x替换为 a ,然后重新编译运行,再ps看看有什么不同/ getc(stdin); /* 等待并获取键盘输入,其实这里主要的作用是保持该c程序在 运行状态,这样才能通过ps 查看pid来观察密码是否被隐藏 。所以在这个函数运行后,不能再有任何的键盘操作 */ return 0; /* 正常退出 */ #include <stdio.h> #include <unistd.h> /* unix类系统定义符号常量的头文件*/ #include <string.h> /* 字符数组的函数定义的头文件*/ #include <sys/types.h> /* Unix/Linux系统的基本系统数据类型的头文件*/ int main(int argc, char *argv[]) /*形参argc指命令行中参数的个数(包括执行文件本身)。形参argv是一个纸箱字符串的指针数组*/ { int i = 0; pid_t mypid = getpid(); /*获得该程序运行时候的pid*/ if (argc == 1) /*如果argc参数个数等于1,按要求,应该argc要为2才行*/ return 1; /*异常退出*/ printf("argc = %d and arguments are:\n", argc); /*打印argc参数个数*/ for (i ; i < argc ; i++) /*打印i序号,以及对应的argv数组指针元素*/ printf("%d = %s\n" ,i, argv[i]); /*打印i序号,以及对应的argv数组指针元素*/ printf("Replacing first argument with x:es... Now open another terminal and run: ps p %d\n", (int)mypid); /*打印该字符串和该程序是的pid*/ fflush(stdout); //*清空缓冲区,并打印其内容*/ memset(argv[1], 'x', strlen(argv[1])); /*注意,这里是本文的重点和关键点。(原文http://www.dabu.info/?p=5150)就是利用memset(void *s, int c, size_t n)函数用x来覆盖密码的每个字符*。你也可以将x替换为 a ,然后重新编译运行,再ps看看有什么不同/ getc(stdin); /* 等待并获取键盘输入,其实这里主要的作用是保持该c程序在 运行状态,这样才能通过ps 查看pid来观察密码是否被隐藏 。所以在这个函数运行后,不能再有任何的键盘操作 */ return 0; /* 正常退出 */ }
然后编译 pwhide.c ,命令如下:
[dabu@dabu.info ~]$ gcc -o hide pwhide.c #编译后的文件叫 hide [dabu@dabu.info ~]$ ls
显示:
代码如下:
hide pwhide.c
用编译后的程序进行测试:
[dabu@dabu.info ~]$ ./hide dabu.info //dabu.info作为参数(其实就是密码) 进行测试 显示: argc = 2 and arguments are: 0 = ./hide 1 = dabu.info Replacing first argument with x:es... Now open another terminal and run: ps p 15585
注意:ps p 15585 。你可能和我的不一样,因为pid每次运行,都会变的。你显示什么数字,后面就用什么数字。
显示出上面结果后,不再进行任何操作,也不关闭这个终端窗口(命令窗口)。然后在用root账号登录,就是相当于同时开两个终端窗口。输入下面的命令:
[root@dabu.info ~]#ps p 15585 #就是运行 ./hide dabu.info后,得到的该程序的pid 显示: PID TTY STAT TIME COMMAND 15585 pts/0 S+ 0:00 ./hide xxxxxxxxx //dabu.info 共有9个字符,所以这里就显示9个x
由此测试的结果,我们知道了这个方法能够使MySQL如何在ps命令下隐藏命令行中的密码。以此类推,在写其他程序后,就知道如何使用这个方法来 让程序 在ps命令下隐藏命令行参数。
为了简明起见,上面的代码可能不怎么好移植到其他平台,但是它可以工作在linux上,并且如愿的表达了关键点。在其它环境,如FreeBSD,你可以使用系统调用setproctitle() 来为你做这种苦力活。关键的一点是重写argv

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
