[완료]mips에서 system call hooking 관련 문의 입니다.

kwonsu의 이미지

안녕하세요

mips에서 커널 모듈을 이용하여 system call hooking을 해보는 중입니다.

x86에서는 실행된 코드를 mips에서 실행해 보았으나 실행되지 않는것을 확인했고

원인을 확인 중에 x86에서 사용하는 Enable/Disable page protection 부분에 문제가

있는 것을 확인하였습니다.

여러 사이트를 확인해보았으나 mips에서 page protection 하는 부분이나 이와 비슷한 기능을 하는 코드를

확인 할 수 없었습니다.

system call table는 System.map 파일로 확인하였고 system call hooking 프로그램 코드는 인터넷에

있는 여러 소스코드를을 대상으로 테스트를 하였습니다.

테스트 환경은 qemu로 구성하였고 아래와 같습니다.

1. Host PC
ubuntu 16.04 LTS

2. Guest PC
CPU : MIPS 24Kc V0.0 FPU V0.0
OS : debian-9.9.0-mips
kernel : 4.9.0-9-4kc-malta

x86은 page protection 처리 부분은 아래와 같은 것을 이용하여 한다는 것을 확인하였습니다.

 12 #define PROT_DISABLE write_cr0(read_cr0() & (~ 0x10000))
 13 #define PROT_ENABLE write_cr0(read_cr0() | 0x10000)
...
void set_addr_rw(unsigned long addr) {
 
    unsigned int level;
    pte_t *pte = lookup_address(addr, &level);
 
    if (pte->pte &~ _PAGE_RW) pte->pte |= _PAGE_RW;
 
}
 
void set_addr_ro(unsigned long addr) {
 
    unsigned int level;
    pte_t *pte = lookup_address(addr, &level);
 
    pte->pte = pte->pte &~_PAGE_RW;
 
}
...
int set_page_rw(long unsigned int _addr)
{
    return set_memory_rw(PAGE_ALIGN(_addr) - PAGE_SIZE, 1);
}
 
int set_page_ro(long unsigned int _addr)
{
    return set_memory_ro(PAGE_ALIGN(_addr) - PAGE_SIZE, 1);
}

예제 코드나 참고 사이트 및 기타 도움을 부탁드립니다.

감사합니다.

kwonsu의 이미지

mips에서는 컨트롤 레지스트리를 제어하는 함수는 존재하지 않는것 같고 래지스트리 자체를 제어하려면 어셈블리를

해야 하는것으로 보입니다.

#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
 
void **system_call_table_address = (void *)0x<system call table>;
 
asmlinkage int (*orig_read)(int fd, void *buf, size_t count);
asmlinkage int hook_read(int fd, void *buf, size_t count)
{
    printk("read........................\n");
    return orig_read(fd, buf, count);
}
 
static int __init stest_init(void)
{
 
    orig_read = (void *)system_call_table_address[__NR_read - __NR_Linux];
    system_call_table_address[__NR_read - __NR_Linux ] = hook_read;
 
    return 0;
 
}
 
static void __exit stest_cleanup(void)
{
 
    system_call_table_address[__NR_read - __NR_Linux] = orig_read;
    system_call_table_address[__NR_read - __NR_Linux ] = hook_read;
 
    return 0;
 
}
 
static void __exit stest_cleanup(void)
{
 
    system_call_table_address[__NR_read - __NR_Linux] = orig_read;
 
}
 
module_init(stest_init);
module_exit(stest_cleanup);
MODULE_LICENSE("GPL");

코드를 보시면 system call table의 read에 접근 하려면 
__NR_read - __NR_Linux

위와 같은 방법으로 접근을 해야 합니다.

그 근거로 mips의 define된 system call table의 내용은 아래와 같습니다.

   1 /*
   2  * This file is subject to the terms and conditions of the GNU General Public
   3  * License.  See the file "COPYING" in the main directory of this archive
   4  * for more details.
   5  *
   6  * Copyright (C) 1995, 96, 97, 98, 99, 2000 by Ralf Baechle
   7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
   8  *
   9  * Changed system calls macros _syscall5 - _syscall7 to push args 5 to 7 onto
  10  * the stack. Robin Farine for ACN S.A, Copyright (C) 1996 by ACN S.A
  11  */
  12 #ifndef _ASM_UNISTD_H
  13 #define _ASM_UNISTD_H
  14
  15 #include <asm/sgidefs.h>
  16
  17 #if _MIPS_SIM == _MIPS_SIM_ABI32
  18
  19 /*
  20  * Linux o32 style syscalls are in the range from 4000 to 4999.
  21  */
  22 #define __NR_Linux          4000
  23 #define __NR_syscall        (__NR_Linux +   0)
  24 #define __NR_exit           (__NR_Linux +   1)
  25 #define __NR_fork           (__NR_Linux +   2)
  26 #define __NR_read           (__NR_Linux +   3)
......

파일의 경로는 /usr/include/mips-linux-gnu/asm/unistd.h 입니다.

system call table의 번호는 결과적으로 같겠습니다면 접근 방식 자체가 다른것으로 보입니다.

어떤분들에게 도움이 되셨으면 합니다.

지식의 여인은 옷을 쉽게 벗지 않는다.
잡초인생. 잡초처럼 끈길기게....

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.