-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixObfuscatedFilenames.c
222 lines (198 loc) · 5.13 KB
/
fixObfuscatedFilenames.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// Created by paul on 1/16/23.
//
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ftw.h>
#include <ctype.h>
#undef DEBUG
const char * myName;
/**
*
* @return
*/
int usage( void )
{
fprintf( stderr, "### %s: requires at least one path to process.\n",
myName );
return 0;
}
/**
* returns a pointer to ether the last period or end of a filename
* @return
*/
const char * getExt( const char * filename )
{
const char *end = strrchr(filename, '.');
if (end == NULL) {
end = filename;
while (*end != '\0') end++;
}
return end;
}
/**
*
* @param filename
* @param ext
* @return
*/
int isFilenameobfuscated( const char * filename, const char * ext )
{
int result = 1;
int count = 0;
int digit = 0;
int xdigit = 0;
int lower = 0;
int upper = 0;
for (const char *p = filename; p < ext; ++p) {
count++;
if (isxdigit(*p)) xdigit++;
if (isdigit(*p)) digit++;
else if (islower(*p)) lower++;
else if (isupper(*p)) upper++;
else {
/* non-alphanum character - therefore it's not obfuscated */
result = 0;
}
}
#ifdef DEBUG
fprintf( stderr, "count:%d xdigit:%d digit:%d lower:%d upper:%d\n",
count, xdigit, digit, lower, upper);
#endif
/* if we haven't already determined it's not obfuscated, check */
if (result == 1) {
if (count == xdigit) {
/* all the characters are valid hex digits */
#ifdef DEBUG
fprintf(stderr, ">>> all hex digits ");
#endif
} else if (digit > 0 && lower > 0 && upper > 0) {
/* a mix of digits, upper and lower case characters */
#ifdef DEBUG
fprintf(stderr, ">>> all mixed alphanum ");
#endif
} else result = 0;
}
return result;
}
/**
*
* @param path
* @param sb
* @param typeFlag
* @param ftwbuf
* @return
*/
int nftwEntry( const char * path, const struct stat * sb, int typeFlag, struct FTW * ftwbuf )
{
int result = FTW_CONTINUE;
const char * leaf = "";
if (ftwbuf != NULL) {
leaf = &path[ ftwbuf->base ];
}
switch ( typeFlag )
{
case FTW_F: // passed a file
if ( leaf[0] == '.' )
{
fprintf( stderr, "### file: \'%s\' ignored\n", leaf );
} else {
const char * ext = getExt(leaf);
if ( isFilenameobfuscated( leaf, ext ) ) {
fprintf( stderr, " obfuscated: \'%s\'\n", leaf );
const char * parent = path;
const char * parend = path;
for ( const char * p = path; *p != '\0'; ++p )
{
if ( *p == '/' ) {
parent = parend;
parend = p;
}
}
++parent; /* skip over the slash */
int pLen = parend - path + 1;
int bLen = parend - parent;
char * newPath;
asprintf( &newPath, "%.*s%.*s%s", pLen, path, bLen, parent, ext );
if ( rename( path, newPath ) == -1 ) {
fprintf( stderr, "### %s: rename failed (%d: %s)\n",
myName, errno, strerror(errno) );
}
fprintf(stderr, "\"%s\" -> \"%s\"\n", path, newPath );
free(newPath);
}
#ifdef DEBUG
else {
fprintf( stderr, "### file: \'%s\'\n", leaf );
}
#endif
}
break;
case FTW_D: // passed a directory
if ( leaf[0] == '.' )
{
#ifdef DEBUG
fprintf( stderr, "### directory: \'%s\' ignored\n", leaf );
#endif
result = FTW_SKIP_SUBTREE;
}
#ifdef DEBUG
else {
fprintf(stderr, "### directory: \'%s\'\n", leaf);
}
#endif
break;
default:
#ifdef DEBUG
fprintf( stderr, "### other: \'%s\'\n", leaf );
#endif
break;
}
return result;
}
/**
*
* @param path
* @return
*/
int scanHierarchy( const char * path )
{
#ifdef DEBUG
fprintf( stderr, "%s: path=\'%s\'\n", myName, path );
#endif
nftw( path, nftwEntry, 10, FTW_ACTIONRETVAL );
return 0;
}
/**
*
* @param argc
* @param argv
* @return
*/
int main( int argc, char * argv[] )
{
int exitcode = 0;
myName = strrchr( argv[0], '/' );
if ( myName++ == NULL ) myName = argv[0];
if ( argc < 2 ) {
exitcode = usage();
}
else for (int i = 1; exitcode == 0 && i < argc; ++i )
{
char * fullPath = realpath(argv[i], NULL );
if ( fullPath == NULL )
{
fprintf( stderr, "### %s: unable to convert \'%s\' to a full path (%d: %s)\n",
myName, argv[i], errno, strerror(errno) );
exitcode = -errno;
} else {
exitcode = scanHierarchy( fullPath );
free( fullPath );
}
}
return exitcode;
}