forked from zombieCraig/UDSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamedata.cc
421 lines (394 loc) · 14.1 KB
/
gamedata.cc
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "gamedata.h"
GameData::GameData() {
}
GameData::~GameData() {
}
Module *GameData::get_module(int id) {
for(vector<Module>::iterator it = modules.begin(); it != modules.end(); ++it) {
if(it->getArbId() == id) return &*it;
}
return NULL;
}
vector <Module *> GameData::get_active_modules() {
vector<Module *> active_modules;
for(vector<Module>::iterator it = modules.begin(); it != modules.end(); ++it) {
if(it->isResponder() == false) active_modules.push_back(&*it);
}
return active_modules;
}
/* Same as get_active_modules but designed for learning mode */
vector <Module *> GameData::get_possible_active_modules() {
vector<Module *> possible_active_modules;
for(vector<Module>::iterator it = possible_modules.begin(); it != possible_modules.end(); ++it) {
if(it->isResponder() == false) possible_active_modules.push_back(&*it);
}
return possible_active_modules;
}
Module *GameData::get_possible_module(int id) {
for(vector<Module>::iterator it = possible_modules.begin(); it != possible_modules.end(); ++it) {
if(it->getArbId() == id) return &*it;
}
return NULL;
}
void GameData::setMode(int m) {
switch(m) {
case MODE_SIM:
Msg("Switching to Simulator mode");
if(_gui) _gui->setStatus("Simulation Mode");
if(mode == MODE_LEARN && possible_modules.size() > 0) { // Previous mode was learning, update
Msg("Normalizing learned data");
GameData::processLearned();
}
mode=m;
if(_gui) _gui->DrawModules();
break;
case MODE_LEARN:
Msg("Switching to Learning mode");
if(_gui) _gui->setStatus("Learning Mode");
mode=m;
break;
case MODE_ATTACK:
Msg("Switching to Attack mode");
if(_gui) _gui->setStatus("Attack Mode");
if(mode == MODE_LEARN && possible_modules.size() > 0) { // Previous mode was learning, update
Msg("Normalizing learned data");
GameData::processLearned();
}
mode=m;
if(_gui) _gui->DrawModules();
break;
default:
Msg("Unknown game mode");
break;
}
}
void GameData::processPkt(canfd_frame *cf) {
switch(mode) {
case MODE_SIM:
GameData::HandleSim(cf);
break;
case MODE_LEARN:
GameData::LearnPacket(cf);
break;
case MODE_ATTACK:
GameData::AttackPacket(cf);
break;
default:
cout << "ERROR: Processing packets while in an unknown mode" << mode << endl;
break;
}
}
void GameData::HandleSim(canfd_frame *cf) {
Module *module = GameData::get_module(cf->can_id);
CanFrame *resFrame = NULL;
// Flow control is special
if(cf->data[0] == 0x30 && !module) module = GameData::get_module(cf->can_id - 1);
if(module) {
if(module->getIgnore()) return;
if(!module->isResponder()) {
module->setState(STATE_ACTIVE);
if(_gui) _gui->DrawModules(true);
vector<CanFrame *>response = module->getResponse(cf, false);
if(!response.empty()) {
resFrame = response.at(0);
if(resFrame->data[1] > 9) {
if (resFrame->data[0] > 8) {// Multi-byte
canif->sendPackets(response);
} else { // Single packet
if(response.size() > 1) {
// More than one possible answer, select a random one
vector<CanFrame *>randFrame;
randFrame.push_back(response.at(rand() % response.size()));
canif->sendPackets(randFrame);
} else {
canif->sendPackets(response);
}
}
} else { // <= 9 then MODE note PID
canif->sendPackets(response);
}
}
}
}
}
void GameData::LearnPacket(canfd_frame *cf) {
Module *module = GameData::get_possible_module(cf->can_id);
Module *possible_module = GameData::isPossibleISOTP(cf);
int possible;
// If module exists then we have seen this ID before
if(module) {
module->addPacket(cf);
if(possible_module) {
module->incMatchedISOTP();
} else {
// Still maybe an ISOTP answer, check for common syntax
if(cf->data[0] == 0x10 && cf->len == 8) {
module->incMatchedISOTP();
} else if(cf->data[0] == 0x30 && cf->len == 3) {
module->incMatchedISOTP();
} else if(cf->data[0] >= 0x21 || cf->data[0] <= 0x30) {
module->incMatchedISOTP();
} else {
module->incMissedISOTP();
}
}
module->setState(STATE_ACTIVE);
if(_gui) _gui->DrawModules(true);
} else if(possible_module) { // Haven't seen this ID yet
possible_module->addPacket(cf);
possible_modules.push_back(*possible_module);
if(_gui) _gui->DrawModules();
}
}
void GameData::AttackPacket(canfd_frame *cf) {
Module *module = GameData::get_module(cf->can_id);
CanFrame *resFrame = NULL;
// Flow control is special
if(cf->data[0] == 0x30 && !module) module = GameData::get_module(cf->can_id - 1);
if(module) {
if(module->getIgnore()) return;
if(!module->isResponder()) {
module->setState(STATE_ACTIVE);
if(_gui) _gui->DrawModules(true);
vector<CanFrame *>response = module->getResponse(cf, true);
if(!response.empty()) {
resFrame = response.at(0);
if(resFrame->data[1] > 9) {
if (resFrame->data[0] > 8) {// Multi-byte
canif->sendPackets(response);
} else { // Single packet
if(response.size() > 1) {
// More than one possible answer, select a random one
vector<CanFrame *>randFrame;
randFrame.push_back(response.at(rand() % response.size()));
canif->sendPackets(randFrame);
} else {
canif->sendPackets(response);
}
}
} else { // <= 9 then MODE note PID
canif->sendPackets(response);
}
}
}
}
}
Module *GameData::isPossibleISOTP(canfd_frame *cf) {
int i;
bool padding = false;
char last_byte;
Module *possible = NULL;
if(cf->data[0] == cf->len - 1) { // Possible UDS request
possible = new Module(cf->can_id);
} else if(cf->data[0] < cf->len - 2) { // Check if remaining bytes are just padding
padding = true;
if(cf->data[0] == 0) padding = false;
last_byte = cf->data[cf->data[0] + 1];
for(i=cf->data[0] + 2; i < cf->len; i++) {
if(cf->data[i] != last_byte) {
padding = false;
} else {
last_byte = cf->data[i];
}
}
if(padding == true) { // Possible UDS w/ padding
possible = new Module(cf->can_id);
possible->setPaddingByte(last_byte);
}
}
return possible;
}
// Goes through the modules and removes ones that are less
// likely to be of use
void GameData::pruneModules() {
vector<Module> goodModules;
bool keep = false;
for(vector<Module>::iterator it = modules.begin(); it != modules.end(); ++it) {
keep = false;
if(it->confidence() > CONFIDENCE_THRESHOLD) {
if(it->getPositiveResponder() > -1 || it->getNegativeResponder() > -1) keep = true;
if(it->isResponder()) keep = true;
if(!keep and it->getMatchedISOTP() > 0) keep = true;
}
if(keep) {
goodModules.push_back(*it);
} else {
if(verbose) cout << "Removing module " << hex << it->getArbId() << endl;
}
}
modules = goodModules;
}
void GameData::processLearned() {
if(verbose) cout << "Identified " << possible_modules.size() << " possible modules" << endl;
modules = possible_modules;
if(verbose) cout << "Locating responders" << endl;
Module *responder = NULL;
for(vector<Module>::iterator it = modules.begin(); it != modules.end(); ++it) {
if(it->isResponder() == false) {
responder = GameData::get_module(it->getArbId() + 0x300);
if(responder) { // GM style positive response
it->setPositiveResponderID(responder->getArbId());
responder->setResponder(true);
}
responder = GameData::get_module(it->getArbId() + 0x400);
if(responder) { // GM style negative response
it->setNegativeResponderID(responder->getArbId());
responder->setResponder(true);
}
responder = GameData::get_module(it->getArbId() + 0x20);
if(responder && it->foundResponse(responder)) { // Renault/Dacia response
it->setPositiveResponderID(responder->getArbId());
it->setNegativeResponderID(responder->getArbId());
responder->setResponder(true);
}
responder = GameData::get_module(it->getArbId() + 0x08);
if(responder && it->foundResponse(responder)) { // Standard response
it->setPositiveResponderID(responder->getArbId());
it->setNegativeResponderID(responder->getArbId());
responder->setResponder(true);
}
responder = GameData::get_module(it->getArbId() + 0x01);
if(responder && it->foundResponse(responder)) { // check for flow control
vector<CanFrame *>pkts = responder->getPacketsByBytePos(0, 0x30);
if(pkts.size() > 0) responder->setResponder(true);
}
}
}
GameData::pruneModules();
// Cleanup - After pruning we can space out modules more
possible_modules = modules; // Clear up possible to known modules
for(vector<Module>::iterator it = modules.begin(); it != modules.end(); ++it) {
Module *mod = &*it;
if(_gui->isModuleOverlapping(mod)) _gui->setRandomModulePosition(mod);
}
_gui->Redraw();
stringstream m;
m << "Identified " << GameData::get_active_modules().size() << " Active modules";
GameData::Msg(m.str());
}
string GameData::frame2string(canfd_frame *cf) {
stringstream pkt;
if(cf->len < 0 || cf->len > 8) {
return "ERROR: CAN packet with imporoper length";
}
pkt << hex << cf->can_id << CANID_DELIM;
int i;
for(i=0; i < cf->len; i++) {
pkt << setfill('0') << setw(2) << hex << (int)cf->data[i];
}
return pkt.str();
}
void GameData::Msg(string mesg) {
if(_gui == NULL) return;
_gui->Msg(mesg);
}
bool GameData::SaveConfig() {
ofstream configFile;
configFile.open("config_data.cfg");
// Globals
// Modules
configFile << endl;
for(vector<Module>::iterator it = modules.begin(); it != modules.end(); ++it) {
configFile << "[" << hex << it->getArbId() << "]" << endl;
configFile << "pos = " << dec << it->getX() << "," << it->getY() << endl;
configFile << "responder = " << it->isResponder() << endl;
if(!it->isResponder()) {
if(it->getPositiveResponder() != -1) configFile << "positiveID = " << hex << it->getPositiveResponder() << endl;
if(it->getNegativeResponder() != -1) configFile << "negativeID = " << hex << it->getNegativeResponder() << endl;
}
if(it->getIgnore()) configFile << "ignore = " << it->getIgnore() << endl;
if(it->getFuzzVin()) configFile << "fuzz_vin = " << it->getFuzzVin() << endl;
if(it->getFuzzLevel() > 0) configFile << "fuzz_level = " << it->getFuzzLevel() << endl;
configFile << "{Packets}" << endl;
vector <CanFrame *>frames = it->getHistory();
for(vector<CanFrame *>::iterator it2 = frames.begin(); it2 != frames.end(); ++it2) {
CanFrame *frame = *it2;
configFile << frame->estr() << endl;
}
configFile << endl;
}
configFile.close();
Msg("Saved config_data.cfg");
return true;
}
void GameData::launchPeach() {
ofstream peachXML;
peachXML.open("fuzz_can.xml");
peachXML << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl;
peachXML << "<Peach xmlns=\"http://peachfuzzer.com/2012/Peach\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" << endl;
peachXML << " xsi:schemaLocation=\"http://peachfuzzer.com/2012/Peach /peach/peach.xsd\">" << endl;
// TODO Use learned information to populate DataModels
peachXML << " <DataModel name=\"CANPacket\">" << endl;
peachXML << " <Blob name=\"sample1\" valueType=\"hex\" value=\"00 00 03 33 03 01 02 03 00 00 00 00 00 00 00 00\"/>" << endl;
peachXML << " </DataModel>" << endl;
peachXML << endl;
peachXML << " <StateModel name=\"TheState\" initialState=\"Initial\">" << endl;
peachXML << " <State name=\"Initial\">" << endl;
peachXML << " <Action type=\"output\">" << endl;
peachXML << " <DataModel ref=\"CANPacket\"/>" << endl;
peachXML << " </Action>" << endl;
peachXML << " </State>" << endl;
peachXML << " </StateModel>" << endl;
peachXML << " <Agent name=\"TheAgent\">" << endl;
peachXML << " </Agent>" << endl;
peachXML << " <Test name=\"Default\">" << endl;
peachXML << " <Agent ref=\"TheAgent\"/>" << endl;
peachXML << " <StateModel ref=\"TheState\"/>" << endl;
peachXML << " <Publisher class=\"CAN\">" << endl;
peachXML << " <Param name=\"Interface\" value=\"" << canif->getIfname() << "\"/>" << endl;
peachXML << " </Publisher>" << endl;
peachXML << " <Logger class=\"File\">" << endl;
peachXML << " <Param name=\"Path\" value=\"logs\"/>" << endl;
peachXML << " </Logger>" << endl;
peachXML << " </Test>" << endl;
peachXML << "</Peach>" << endl;
peachXML.close();
Msg("Created fuzz_can.xml");
}
void GameData::nextMode() {
switch(mode) {
case MODE_SIM:
GameData::setMode(MODE_LEARN);
break;
case MODE_LEARN:
GameData::setMode(MODE_ATTACK);
break;
case MODE_ATTACK:
GameData::setMode(MODE_SIM);
break;
}
}
int GameData::string2hex(string s) {
stringstream ss;
int h;
ss << hex << s;
ss >> h;
return h;
}
int GameData::string2int(string s) {
stringstream ss;
int i;
ss << dec << s;
ss >> i;
return i;
}
void GameData::processCan() {
struct canfd_frame cf;
int i;
if(!canif) return;
int ticks = SDL_GetTicks();
if(_lastTicks + CAN_DELAY > ticks) {
vector <CanFrame *>frames = canif->getPackets();
for(vector <CanFrame *>::iterator it=frames.begin(); it != frames.end(); ++it) {
CanFrame *pkt = *it;
if(verbose) Msg(pkt->str());
cf.can_id = pkt->can_id;
cf.len = pkt->len;
for(i=0; i < pkt->len; i++) {
cf.data[i] = pkt->data[i];
}
GameData::processPkt(&cf);
}
_lastTicks = ticks;
}
}