修改进程名字

每一个c程序都有个main函数,作为程序启动入口函数。main函数的原型是int main(int argc , char *argv[]);其中argc表示命令行参数的个数;argv是一个指针数组,保存所有命令行字符串。Linux进程名称是通过命令行参数argv[0]来表示的。

Linux 还有环境变量参数信息,表示进程执行需要的所有环境变量信息。通过全局变量

Char **environ;可以访问环境变量。

命令行参数argv和环境变量信息environ是在一块连续的内存中表示的,并且environ紧跟在argv后面。如下图:

 

1.2    验证argv和environ执行连续内存的测试程序

[cpp]  view plain copy
  1. 1 #include <stdio.h>  
  2.   2 #include <string.h>  
  3.   3  
  4.   4 extern char **environ;  
  5.   5 int main(int argc , char *argv[])  
  6.   6 {  
  7.   7    int i;  
  8.   8  
  9.   9    printf("argc:%d\n" , argc);  
  10.  10  
  11.  11    for (i = 0; i < argc; ++i){  
  12.  12        printf("%x\n" , argv[i]);  
  13.  13        printf("argv[%d]:%s\n" , i , argv[i]);  
  14.  14     }  
  15.  15  
  16.        for (i = 0; evrion[i]; i ++)
  17.  16     printf("evriron=%x %s\n" , environ[i], environ[i]);  
  18.  17  
  19.  18    return 0;  
  20.  19 }  

 

 

root@VM-Ubuntu203001:~/test#gcc -o test main.c

root@VM-Ubuntu203001:~/test#./test -p /usr/local/nginx -n 32

argc:5

bfa0c8f2

argv[0]:./test

bfa0c8f9

argv[1]:-p

bfa0c8fc

argv[2]:/usr/local/nginx

bfa0c90d

argv[3]:-n

bfa0c910

argv[4]:32

evriron=bfa0c913

1.3    修改进程名称

按理说,修改进程名称,只需要修改argv[0]指向的内存的值为所需要的值即可。但是当我们要修改的值超过argv[0]所指向的内存空间大小时,再这样直接修改,就会覆盖掉一部分argv[1]的值,因为从上面的图中,很容易就可以看出。

这时候,该怎么做呢?

1、必须重新分配一块连续的内存空间,把argv和environ的参数都复制到新的空间。

2、修改argv[0]为所需要修改的值。

1.4    Nginx的做法

[cpp]  view plain copy
  1.  * To change the process title in Linux andSolaris we have to set argv[1]  
  2.  * to NULL and to copy the title to the sameplace where the argv[0] points to.  
  3.  * However, argv[0] may be too small to hold anew title.  Fortunately, Linux  
  4.  * and Solaris store argv[] and environ[] oneafter another.  So we should  
  5.  * ensure that is the continuous memory andthen we allocate the new memory  
  6.  * for environ[] and copy it.  After this we could use the memory starting  
  7.  * from argv[0] for our process title.  
  8.  *  
  9.  * The Solaris's standard /bin/ps does not showthe changed process title.  
  10.  * You have to use "/usr/ucb/ps -w"instead.  Besides, the UCB ps does not  
  11.  * show a new title if its length less than theorigin command line length.  
  12.  * To avoid it we append to a new title theorigin command line in the  
  13.  * parenthesis.  
  14.  */  
  15.    
  16. extern char **environ;  
  17.    
  18. static char *ngx_os_argv_last;  
  19.    
  20. ngx_int_t  
  21. ngx_init_setproctitle(ngx_log_t *log)  
  22. {  
  23.     u_char      *p;  
  24.     size_t       size;  
  25.     ngx_uint_t   i;  
  26.    
  27.     size = 0;  
  28.    
  29.     for (i = 0; environ[i]; i++) {  
  30.         size+= ngx_strlen(environ[i]) + 1;  
  31.     }  
  32.    
  33.     p = ngx_alloc(size, log);  
  34.     if (p == NULL) {  
  35.         return NGX_ERROR;  
  36.     }  
  37.    
  38.    /* 
  39.    这是为了找出argv和environ指向连续内存空间结尾的位置,为了能处理argv[i]被修改后,指向非进程启动时所分配的连续内存,而采用了下面的算法。但是实际上,这样还是处理不了这种情况。仅仅是个人愚见!!! 
  40.    */  
  41.     ngx_os_argv_last= ngx_os_argv[0];  
  42.    
  43.     for (i = 0; ngx_os_argv[i]; i++) {  
  44.         if (ngx_os_argv_last == ngx_os_argv[i]) {  
  45.             ngx_os_argv_last= ngx_os_argv[i]+ ngx_strlen(ngx_os_argv[i]) + 1;  
  46.         }  
  47.     }  
  48.    
  49.     for (i = 0; environ[i]; i++) {  
  50.         if (ngx_os_argv_last == environ[i]) {  
  51.    
  52.             size= ngx_strlen(environ[i]) + 1;  
  53.             ngx_os_argv_last= environ[i]+ size;  
  54.    
  55.             ngx_cpystrn(p, (u_char *) environ[i], size);  
  56.             environ[i] = (char *) p;  
  57.             p+= size;  
  58.         }  
  59.     }  
  60.    
  61.     ngx_os_argv_last--;  
  62.    
  63.     return NGX_OK;  
  64. }  
  65.    
  66.    
  67. void  
  68. ngx_setproctitle(char *title)  
  69. {  
  70.     u_char     *p;  
  71.    
  72. #if (NGX_SOLARIS)  
  73.    
  74.     ngx_int_t   i;  
  75.     size_t      size;  
  76.    
  77. #endif  
  78.    
  79.     ngx_os_argv[1]= NULL;  
  80.    
  81.     p = ngx_cpystrn((u_char*) ngx_os_argv[0], (u_char*) "nginx: ",  
  82.                     ngx_os_argv_last- ngx_os_argv[0]);  
  83.    
  84.     p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char*) p);  
  85.    
  86. #if (NGX_SOLARIS)  
  87.    
  88.     size = 0;  
  89.    
  90.     for (i = 0; i < ngx_argc; i++) {  
  91.         size+= ngx_strlen(ngx_argv[i]) + 1;  
  92.     }  
  93.    
  94.     if (size > (size_t)((char *) p - ngx_os_argv[0])) {  
  95.    
  96.         /* 
  97.          * ngx_setproctitle() is too rareoperation so we use 
  98.          * the non-optimized copies 
  99.          */  
  100.    
  101.         p = ngx_cpystrn(p, (u_char *) " (",ngx_os_argv_last - (char*) p);  
  102.    
  103.         for (i = 0; i < ngx_argc; i++) {  
  104.             p= ngx_cpystrn(p,(u_char *) ngx_argv[i],  
  105.                             ngx_os_argv_last - (char*) p);  
  106.             p= ngx_cpystrn(p,(u_char *) "", ngx_os_argv_last - (char *) p);  
  107.         }  
  108.    
  109.         if (*(p - 1) == ' ') {  
  110.             *(p- 1) = ')';  
  111.         }  
  112.     }  
  113.    
  114. #endif  
  115.    
  116.     if (ngx_os_argv_last - (char*) p) {  
  117.         ngx_memset(p, NGX_SETPROCTITLE_PAD,ngx_os_argv_last - (char*) p);  
  118.     }  
  119.    
  120.     ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,  
  121.                    "setproctitle:\"%s\"", ngx_os_argv[0]);  
  122. }  
