How to read the source code under Linux and extract and write simple demo

Source: Internet
Author: User
Tags close close sprintf version control system cve

How to read the source code under Linux and extract and write demo
Here, take PS as an example
The tools used are clion.
See the PS Path First
which

View Source Package
Dpkg

Use Apt-get download (this may be reported xxxxxxxx cannot be authenticated. Updated under good sudo apt-key update sudo apt-get update)
(It will download a lot of things only care about the source file is good here is procps-3.3.9)

[email protected]:~# dpkg-s/bin/psprocps:/bin/ps[email protected]:~# lsClionProjects Documents manpages-zh-1.5.1 Music public videosdesktop Downloads manpages-zh-1.5.1.tar.gz Pictures Templat es[email protected]:~# CD desktop/[email protected]:~/desktop# ls[email protected]:~/desktop# Apt-get Source procpsreading Package lists ... Donebuilding Dependency Tree Reading state information ... Donenotice: ' Procps ' packaging is maintained in the ' Git ' version control system At:git://git.debian.org/collab-maint/pro Cps.gitneed to get 612 KB of source archives. Get:1 Http://us.archive.ubuntu.com/ubuntu/trusty-updates/main Procps 1:3.3.9-1ubuntu2.3 (DSC) [2,164 B]get:2/HTTP/ Us.archive.ubuntu.com/ubuntu/trusty-updates/main Procps 1:3.3.9-1ubuntu2.3 (TAR) [561 kB]Get:3/HTTP 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 May 14, 2018 Monday 05:38 54 sec. PDT using RSA key ID a744be93gpgv:can ' t check signature:public key not founddpkg-source:warning:failed to Ver Ify 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.patch[email protected]:~/desktop#


Open Clion Import Project (registration can be used with Remote server http://idea.imsxm.com)
In Clion, you can search for strings in all files in the specified directory (as with code audits)


Can be selected All or default

Click Project

Direct OK to view or preview

Find the PS real main function:
The other usage is the use of clion.
Such as: can see the function, the variable is referenced by whom and so on


Here, I'm going to give you the demo that I randomly extracted (features a traversal process and memory for a particular process)

#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 row    Error//CTRL + Q View document 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 debug//CTRL + Q View document 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-64-bit decimal is about chars */int num;        int FD;        char* tmp; struct STAT sb;        /* STAT () used to get Euid */snprintf (buf, +, "/proc/%d/stat", PID);        if (fd = open (buf, o_rdonly, 0)) = =-1) return;        num = Read (FD, buf, sizeof buf-1);        Fstat (FD, &AMP;SB);        Close (FD);        Buf[num] = ' + ';      TMP = STRRCHR (buf, ') ');                  /* split into "PID (cmd" and "<rest>" */*tmp = ' + ';  /* 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%) = = 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;}

How to read the source code under Linux and extract and write a simple demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.