C/C++知识点之如何在linux下阅读源码以及提取写简单demo
小标 2019-05-08 来源 : 阅读 2355 评论 0

摘要:本文主要向大家介绍了C/C++知识点之如何在linux下阅读源码以及提取写简单demo,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之如何在linux下阅读源码以及提取写简单demo,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

C/C++知识点之如何在linux下阅读源码以及提取写简单demo

//如何在linux下阅读源码以及提取写demo
这里以 ps 为例
用到的工具有 clion
先查看 ps 路径
which

root@ubuntu:~# which ps/bin/ps
root@ubuntu:~#

查看源码包
dpkg

root@ubuntu:~# dpkg -S /bin/psprocps: /bin/ps
root@ubuntu:~#

使用 apt-get 下载(这里可能会报xxxxxxxx  cannot be authenticated.  更新下就好 sudo apt-key update  sudo apt-get update)
(它会下载很多东西 只关心源码文件就好这里是procps-3.3.9)

root@ubuntu:~# dpkg -S /bin/psprocps: /bin/psroot@ubuntu:~# lsClionProjects  Documents  manpages-zh-1.5.1         Music     Public     Videos
Desktop        Downloads  manpages-zh-1.5.1.tar.gz  Pictures  Templatesroot@ubuntu:~# cd Desktop/root@ubuntu:~/Desktop# lsroot@ubuntu:~/Desktop# apt-get source procpsReading package lists... Done
Building dependency tree       
Reading state information... DoneNOTICE: 'procps' packaging is maintained in the 'Git' version control system at:git://git.debian.org/collab-maint/procps.gitNeed to get 612 kB of source archives.Get:1 //us.archive.ubuntu.com/ubuntu/ trusty-updates/main procps 1:3.3.9-1ubuntu2.3 (dsc) [2,164 B]Get:2 //us.archive.ubuntu.com/ubuntu/ trusty-updates/main procps 1:3.3.9-1ubuntu2.3 (tar) [561 kB]Get:3 //us.archive.ubuntu.com/ubuntu/ trusty-updates/main procps 1:3.3.9-1ubuntu2.3 (diff) [49.1 kB]Fetched 612 kB in 46s (13.3 kB/s)gpgv: Signature made 2018年05月14日 星期一 05时38分54秒 PDT using RSA key ID A744BE93gpgv: Can't check signature: public key not founddpkg-source: warning: failed to verify signature on ./procps_3.3.9-1ubuntu2.3.dscdpkg-source: info: extracting procps in procps-3.3.9dpkg-source: info: unpacking procps_3.3.9.orig.tar.xzdpkg-source: info: unpacking procps_3.3.9-1ubuntu2.3.debian.tar.gzdpkg-source: info: applying uptime_testdpkg-source: info: applying ignore_eaccess.patchdpkg-source: info: applying testsuite_unsuppdpkg-source: info: applying pmap_testdpkg-source: info: applying libtool-update.diffdpkg-source: info: applying /p_pid-enum.diffdpkg-source: info: applying ignore_erofs.patchdpkg-source: info: applying CVE-2018-1122.patchdpkg-source: info: applying CVE-2018-1123.patchdpkg-source: info: applying CVE-2018-1124.patchdpkg-source: info: applying CVE-2018-1125.patchdpkg-source: info: applying CVE-2018-1126.patchdpkg-source: info: applying pmap_new_kernel.patchroot@ubuntu:~/Desktop#