### 回答1: 隐藏进程是一种隐藏计算机系统中正在运行的进程的方法,目的是为了防止被探测和关闭。下面是一些可能的思路来隐藏进程: 1. 修改进程名:通过修改进程名字,使其在任务管理器或进程监控工具中变得不容易被察觉和发现。可以通过修改进程名字的方式,使其与系统中其他普通进程的名称相似或者更改为一些无害的进程名称,以此来隐藏进程的存在。 2. 修改进程属性:通过修改进程的属性,可以将其设置为隐藏或系统进程,并且不在任务管理器等工具中显示。这样做可以使进程不容易被察觉,保护进程免受关闭的风险。 3. 加载驱动程序:通过加载驱动程序来隐藏进程,驱动程序可以通过修改操作系统的内核或者系统调用,来隐藏正在运行的进程。这种方法相对复杂,需要对操作系统内部有深刻的理解。 4. 使用Rootkit技术:Rootkit是一种恶意软件,可以修改操作系统的核心组件,从而隐藏恶意进程。它可以修改系统API,使得在进程列表中看不到指定的进程。Rootkit技术风险较高,但也是一种隐藏进程的方法。 需要注意的是,隐藏进程可能违反计算机安全政策和法律法规,因此在使用这些方法之前,需要确保自己的行为合法合规,遵守计算机安全规范和法律法规。 ### 回答2: 隐藏进程是指将某个进程在系统中不显示或屏蔽起来,使其在任务管理器或进程列表中不可见。以下是隐藏进程的一些思路: 1. 修改进程名:通过修改进程名可以使其在任务管理器或进程列表中显示为其他名称,从而隐藏起来。可以使用专门的工具或编程技巧来实现。 2. 修改进程属性:通过修改进程的属性,如PID (Process ID) 或进程描述符(Process Descriptor)可以使其不被系统所识别,从而实现隐藏进程的效果。 3. 使用Rootkit技术:Rootkit是一种恶意软件,可以对操作系统核心进行修改,从而隐藏进程并在系统中留下后门。它可以修改系统函数、Hook系统调用等来实现对进程的隐藏。 4. 修改注册表或系统配置文件:通过修改注册表或系统配置文件,可以使系统无法检测到特定进程的存在,从而达到隐藏进程的目的。 5. 使用虚拟化技术:通过在虚拟环境中运行进程,可以隐藏其在宿主系统中的存在。虚拟化技术可以创建一个隔离的环境,让进程在其中运行,不会在宿主系统中留下痕迹。 需要注意的是,隐藏进程并非一种良性的行为,通常被恶意软件或黑客用来逃避检测、传播病毒或进行攻击等不法目的。对于正常的系统管理和安全防护来说,隐藏进程是一种不可接受的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值