-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsecdel.C
More file actions
185 lines (159 loc) · 5.85 KB
/
secdel.C
File metadata and controls
185 lines (159 loc) · 5.85 KB
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
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <tclap/CmdLine.h>
#include "secdel.H"
typedef std::vector<std::string>::const_iterator FnameIter;
struct inputInfo {
std::string basepath, basename;
std::vector<std::string> inputStrings;
int levelVal;
bool streamVal, renameVal;
} CMDArgs;
/*------------------------------------------------------------
* RNG dependant
*------------------------------------------------------------*/
BYTE randomNumber() { return std::rand() & 0xFF; }
char randomChar() { return 'a' + std::rand() % 26; }
/*------------------------------------------------------------
* Wiping function
*------------------------------------------------------------*/
void wipeFunction (InputFile &file)
{
int cnt = file.filesize() / MEGABYTE;
int mod = file.filesize() % MEGABYTE;
std::ios fmt(NULL);
fmt.copyfmt(std::cout);
BYTE *chunk;
if (CMDArgs.streamVal)
chunk = new BYTE[MEGABYTE];
else
chunk = new BYTE[file.filesize()];
for (int levCount = 0; levCount < CMDArgs.levelVal; levCount++) {
BYTE rand = randomNumber();
if (CMDArgs.streamVal) {
memset (chunk, rand, MEGABYTE);
for (int i = 0; i < cnt; i++)
file.write(chunk, MEGABYTE);
file.write(chunk, mod);
} else {
memset (chunk, rand, file.filesize());
file.write(chunk, file.filesize());
}
file.resetHandle();
std::cout << "Pass " << std::setw(2) << levCount
<< ": written value: " << std::hex << std::setfill('0')
<< std::internal << std::showbase << std::setw(4)
<< (int)rand << '\n';
std::cout.copyfmt(fmt);
}
delete[] chunk;
}
/*------------------------------------------------------------
* Renaming function
*------------------------------------------------------------*/
std::string rename (InputFile &file, std::string filename)
{
std::string tempFilename = filename;
for (int i = 0; i < CMDArgs.levelVal; i++) {
std::string newFilename(tempFilename.size(), randomChar());
if (file.rename(tempFilename.c_str(), newFilename.c_str()))
std::cout << "Pass " << std::setw(2) << i << ": renamed as: "
<< newFilename << '\n';
tempFilename = newFilename;
}
return tempFilename;
}
/*------------------------------------------------------------
* Reverse iterate the provided file path
*------------------------------------------------------------*/
void cmdArgsFileProcess (std::string origName)
{
std::string::reverse_iterator revIt = origName.rbegin();
CMDArgs.basepath.clear();
CMDArgs.basename.clear();
while (*revIt != '\\') {
CMDArgs.basename.push_back(*revIt);
revIt++;
}
if (revIt > origName.rend()) {
CMDArgs.basepath = ".\\";
CMDArgs.basename = origName;
} else {
for (; revIt < origName.rend(); revIt++)
CMDArgs.basepath.push_back(*revIt);
std::reverse (CMDArgs.basepath.begin(), CMDArgs.basepath.end());
std::reverse (CMDArgs.basename.begin(), CMDArgs.basename.end());
}
}
/*------------------------------------------------------------
* Process a file by name
*------------------------------------------------------------*/
int processFile(std::string filename) {
cmdArgsFileProcess(filename);
std::cout << "\nInput file: " << '\t' << CMDArgs.basename << '\n';
InputFile file((LPCTSTR)filename.c_str());
file.open();
if (file.hInput == INVALID_HANDLE_VALUE) {
std::cout << "Failed to open handle to file. Error code: "
<< GetLastError() << '\n';
return 1;
}
std::cout << "Filesize: " << '\t' << file.filesize()
<< " bytes\n\nWiping File Contents:\n";
wipeFunction(file);
file.close();
if (CMDArgs.renameVal) {
std::cout << "\nWiping Filename:" << filename << '\n';
filename = rename(file, filename);
}
return file.remove(filename.c_str());
}
/*------------------------------------------------------------
Argument Returning (CMDArgs)
------------------------------------------------------------*/
void tclapArgs (int argn, char const *args[])
{
try {
TCLAP::CmdLine cmd("Secure deletion tool for Windows", ' ', "1.05");
TCLAP::UnlabeledMultiArg<std::string> inputArg("input",
"Input File-path", true, "String", false);
TCLAP::ValueArg<int> levelArg("l", "levels",
"Number of times to wipe", false, 1, "Integer");
TCLAP::SwitchArg streamSwitch("s", "stream",
"Wipe in small chunks", cmd, 0);
TCLAP::SwitchArg renameSwitch("r", "rename",
"Rename [-l] levels before deletion", cmd, 0);
cmd.add(inputArg);
cmd.add(levelArg);
cmd.parse(argn, args);
CMDArgs.inputStrings = inputArg.getValue();
CMDArgs.levelVal = levelArg.getValue();
CMDArgs.renameVal = renameSwitch.getValue();
CMDArgs.streamVal = streamSwitch.getValue();
} catch (TCLAP::ArgException &e) {
std::cerr << "Error: " << e.error() << " for arg "
<< e.argId() << std::endl;
}
}
/*----------------------------------------------------
* MAIN()
*----------------------------------------------------*/
int main(int argc, char const *argv[])
{
std::srand(std::time(0));
tclapArgs(argc, argv);
std::vector<std::string> fnames = CMDArgs.inputStrings;
for (FnameIter it = fnames.begin(); it != fnames.end(); it++)
if (processFile(*it))
std::cout << "\nFile deleted (securely)" << '\n';
else
std::cout << "\nCould not process file. Error code: "
<< GetLastError() << '\n';
std::cout << '\n' << "Program Completed Successfully" << '\n';
return 0;
}