打开 clion 导入项目(注册可以用远程服务器//idea.imsxm.com)
在 clion 里就可以搜索指定目录下所有文件中的字符串了(跟代码审计一样)

可以全选也可以默认

点击项目

可以直接 ok查看 或者看预览(preview)

找到 ps 真正的 main 函数:
其它用法就是clion 的用法了
如:可以查看函数、变量被谁引用了等等

这里就直接给出我随便提取的 demo(功能有 遍历进程 与特定进程的内存)

#include <iostream>#include <cstring>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <dirent.h>#include <sys/ptrace.h>#include <wait.h>void test1(){    // alt + enter  排错    // ctrl + q     查看文档
    FILE* fp = fopen(""hello.txt"", ""a+"");    if(fp == NULL){        std::cout << ""open file error"" << std::endl;        return;
    }    char szBuf[100] = {""hello world!""};
    fwrite(szBuf,strlen(szBuf),1,fp);

    fclose(fp);
}void test2(){    // alt + enter  排错    // ctrl + q     查看文档
    FILE* fp = fopen(""hello.txt"", ""r+"");    if(fp == NULL){        std::cout << ""open file error"" << std::endl;        return;
    }    int nFileSize = 0;    struct stat stcFileInfo = {0};
    stat(""hello.txt"", &stcFileInfo);
    nFileSize = stcFileInfo.st_size;    char szBuf[100] = {};
    fread(szBuf,nFileSize,1,fp);

    fclose(fp);    printf(""read content : %s "",szBuf);

}void test3(){    // 1. open file    int fd = open(""hello1.txt"",
                  O_WRONLY | O_CREAT,
                  S_IRWXU | S_IRWXG | S_IRWXO);    if(fd == -1){
        perror(""open error"");        return;
    }    // 2. write file    char szBuf[100] = {""hello world!""};    int nRet = write(fd, szBuf, strlen(szBuf));    if(nRet == -1){
        perror(""write error"");        return;
    }    // 3. close file
    close(fd);

}void test4(){    // 1. open file    int fd = open(""hello1.txt"",
                  O_RDONLY);    if(fd == -1){
        perror(""open error"");        return;
    }    // 2. write file    int nFileSize = 0;    struct stat stcFileInfo = {0};
    stat(""hello.txt"", &stcFileInfo);
    nFileSize = stcFileInfo.st_size;    char szBuf[100] = {0};    int nRet = read(fd, szBuf, nFileSize);    if(nRet == -1){
        perror(""read error"");        return;
    }    // 3. close file
    close(fd);    printf(""read content 1 : %s "",szBuf);

}void enumProcess(){    struct dirent *ent;          /* dirent handle */
    DIR *dir;    int ouruid;    int found_a_proc;
    found_a_proc = 0;
    ouruid = getuid();
    dir = opendir(""/proc"");    while(( ent = readdir(dir) )){        if(*ent->d_name<'0' || *ent->d_name>'9') continue;        int pid = atoi(ent->d_name);        char p_cmd[16] = {0};        char buf[800]; /* about 40 fields, 64-bit decimal is about 20 chars */        int num;        int fd;        char* tmp;        struct stat sb; /* stat() used to get EUID */        snprintf(buf, 32, ""/proc/%d/stat"", pid);        if ( (fd = open(buf, O_RDONLY, 0) ) == -1 ) return;
        num = read(fd, buf, sizeof buf - 1);
        fstat(fd, &sb);
        close(fd);
        buf[num] = '\0';
        tmp = strrchr(buf, ')');      /* split into ""PID (cmd"" and ""<rest>"" */
        *tmp = '\0';                  /* replace trailing ')' with NUL */        sscanf(buf, ""%d (%15c"", &pid, p_cmd);  /* comm[16] in kernel */        printf(""pid = %d name = %s \r\n"", pid, p_cmd);
    }
    closedir(dir);
}void show_info(const char* buf, int size) {    int count = 0;    while(count < size){        char ch = buf[count++];        char show[10] = {0};        sprintf(show, ""%02x "", (u_char)ch);        printf(""%s"",show);        if ((count % 16) == 0){            printf(""\r\n"");
        }
    }
}void readProcessMem(int pid){    // 1. attach process    long lRet = ptrace(PTRACE_ATTACH, pid,NULL,NULL);    if(lRet == -1){
        perror(""ptrace err"");        return;
    }    // 2. wait pid    int stat = 0;    __pid_t pid1 = waitpid(pid, &stat, 0);    if(pid1 == -1){
        perror(""waitpid err"");        return;
    }    // 3. open mem    char szBuf[100]  ={0};    sprintf(szBuf, ""/proc/%d/mem"", pid);    int fd = open(szBuf,O_RDONLY);    if(fd == -1){
        perror(""open err"");        return;
    }    // 4. lseek pos    int nRet = lseek(fd, 0x55c266915000, SEEK_SET);    if(nRet == -1){
        perror(""lseek err"");        return;
    }    // 5. read mem    char buf[0x200] = {0};
    nRet = read(fd, buf, 200);    if(nRet == -1){
        perror(""read err"");        return;
    }    // 6. close
    close(fd);
    ptrace(PTRACE_DETACH, pid, 0,0);    // 7, show
    show_info(buf, 200);

}int main() {

    enumProcess();    int pid = 0;    printf(""please input pid :"");    scanf(""%d"", &pid);

    readProcessMem(pid);

    getchar();    return 0;
}

问题1:C/C++知识点之如何在linux下阅读源码以及提取写简单demo

sudo sed -i -- 's/#deb-src/deb-src/g' /etc/apt/sources.list && sudo sed -i -- 
's/# deb-src/deb-src/g' /etc/apt/sources.list

问题2:C/C++知识点之如何在linux下阅读源码以及提取写简单demo

或者换软件源

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved