rootkit for linux 16.获取tcp可用端口号

我说我很愤怒

你说你理解我

我说 那不可能

你说真的 我说那我考考你

帮你爸一忙

你媳妇大肚子

傻*照镜子

 

你知道什么意思么

你微笑着说  不

我说 我来解释给你

他们是

 

我*你妈

装你MA孙子

瞅你*德行

 

现在 你理解我的愤怒了么?

 

                  ----摘自《活不明白》

 

 

每个人都有自己的愤怒。你有自己的愤怒吗?如果没有,去听听Woods Of Infinity、Burzum、Nargaroth你就明白了。

 

今天我想说的是,我们要用tcpip传输数据,怎么知道某个本地端口是否已经被内核使用了呢?

我们都知道用netstat命令可以看到所有socket的信息,我们现在不用看到所有socket的信息,只需要看到tcp socket的信息就行了。

strace netstat的记录表明,它通过读取/proc/net/tcp来获取相应的socket信息。

来看看/proc/net/tcp的格式:

 

  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                                    
   0: 0100A8C0:9CAF 0200A8C0:008B 01 00000000:00000000 00:00000000 00000000     0        0 9960 3 dba79b40 61 12 1 2 -1                             

看到没。有人一定会问,这是啥j8东西啊?

 

sl:socket编号

local_address:本地地址和端口号

rem_address:远程地址和端口号

st:tcp状态

enum {
 TCP_ESTABLISHED = 1,
 TCP_SYN_SENT,
 TCP_SYN_RECV,
 TCP_FIN_WAIT1,
 TCP_FIN_WAIT2,
 TCP_TIME_WAIT,
 TCP_CLOSE,
 TCP_CLOSE_WAIT,
 TCP_LAST_ACK,
 TCP_LISTEN,
 TCP_CLOSING, /* Now a valid state */

 TCP_MAX_STATES /* Leave at the end! */
};
tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode :这几个我们就别管了吧。

 

