簡體   English   中英

stat.h 文件訪問文件描述符 open() 黑客攻擊的藝術

[英]stat.h file access file descriptors open() Hacking The Art of Exploitation

我正在使用 VM (virutalbox) 運行它附帶的 LiveCD (Ubuntu 7.04) 來編寫 Jon Erickson 的“黑客:剝削的藝術”的第 2 版。 在第 0x281 節“文件訪問”中,作者使用第 82-84 頁的示例解釋了通過文件描述符訪問文件,以及 open() close() read() 和 write() 函數。

simplenote.c的代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

void usage(char *prog_name,char *filename){
        printf("Usage: %s < data to add to %s>\n",prog_name,filename);
        exit(0);
}

void fatal(char *);
void *ec_malloc(unsigned int );

int main(int argc,char *argv[]){
        int fd; //file descriptor
        char *buffer,*datafile;

        buffer = (char *)ec_malloc(100);
        datafile = (char *)ec_malloc(20);
        strcpy(datafile,"/tmp/notes");

        if(argc < 2)
                usage(argv[0],datafile);

        strcpy(buffer,argv[1]);

        printf("[DEBUG] buffer   @ %p:\'%s'\n",buffer,buffer);
        printf("[DEBUG] datafile @ %p:\'%s'\n",datafile,datafile);

        strncat(buffer,"\n",1);//Add a newline on the end.

        fd = open(datafile,O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
        if(fd == -1)
                fatal("in main() while opening file");
        printf("[DEBUG] file descriptor is %d\n",fd);
        //Writing data
        if(write(fd,buffer,strlen(buffer)) == -1)
                fatal("in main() while writing buffer to file");
        //Closing file
        if(close(fd) == -1)
                fatal("in main() while closing file");

        printf("Note has been saved.\n");
        free(buffer);
        free(datafile);
}

//A function to display an error message and then exit
void fatal(char *message){
        char error_message[100];

        strcpy(error_message,"[!!]Fatal Error");
        strncat(error_message,message,83);
        perror(error_message);
        exit(-1);
}

//An error-checked malloc() wrapper function 
void *ec_malloc(unsigned int size){
        void *ptr;
        ptr = malloc(size);
        if(ptr == NULL)
                fatal("in ec_malloc() on memory allocation");
        return ptr;
}

但是,當我將書中所述的以下說明輸入我的終端 window 時,它返回以下錯誤消息:

reader@hacking:~/booksrc $ gcc -o simplenote simplenote.c
In file included from /usr/include/sys/stat.h:105, from simplenote.c:6:
/usr/include/bits/stat.h:70: error: field 'st_atim' has incomplete type
/usr/include/bits/stat.h:71: error: field 'st_mtim' has incomplete type
/usr/include/bits/stat.h:72: error: field 'st_ctim' has incomplete type
simplenote.c: In function 'main':
simplenote.c:35: error: 'O-WRONLY' undeclared (first use in this function)
simplenote.c:35: error: (Each undeclared identifier is reported only once
simplenote.c:35: error: for each function it appears in.)
simplenote.c:35: error: 'O_CREAT' undeclared (first use in this function)
simplenote.c:35: error: 'O_APPEND' undeclared (first use in this function)

這是 sys/stat.h 第 105 行:

#include <bits/stat.h>

這是 bits/stat.h 第 63-83 行:

#ifdef __USE_MISC
    /* Nanosecond resolution timestamps are stored in a format 
       equivalent to 'struct timespec'. This is the type used 
       whenever possible but the Unix namespace rules do not allow the 
       identifier 'timespec' to appear in the <sys/stat.h> header. 
       Therefore we have to handle the use of this header in strictly 
       standard-compliant sources special. */
    struct timespec st_atim;    /* Time of last access. */
    struct timespec st_mtim;    /* Time of last modification. */
    struct timespec st_ctim;    /* Time of last status change. */

# define st_atime st_atim.tv_sec    /* Backward compatibility */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
    __time_t st_atime;                 /* Time of last access. */
    unsigned long int st_atimensec;    /* Nscecs of last access. */
    __time_t st_mtime;                 /* Time of last modification. */
    unsigned long int st_mtimensec;    /* Nsecs of last modification. */
    __time_t st_ctime;                 /* Time of last status change. */
    unsigned long int st_ctimensec;    /* Nsecs of last status change. */
#endif

我想這可能對第一組問題有用:

C++ 系統文件 bits/stat.h 突然中斷並顯示“錯誤:字段‘st_atim’的類型不完整”

/usr/include/time.h

cat time.h

在我的終端 window 中沒有執行任何操作。

這是 simplenote.c main function 第 1-6、34-35 行:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

// Opening the file
    fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);

