Hacking - The Art of Exploitation, 2nd Edition

(Romina) #1

84 0x200


reader@hacking:~/booksrc $ ./simplenote "this is a test note"
[DEBUG] buffer @ 0x804a008: 'this is a test note'
[DEBUG] datafile @ 0x804a070: '/tmp/notes'
[DEBUG] file descriptor is 3
Note has been saved.
reader@hacking:~/booksrc $ cat /tmp/notes
this is a test note
reader@hacking:~/booksrc $ ./simplenote "great, it works"
[DEBUG] buffer @ 0x804a008: 'great, it works'
[DEBUG] datafile @ 0x804a070: '/tmp/notes'
[DEBUG] file descriptor is 3
Note has been saved.
reader@hacking:~/booksrc $ cat /tmp/notes
this is a test note
great, it works
reader@hacking:~/booksrc $

The output of the program’s execution is pretty self-explanatory, but
there are some things about the source code that need further explanation.
The files fcntl.h and sys/stat.h had to be included, since those files define the
flags used with the open() function. The first set of flags is found in fcntl.h
and is used to set the access mode. The access mode must use at least one of
the following three flags:

These flags can be combined with several other optional flags using the
bitwise OR operator. A few of the more common and useful of these flags are
as follows:

Bitwise operations combine bits using standard logic gates such as OR and
AND. When two bits enter an OR gate, the result is 1 if either the first bit or the
second bit is 1. If two bits enter an AND gate, the result is 1 only if both the first
bit and the second bit are 1. Full 32-bit values can use these bitwise operators to
perform logic operations on each corresponding bit. The source code of
bitwise.c and the program output demonstrate these bitwise operations.

bitwise.c


#include <stdio.h>

int main() {
int i, bit_a, bit_b;
printf("bitwise OR operator |\n");

O_RDONLY Open file for read-only access.
O_WRONLY Open file for write-only access.
O_RDWR Open file for both read and write access.

O_APPEND Write data at the end of the file.
O_TRUNC If the file already exists, truncate the file to 0 length.
O_CREAT Create the file if it doesn’t exist.
Free download pdf