那好办,就直接读取这个文件就行了。

 

  1. // fastcall int tcp_get_port(int port);
  2. // @port: the port you want to bind
  3. // @return: if the port is avail, returns 1, otherwise 0.
  4. EXPORT_LABEL(tcp_get_port)
  5.     PUSH_ALL
  6.     subl $0x38, %esp
  7.     movl %eax, 0x2c(%esp)
  8.     movl $1024, %esi
  9.     
  10.     // cancal user check
  11.     movl %esp, %eax 
  12.     andl $0xffffe000, %eax 
  13.     movl 0x18(%eax), %ebx 
  14.     movl %ebx, 0x30(%esp)
  15.     movl $0xffffffff, 0x18(%eax)
  16. 2:
  17.     // buf = __kmalloc(size, GFP_KERNEL);
  18.     GET_VAL32(__kmalloc, %ebp)
  19.     movl %esi, %eax
  20.     movl $0xd0, %edx
  21.     call *%ebp
  22.     testl %eax, %eax
  23.     jz tcp_get_port_out
  24.     movl %eax, %edi
  25.     // file = filp_open("/proc/net/tcp", O_RDONLY, 0);
  26.     GET_VAL32(filp_open, %ebp)
  27.     GET_STR("/proc/net/tcp", %eax)
  28.     xorl %edx, %edx
  29.     xorl %ecx, %ecx
  30.     call *%ebp
  31.     testl %eax, %eax
  32.     jz tcp_get_port_out
  33.     movl %eax, 0x34(%esp)
  34.     // *pos = 0;
  35.     movl $0, 0x4(%esp)
  36.     movl $0, 0x8(%esp)
  37.     // ret = file->f_op->read(file, buf, size, pos);
  38.     movl 0x34(%esp), %edx
  39.     GET_STRUCT_VAL32(%edx, file.f_op, %ecx)
  40.     GET_STRUCT_VAL32(%ecx, file_operations.read, %ebp)
  41.     leal 0x4(%esp), %eax
  42.     movl %eax, (%esp)
  43.     movl %edx, %eax
  44.     movl %edi, %edx
  45.     movl %esi, %ecx
  46.     call *%ebp
  47.     // if (IS_ERR(ret)) goto out_restore;
  48.     testl %eax, %eax
  49.     js tcp_get_port_out_restore
  50.     
  51.     /* if (ret != count) 
  52.      *      // complete read
  53.      * else {
  54.      *      size *= 2;
  55.      *      // malloc and read again
  56.      * }
  57.      */
  58.     cmpl %eax, %esi
  59.     jnz 1f
  60.     // kfree(buf);
  61.     GET_VAL32(kfree, %ebp)
  62.     movl %edi, %eax
  63.     call *%ebp
  64.     // filp_close(file, 0);
  65.     GET_VAL32(filp_close, %ebp)
  66.     movl 0x34(%esp), %eax
  67.     xorl %edx, %edx
  68.     call *%ebp
  69.     shl $1, %esi
  70. #ifdef _DEBUG_
  71.     movl %esi, 0x4(%esp)
  72.     DPRINT("<3>try new size %x/n")
  73. #endif
  74.     
  75.     jmp 2b
  76. 1:  // read ok
  77.     movl %eax, %esi
  78.     // filp_close(file, 0);
  79.     GET_VAL32(filp_close, %ebp)
  80.     movl 0x34(%esp), %eax
  81.     xorl %edx, %edx
  82.     call *%ebp
  83.     
  84.     movl %esi, %ecx
  85.     movl %ecx, 0x34(%esp)
  86.     // skip the title
  87.     movb $'/n', %al
  88.     cld; repne scasb
  89.     incl %edi
  90.     movl %edi, %esi
  91. 2:
  92.     // find '/n' in content, replace it with '/0'
  93.     movb $'/n', %al
  94.     movl 0x34(%esp), %ecx
  95.     cld; repne scasb
  96.     jz 3f
  97.     
  98. #ifdef _DEBUG_
  99.     DPRINT("<3>read end/n")
  100. #endif
  101.     jmp tcp_get_port_out_restore
  102. 3:  movb $'/0', (%edi)
  103.     incl %edi
  104.     movl %ecx, 0x34(%esp)
  105.     // sscanf(line, "%x: %x:%x", &stack[0x14], &stack[0x18], &stack[0x1c]);
  106.     GET_VAL32(sscanf, %ebp)
  107.     movl %esi, (%esp)
  108.     GET_STR("%x: %x:%x", %eax)
  109.     movl %eax, 0x4(%esp)
  110.     leal 0x14(%esp), %eax
  111.     movl %eax, 0x8(%esp)
  112.     leal 0x18(%esp), %eax
  113.     movl %eax, 0xc(%esp)
  114.     leal 0x1c(%esp), %eax
  115.     movl %eax, 0x10(%esp)
  116.     call *%ebp
  117. #ifdef _DEBUG_
  118.     movl 0x14(%esp), %eax
  119.     movl %eax, 0x4(%esp)
  120.     movl 0x18(%esp), %eax
  121.     movl %eax, 0x8(%esp)
  122.     movl 0x1c(%esp), %eax
  123.     movl %eax, 0xc(%esp)
  124.     DPRINT("<3>sock:%x local_addr:%x local_port:%x/n")
  125. #endif
  126.     movl 0x1c(%esp), %eax
  127.     cmpl %eax, 0x2c(%esp)
  128.     jnz 3f
  129. #ifdef _DEBUG_
  130.     DPRINT("<3>port found/n")
  131. #endif
  132.     jmp tcp_get_port_out_restore
  133. 3:
  134.     // get next line
  135.     movl %edi, %esi
  136.     jmp 2b
  137. tcp_get_port_out_restore:
  138.     // restore user check
  139.     movl %esp, %ebx 
  140.     andl $0xffffe000, %ebx 
  141.     movl 0x30(%esp), %ecx 
  142.     movl %ecx, 0x18(%ebx)
  143. tcp_get_port_out:
  144.     addl $0x38, %esp
  145.     POP_ALL
  146.     ret
  147.     

其实今天的文章,也没什么技术含量,不过我还是要写,因为我现在tmd太无聊了。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值