1
+ ;Example of reading the contents of a directory using the getdents API call. API calls found in this example program:
2
+ ; open, getdents, exit
3
+ ; High level description of what theis example program does:
4
+ ; Opens a directory in readonly mode for the file descriptor with open API
5
+ ; Reads data structure for directory and places in memory buffer with getdents API
6
+ ; Closes fd for directory with close API
7
+ ; exits gracefully with exit().
8
+
9
+ section .text
10
+ global _start
11
+
12
+ _start:
13
+
14
+ ; Open dir with open() call and a dir specified at 'dir' buffer
15
+ ;------------------------------------------------------------------------------
16
+ mov eax , 5 ;open
17
+ mov ebx , dir ;pointer to the directory
18
+ xor ecx , ecx ;readonly
19
+ xor edx , edx ;permissions not relevant
20
+ int 0x80
21
+ mov [ filehandle ], eax ;save filehandle
22
+
23
+ ; Get directroy contents into dirbuffer memory location
24
+ ;------------------------------------------------------------------------------
25
+ mov eax , 141 ;getdents
26
+ mov ebx , [ filehandle ] ;pointer to the directory fd
27
+ mov ecx , dirbuffer ;where we want the results
28
+ mov edx , 300 ;size of buffer for results
29
+ int 0x80
30
+
31
+ ; Close the file with close().
32
+ ;------------------------------------------------------------------------------
33
+ mov ebx , [ filehandle ] ;get filehandle return value
34
+ mov eax , 6 ;close
35
+ int 0x80
36
+
37
+ ; Exit program
38
+ ;------------------------------------------------------------------------------
39
+ mov eax , 1
40
+ int 0x80
41
+
42
+ section .data
43
+ dir db '.' , 0x00
44
+
45
+ section .bss
46
+ filehandle resb 4
47
+ dirbuffer resb 300
48
+
49
+ ; Seems simple right? Oh...you wanted something inteligible as far as the
50
+ ; directory listing results, and you wanted them printed out, not just a data
51
+ ; blob in a memory buffer? Aint nobody got time for that, below is some
52
+ ; documentation for the datastructure, parse it (with your brain) from memory
53
+ ; yourself. This is just a quick PoC for getdents, not a PoC for parsing a
54
+ ; complex data structure and displaying it to a human (that program would be
55
+ ; bigger).
56
+ ; ------------------------------
57
+ ; | Some bitfield explanations |
58
+ ; ------------------------------
59
+
60
+ ; Data Structure for the directory results that show up in the provided buffer
61
+ ;------------------------------------------------------------------------------
62
+ ; struct linux_dirent {
63
+ ; unsigned long d_ino; /* Inode number */
64
+ ; unsigned long d_off; /* Offset to next linux_dirent */
65
+ ; unsigned short d_reclen; /* Length of this linux_dirent */
66
+ ; char d_name[]; /* Filename (null-terminated) */
67
+ ; /* length is actually (d_reclen - 2 -
68
+ ; offsetof(struct linux_dirent, d_name)) */
69
+ ; /*
70
+ ; char pad; // Zero padding byte
71
+ ; char d_type; // File type (only since Linux
72
+ ; // 2.6.4); offset is (d_reclen - 1)
73
+ ;
74
+ ; }
0 commit comments