Linux下使用python读取共享内存

python没有独立的库可以读取linux下的共享内存,下面使用ctypes调用系统的API读取共享内存的内容

使用C++创建共享内存

Cpp代码   收藏代码
  1. #include <stdio.h>  
  2. #include <iostream>  
  3. #include <unistd.h>    
  4. #include <sys/ipc.h>  
  5. #include <sys/shm.h>  
  6. #include <stdlib.h>  
  7. #include <errno.h>  
  8.   
  9. #define MY_SHM_ID 67483  
  10.   
  11. void get_buf(char *buf)  
  12. {  
  13.     int i=0;  
  14.     while((buf[i]=getchar())!='\n'&&i<1024)  
  15.         i++;  
  16. }  
  17.   
  18.   
  19. int main(  )  
  20. {  
  21.     printf("page size=%d\n", getpagesize());  
  22.     int shmid=0, ret=0;  
  23.     shmid = shmget(MY_SHM_ID, 4096, 0666|IPC_CREAT);  
  24.       
  25.     if (shmid > 0)  
  26.     {  
  27.         printf("Create a shared memory segment %d\n", shmid);  
  28.     }  
  29.     struct shmid_ds shmds;  
  30.     ret = shmctl( shmid, IPC_STAT, &shmds );  
  31.   
  32.     if (ret == 0 )  
  33.     {  
  34.         printf( "Size of memory segment is %d \n", shmds.shm_segsz );  
  35.         printf( "Number of attaches %d \n", (int)shmds.shm_nattch );  
  36.     }  
  37.     else  
  38.     {  
  39.         printf( "shmctl () call failed \n");  
  40.     }  
  41.       
  42.         // write data to share memary  
  43.         char *buf = NULL;  
  44.         if ((int)(buf=(char*)shmat(shmid, NULL, 0))==-1)  
  45.         {  
  46.             perror("Share memary can't get pointer\n");  
  47.                 exit(1);  
  48.         }  
  49.     get_buf(buf);  
  50.   
  51.   
  52.     //ret = shmctl(shmid, IPC_RMID, 0);  
  53.       
  54.     if (ret == 0)  
  55.     {  
  56.         printf("Shared memary removed \n");  
  57.     }  
  58.     else  
  59.     {  
  60.         printf("Shared memory remove failed \n");  
  61.     }  
  62.       
  63.     return 0;  
  64. }  

 

查看共享内存:

$ipcs

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x0001079b 98305      postmast   666        4096       0                      

------ Semaphore Arrays --------
key        semid      owner      perms      nsems    

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x000004d2 131073     abber      666        17           3  

 

 

使用python读取共享内存 代码如下:

Python代码   收藏代码
  1. [postmast@xuanyuan-soft22 ~/test]$vi shm.py  
  2.  #!/usr/bin/env python  
  3.  # -*- coding: utf-8 -*-  
  4.  #  
  5.  # This script dumps the content of a shared memory block  
  6.  # used by Linux/Cdorked.A into a file named httpd_cdorked_config.bin  
  7.  # when the machine is infected.  
  8.  #  
  9.  # Some of the data is encrypted. If your server is infected and you  
  10.  # would like to help, please send the httpd_cdorked_config.bin  
  11.  # to our lab for analysis. Thanks!  
  12.  #  
  13.  # Marc-Etienne M.Léveillé <[email protected]>  
  14.  #  
  15.    
  16.  from ctypes import *  
  17.    
  18.  SHM_SIZE = 4096  
  19.  SHM_KEY = 67483  
  20.    
  21.  OUTFILE="httpd_cdorked_config.bin"  
  22.    
  23.  try:  
  24.    rt = CDLL('librt.so')  
  25.  except:  
  26.    rt = CDLL('librt.so.1')  
  27.    
  28.  shmget = rt.shmget  
  29.  shmget.argtypes = [c_int, c_size_t, c_int]  
  30.  shmget.restype = c_int  
  31.  shmat = rt.shmat  
  32.  shmat.argtypes = [c_int, POINTER(c_void_p), c_int]  
  33.  shmat.restype = c_void_p  
  34.      
  35.  shmid = shmget(SHM_KEY, SHM_SIZE, 0o666)  
  36.  if shmid < 0:  
  37.    print ("System not infected")  
  38.  else:   
  39.    addr = shmat(shmid, None0)  
  40.    
  41.    #f = file(OUTFILE, 'wb')  
  42.    f=open(OUTFILE, 'wb')  
  43.    f.write(string_at(addr,SHM_SIZE))  
  44.    f.close()  
  45.    print(addr, type(addr))  
  46.  print ("Dumped %d bytes in %s" % (SHM_SIZE, OUTFILE))  
  47.    
  48.    

 python 读取的结果存放在文件httpd_cdorked_config.bin中

$cat httpd_cdorked_config.bin
hello word!this is a test.

$

猜你喜欢

转载自blog.csdn.net/amds123/article/details/80316781