1 /* @(#) $Id: ads_dump.c,v 1.4 2009/06/24 18:53:08 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
20 * Dumps every NTFS ADS found in a directory (recursive)
24 int read_sys_dir(char *dir_name);
28 /* Print out streams of a file */
29 int os_get_streams(char *full_path)
35 char stream_name[MAX_PATH +1];
36 char final_name[MAX_PATH +1];
38 DWORD dwRead, shs, dw1, dw2;
42 file_h = CreateFile(full_path,
47 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_POSIX_SEMANTICS,
50 if (file_h == INVALID_HANDLE_VALUE)
57 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
59 /* Getting stream header size -- should be 20 bytes */
60 shs = (LPBYTE)&sid.cStreamName - (LPBYTE)&sid+ sid.dwStreamNameSize;
65 if(BackupRead(file_h, (LPBYTE) &sid, shs, &dwRead,
66 FALSE, FALSE, &context) == 0)
75 stream_name[0] = '\0';
76 stream_name[MAX_PATH] = '\0';
77 if(BackupRead(file_h, (LPBYTE)stream_name,
79 &dwRead, FALSE, FALSE, &context))
84 snprintf(final_name, MAX_PATH, "%s%S", full_path,
85 (WCHAR *)stream_name);
86 tmp_pt = strrchr(final_name, ':');
91 printf("Found NTFS ADS: '%s' \n", final_name);
97 if(!BackupSeek(file_h, sid.Size.LowPart, sid.Size.HighPart,
98 &dw1, &dw2, &context))
109 int read_sys_file(char *file_name)
114 /* Getting streams */
115 os_get_streams(file_name);
118 if(stat(file_name, &statbuf) < 0)
123 /* If directory, read the directory */
124 else if(S_ISDIR(statbuf.st_mode))
126 return(read_sys_dir(file_name));
135 int read_sys_dir(char *dir_name)
139 struct dirent *entry;
143 /* Getting the number of nodes. The total number on opendir
146 if(stat(dir_name, &statbuf) < 0)
152 /* Must be a directory */
153 if(!S_ISDIR(statbuf.st_mode))
159 /* Opening the directory given */
160 dp = opendir(dir_name);
166 /* Reading every entry in the directory */
167 while((entry = readdir(dp)) != NULL)
169 char f_name[MAX_PATH +2];
171 /* Just ignore . and .. */
172 if((strcmp(entry->d_name,".") == 0) ||
173 (strcmp(entry->d_name,"..") == 0))
178 /* Creating new file + path string */
179 snprintf(f_name, MAX_PATH +1, "%s\\%s",dir_name, entry->d_name);
181 read_sys_file(f_name);
191 int main(int argc, char **argv)
193 printf("%s: NTFS ADS dumper (GPL v2)\n", argv[0]);
194 printf("by Daniel B. Cid - dcid at ossec.net\n\n");
197 /* Going to print every NTFS ADS found */
200 printf("%s dir\n", argv[0]);
205 /* Getting streams */
206 read_sys_file(argv[1]);
211 printf("No NTFS ADS found.\n");