1 /* @(#) $Id: ./src/rootcheck/util/ads_dump.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
21 * Dumps every NTFS ADS found in a directory (recursive)
25 int read_sys_dir(char *dir_name);
29 /* Print out streams of a file */
30 int os_get_streams(char *full_path)
36 char stream_name[MAX_PATH +1];
37 char final_name[MAX_PATH +1];
39 DWORD dwRead, shs, dw1, dw2;
43 file_h = CreateFile(full_path,
48 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_POSIX_SEMANTICS,
51 if (file_h == INVALID_HANDLE_VALUE)
58 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
60 /* Getting stream header size -- should be 20 bytes */
61 shs = (LPBYTE)&sid.cStreamName - (LPBYTE)&sid+ sid.dwStreamNameSize;
66 if(BackupRead(file_h, (LPBYTE) &sid, shs, &dwRead,
67 FALSE, FALSE, &context) == 0)
76 stream_name[0] = '\0';
77 stream_name[MAX_PATH] = '\0';
78 if(BackupRead(file_h, (LPBYTE)stream_name,
80 &dwRead, FALSE, FALSE, &context))
85 snprintf(final_name, MAX_PATH, "%s%S", full_path,
86 (WCHAR *)stream_name);
87 tmp_pt = strrchr(final_name, ':');
92 printf("Found NTFS ADS: '%s' \n", final_name);
98 if(!BackupSeek(file_h, sid.Size.LowPart, sid.Size.HighPart,
99 &dw1, &dw2, &context))
110 int read_sys_file(char *file_name)
115 /* Getting streams */
116 os_get_streams(file_name);
119 if(stat(file_name, &statbuf) < 0)
124 /* If directory, read the directory */
125 else if(S_ISDIR(statbuf.st_mode))
127 return(read_sys_dir(file_name));
136 int read_sys_dir(char *dir_name)
140 struct dirent *entry;
144 /* Getting the number of nodes. The total number on opendir
147 if(stat(dir_name, &statbuf) < 0)
153 /* Must be a directory */
154 if(!S_ISDIR(statbuf.st_mode))
160 /* Opening the directory given */
161 dp = opendir(dir_name);
167 /* Reading every entry in the directory */
168 while((entry = readdir(dp)) != NULL)
170 char f_name[MAX_PATH +2];
172 /* Just ignore . and .. */
173 if((strcmp(entry->d_name,".") == 0) ||
174 (strcmp(entry->d_name,"..") == 0))
179 /* Creating new file + path string */
180 snprintf(f_name, MAX_PATH +1, "%s\\%s",dir_name, entry->d_name);
182 read_sys_file(f_name);
192 int main(int argc, char **argv)
194 printf("%s: NTFS ADS dumper (GPL v2)\n", argv[0]);
195 printf("by Daniel B. Cid - dcid at ossec.net\n\n");
198 /* Going to print every NTFS ADS found */
201 printf("%s dir\n", argv[0]);
206 /* Getting streams */
207 read_sys_file(argv[1]);
212 printf("No NTFS ADS found.\n");