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 223 224 225 226
|
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstdint>
#include<cstring>
#include<fstream>
#include <errno.h>
#include <string>
#include <unistd.h>
using std::string;
// WARNING: If you change this enum you must also change operStrings below
enum Oper {add, subtract, multiply, divide, Random, XOR, AND, OR, NOT, MOD};
// Names of the opers. WARNING: These must correspond 1-1 with the Oper's above
static const char * operStrings[] = {
"add", "sub", "mul", "div", "ran", "xor", "and", "or", "not", "mod",
nullptr
};
FILE *ifile;
FILE *ofile;
// Just like fopen() but it prints a message to stdout on error
FILE *myfopen(const char *name, const char *mode)
{
FILE *result = fopen(name, mode);
if (result == nullptr) {
printf("Can't open %s: %s\n", name, strerror(errno));
}
return result;
}
void
errormsg()
{
printf("%s", "Syntax error. Syntax is:\n");
printf("%s",
"corrupt [switches] <ifile> <ofile> <start> <stop> <every> <operation> <additional>\n");
printf("%s", "ifile is the path to the input file.\n");
printf("%s", "ofile is the path to the output file.\n");
printf("%s", "start is the position in the file to start corrupting.\n");
printf("%s", "stop is the position in the file to stop corrupting.\n");
printf("%s", "every is the frequency of memory to ignore.\n");
printf("%s", "\n");
printf("%s", "Operations:\n");
printf("%s", "add Adds current byte to addative\n");
printf("%s", "sub Subtracts current byte from addative\n");
printf("%s", "mul Multiplies current byte by addative\n");
printf("%s", "div Divides current byte by addative\n");
printf("%s", "mod Modular current byte by addative\n");
printf("%s", "xor Xor's current byte by addative\n");
printf("%s", "and And's current byte by addative\n");
printf("%s", " or Or's current byte by addative\n");
printf("%s", "ran Randomizes current byte\n");
printf("%s", "not Not's current byte\n");
printf("%s", "\n");
printf("%s", "Switches:\n");
printf("%s", "-l [file] Load corruption from file\n");
printf("%s", "-v Output corruptor version\n");
printf("%s", "-s [file] Save corruption to file\n");
exit(1);
}
enum Oper getOper(const string &str)
{
for (int i=0; operStrings[i]; ++i) {
if (str == operStrings[i]) {
return (Oper)i;
}
}
printf("%s is not a valid operation string\n", str.c_str());
exit(1);
}
char
op(enum Oper oper, char a, char b)
{
switch(oper) {
case add:
return a + b;
break;
case subtract:
return a - b;
break;
case multiply:
return a * b;
break;
case divide:
return a / b;
break;
case Random:
return rand() % 128 - 64;
break;
case XOR:
return a xor b;
break;
case AND:
return a and b;
break;
case OR:
return a or b;
break;
case NOT:
return not(a);
break;
case MOD:
return a % b;
break;
}
return 0; // should not be reached.
}
// Corrupt every nth byte in ifile from strt to stop by changing the byte to
// byte <oper> operand
void
corrupt(unsigned long long int start, unsigned long long int stop,
unsigned int n, enum Oper oper, char operand)
{
unsigned long long int dimX = 0;
unsigned int FreqCounter = 0;
int regbyte;
while ((regbyte = fgetc(ifile)) != EOF) //While we haven't reached the end of the file...
{
if ((FreqCounter >= n) and((dimX >= start) and(dimX <= stop))) //And the conditions are juuust right...
{
regbyte = op(oper, regbyte, operand);
putc(regbyte, ofile);
FreqCounter = 0; //corrupt dat byte!
} else {
putc(regbyte, ofile);
} //Or don't...
FreqCounter = FreqCounter + 1;
dimX = dimX + 1; //counting
}
printf("%s", "The file seems to have been successfully corrupted...\n");
}
int
main(int argc, char *argv[])
{
string opString;
std::ofstream savefile;
std::ifstream loadfile;
int op;
// These are arguments to corrupt
unsigned long long start, stop;
unsigned int frequency;
enum Oper oper;
char operand;
while ((op = getopt(argc, argv, "l:vs:")) != EOF) {
switch (op) {
case 'l':
loadfile.open(optarg);
if (!loadfile) {
printf("Can't open %s\n", optarg);
return 1;
}
break;
case 'v':
printf("%s", "LAwl\n");
break;
case 's':
savefile.open(optarg);
if (!savefile) {
printf("Can't open %s\n", optarg);
return 1;
}
break;
default:
errormsg();
}
}
if (optind+2 > argc) errormsg();
if ((ifile = myfopen(argv[optind++], "rb")) == nullptr) {
return 1;
}
if ((ofile = myfopen(argv[optind++], "wb")) == nullptr) {
return 1;
}
// Now get the corruption options from the command
// line or loadFile
if (loadfile.is_open()) {
int tmp;
loadfile >> start >> stop >> frequency >> opString >> tmp;
operand = tmp;
oper = getOper(opString);
} else {
if (optind + 5 > argc) {
errormsg();
}
start = atoi(argv[optind++]);
stop = atoi(argv[optind++]);
frequency = atoi(argv[optind++]);
oper = getOper(argv[optind++]);
operand = atoi(argv[optind++]);
}
// Now you have all the options
corrupt(start, stop, frequency, oper, operand);
fclose(ifile);
fclose(ofile);
if (savefile.is_open()) {
savefile << start << '\n'
<< stop << '\n'
<< frequency << '\n'
<< operStrings[(int)oper] << '\n'
<< operand << '\n';
}
return 0;
}
|