我猜開放 function 問題源於 fcntl.h?

由於作者提供的錯誤代碼,我似乎一直遇到問題。 我不想一直依賴 stackoverflow 社區的幫助,那么您對新手將來檢查和修復這些問題有什么建議?

謝謝。

將選擇的評論轉換為半連貫的答案。

您可能應該顯式啟用 POSIX 定義。 -D_XOPEN_SOURCE=700添加到命令行,或在第一個#include之前添加#define _XOPEN_SOURCE 700 ,看看是否能解決任何問題。 不過,您不應該遇到問題; header 應該是獨立的。

哦,但是 Ubuntu 7.04 已經過時了……您可能需要使用 600 而不是 700。它是什么時候發布的(這本書是什么時候出版的)? 如果是 2009 年或更早版本,您可能需要舊版本 (600)。 您看到錯誤仍然令人驚訝。 您指定的命令行不包含通常會引起麻煩的選項(例如-ansi -pedantic-std=c99 -pedantic )。 您也可以嘗試使用-std=gnu99 它可能會更好。

您最近遇到了類似的問題( gcc -o stdlib.h syntax error c Hacking the Art of Exploitation )。 你解決了嗎? 聽起來好像 Live CD 上的編譯系統不是自連貫的,或者您能夠使用它的方式意味着它不是自連貫的。 你確定編譯系統有效嗎? 它似乎是半廢棄的。 它是否以某種方式使用了錯誤的標頭?

我能夠通過在#include <stdlib.h>之前插入#include <stdint.h>來解決之前的問題

我會嘗試-D_XOPEN_SOURCE=600並回復您。 一定是編譯系統出了問題。

好吧,您可能需要在<sys/stat.h>之前包含<time.h> (或者可能是<sys/time.h> ),但是如果可行的話<sys/stat.h> header 已經損壞了。 <stdlib.h> header 如果您必須在包含它之前包含<stdint.h> ,它就會被破壞。 我想 Ubuntu 7.04 可能太舊了,您應該在許多這些標頭之前#include <sys/types.h> ,但這仍然不是<stdlib.h>的借口; 那應該是獨立的。 POSIX 1997 要求在<sys/stat.h>之前#include <sys/types.h> > ; POSIX 2004 沒有。 而且我認為 Ubuntu 7.04 沒那么老。

不過請注意, st_atim成員是新成員; 它被添加到 POSIX 2008(因此在 POSIX 2013 中)。 之前只是st_atime (而st_atime現在是st_atim.tv_sec的宏)。

包括-D_XOPEN_SOURCE=600處理位統計問題。 Ubuntu 7.04 於 2007 年發布,我正在使用的本書的第二版於 2008 年出版。另外,不確定這是否有用,但在另一個包含<stdio.h><string.h>的先前示例中<string.h> (與僅<stdio.h>相對),代碼無需任何干預即可正常運行。

有趣……它會讓你的生活變得有趣,以一種生活不需要有趣的方式。 (記住像“願你生活在有趣的時代”這樣的中文詛咒 spring。)在所有編譯中使用-DXOPEN_SOURCE=600選項並保持雙手交叉; 這可能會解決你的大部分問題。 也可以考慮使用-std=gnu99或代替。 幸運的話,其中一個或兩個應該可以解決大多數問題。

為了防止其他人遇到與本書相同的問題,我從hacking-live-1.0.iso下載了 iso 文件。 創建了一個可啟動的 usb 並且一切正常,沒有損壞的標頭或任何東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM