Browse Source

fixed authentication to sector

develop
Robin Thoni 7 years ago
parent
commit
1eed01d23a

+ 39
- 22
cli/Interface/MainClass.cpp View File

121
 //    std::vector<std::string> keys;
121
 //    std::vector<std::string> keys;
122
 //    keys.push_back(StringUtils::humanToRaw("8829da9daf76").getData());
122
 //    keys.push_back(StringUtils::humanToRaw("8829da9daf76").getData());
123
 //    keys.push_back(StringUtils::humanToRaw("ffffffffffff").getData());
123
 //    keys.push_back(StringUtils::humanToRaw("ffffffffffff").getData());
124
-//    keys.push_back(StringUtils::humanToRaw("484558414354").getData());
124
+    keys.push_back(StringUtils::humanToRaw("484558414354").getData());
125
 
125
 
126
     int res = dump(tag, keys);
126
     int res = dump(tag, keys);
127
 //    int res = mapKeys(tag, keys);
127
 //    int res = mapKeys(tag, keys);
134
 
134
 
135
 int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
135
 int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
136
 {
136
 {
137
-    auto mappedKeysResult = tag->mapKeys(keys, printPercent);
138
-    std::cout << "\r";
137
+    auto mappedKeysResult = tag->mapKeys(keys, printPercentMapKeys);
138
+    if (isatty(1)) {
139
+        std::cout << "\r";
140
+    }
139
     if (!mappedKeysResult) {
141
     if (!mappedKeysResult) {
140
         mappedKeysResult.print();
142
         mappedKeysResult.print();
141
     }
143
     }
142
     else {
144
     else {
143
         auto mappedKeys = mappedKeysResult.getData();
145
         auto mappedKeys = mappedKeysResult.getData();
144
         for (int s = 0; s < mappedKeys.size(); ++s) {
146
         for (int s = 0; s < mappedKeys.size(); ++s) {
145
-            auto sectorKeys = mappedKeys[s];
147
+            auto sectorKey = mappedKeys[s];
146
             std::cout << "+Sector: " << s << std::endl;
148
             std::cout << "+Sector: " << s << std::endl;
147
-            for (int b = 0; b < sectorKeys.size(); ++b) {
148
-                auto blockKeys = sectorKeys[b];
149
+            for (int b = 0; b < 4; ++b) {
149
                 std::cout << "+Block: " << b << std::endl;
150
                 std::cout << "+Block: " << b << std::endl;
150
-                std::cout << "Key A: " << StringUtils::rawToHuman(blockKeys.first) << std::endl;
151
-                std::cout << "Key B: " << StringUtils::rawToHuman(blockKeys.second) << std::endl;
151
+                std::cout << "Key A: " << StringUtils::rawToHuman(sectorKey.first) << std::endl;
152
+                std::cout << "Key B: " << StringUtils::rawToHuman(sectorKey.second) << std::endl;
152
             }
153
             }
153
         }
154
         }
154
     }
155
     }
157
 
158
 
158
 int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
159
 int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
159
 {
160
 {
160
-    auto dumpResult = tag->dump(keys, printPercent, printPercent);
161
-    std::cout << "\r";
161
+    auto dumpResult = tag->dump(keys, printPercentMapKeys, printPercentDump);
162
+    if (isatty(1)) {
163
+        std::cout << "\r";
164
+    }
162
     if (!dumpResult) {
165
     if (!dumpResult) {
163
         dumpResult.print();
166
         dumpResult.print();
164
         return 7;
167
         return 7;
191
 
194
 
192
 void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
195
 void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
193
 {
196
 {
194
-    std::cout << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : "") << (accessBits.canKeyBReadBlock(block) ? "B" : "");
195
-    std::cout << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : "") << (accessBits.canKeyBWriteBlock(block) ? "B" : "");
196
-    std::cout << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : "") << (accessBits.canKeyBIncrementBlock(block) ? "B" : "");
197
-    std::cout << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : "") << (accessBits.canKeyBDecrementBlock(block) ? "B" : "") << std::endl;
197
+    std::cout << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : " ") << (accessBits.canKeyBReadBlock(block) ? "B" : " ");
198
+    std::cout << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : " ") << (accessBits.canKeyBWriteBlock(block) ? "B" : " ");
199
+    std::cout << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : " ") << (accessBits.canKeyBIncrementBlock(block) ? "B" : " ");
200
+    std::cout << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : " ") << (accessBits.canKeyBDecrementBlock(block) ? "B" : " ") << std::endl;
198
 }
201
 }
199
 
202
 
200
 void MainClass::printTrailerAccessBits(const AccessBitsDbo &accessBits)
203
 void MainClass::printTrailerAccessBits(const AccessBitsDbo &accessBits)
201
 {
204
 {
202
-    std::cout << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : "") << (accessBits.canKeyBReadKeyATrailer() ? "B" : "");
203
-    std::cout << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : "") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : "");
205
+    std::cout << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyATrailer() ? "B" : " ");
206
+    std::cout << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : " ");
204
 
207
 
205
-    std::cout << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : "") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : "");
206
-    std::cout << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : "") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : "");
208
+    std::cout << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : " ");
209
+    std::cout << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : " ");
207
 
210
 
208
-    std::cout << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : "") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : "");
209
-    std::cout << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : "") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : "") << std::endl;;
211
+    std::cout << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : " ");
212
+    std::cout << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : " ") << std::endl;;
213
+}
214
+
215
+void MainClass::printPercent(int done, int total, const std::string& header)
216
+{
217
+    if (isatty(1)) {
218
+        std::cout << "\r\033[2K" << header << ": " << std::fixed << std::setprecision(1)
219
+        << ((float) done / (float) total * 100.0) << "%" << std::flush;
220
+    }
221
+//    std::cout << std::fixed << std::setprecision(1) << ((float)done / (float)total * 100.0) << "%" << std::endl;
222
+}
223
+
224
+void MainClass::printPercentMapKeys(int done, int total)
225
+{
226
+    printPercent(done, total, "Mapping keys");
210
 }
227
 }
211
 
228
 
212
-void MainClass::printPercent(int done, int total)
229
+void MainClass::printPercentDump(int done, int total)
213
 {
230
 {
214
-    std::cout << "\r\033[2K" << std::fixed << std::setprecision(1) << ((float)done / (float)total * 100.0) << "%" << std::flush;
231
+    printPercent(done, total, "Dumping");
215
 }
232
 }
216
 
233
 
217
 void MainClass::printVersion() const
234
 void MainClass::printVersion() const

+ 5
- 1
cli/Interface/MainClass.h View File

31
 
31
 
32
     void printVersion() const;
32
     void printVersion() const;
33
 
33
 
34
-    static void printPercent(int done, int total);
34
+    static void printPercent(int done, int total, const std::string& header);
35
+
36
+    static void printPercentMapKeys(int done, int total);
37
+
38
+    static void printPercentDump(int done, int total);
35
 
39
 
36
 private:
40
 private:
37
     int _argc;
41
     int _argc;

+ 34
- 46
src/Business/FreeFareTagBusiness.cpp View File

9
 {
9
 {
10
 }
10
 }
11
 
11
 
12
-ResultBool FreeFareTagBusiness::authenticate(int sector, int block, std::string key, int keyType)
12
+ResultBool FreeFareTagBusiness::authenticate(int sector, std::string key, int keyType)
13
 {
13
 {
14
-    return _tag->authenticate(sector, block, key, keyType);
14
+    return _tag->authenticate(sector, key, keyType);
15
 }
15
 }
16
 
16
 
17
 ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
17
 ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
52
 
52
 
53
 Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
53
 Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
54
 {
54
 {
55
-    std::vector<std::vector<std::pair<std::string, std::string>>> mappedKeys;
55
+    MappedKeys mappedKeys;
56
 
56
 
57
     for (int i = 0; i < 16; ++i) {
57
     for (int i = 0; i < 16; ++i) {
58
-        std::vector<std::pair<std::string, std::string>> sectorKeys;
59
-        for (int j = 0; j < 4; ++j) {
60
-            std::pair<std::string, std::string> blockKeys;
61
-            for (int k = 0; k < keys.size(); ++k) {
62
-                auto key = keys[k];
63
-                if (authenticate(i, j, key, MFC_KEY_A)) {
64
-                    blockKeys.first = key;
65
-                }
66
-                if (authenticate(i, j, key, MFC_KEY_B)) {
67
-                    blockKeys.second = key;
68
-                }
69
-                if (cb != 0) {
70
-                    cb((i * 4 * keys.size()) + (j * keys.size()) + k + 1, 16 * 4 * keys.size());
71
-                }
72
-                if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
73
-                    break;
74
-                }
58
+        std::pair<std::string, std::string> blockKeys;
59
+        for (int k = 0; k < keys.size(); ++k) {
60
+            auto key = keys[k];
61
+            if (authenticate(i, key, MFC_KEY_A)) {
62
+                blockKeys.first = key;
63
+            }
64
+            if (authenticate(i, key, MFC_KEY_B)) {
65
+                blockKeys.second = key;
66
+            }
67
+            if (cb != 0) {
68
+                cb((i * keys.size()) + k + 1, 16 * keys.size());
69
+            }
70
+            if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
71
+                break;
75
             }
72
             }
76
-            sectorKeys.push_back(blockKeys);
77
         }
73
         }
78
-        mappedKeys.push_back(sectorKeys);
74
+        mappedKeys.push_back(blockKeys);
79
     }
75
     }
80
 
76
 
81
-    return Result<std::vector<std::vector<std::pair<std::string, std::string>>>>::ok(mappedKeys);
77
+    return Result<MappedKeys>::ok(mappedKeys);
82
 }
78
 }
83
 
79
 
84
 Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
80
 Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
86
     if (keys.size() != 16) {
82
     if (keys.size() != 16) {
87
         return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
83
         return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
88
     }
84
     }
89
-    for (int i = 0; i < keys.size(); ++i) {
90
-        auto key = keys[i];
91
-        if (key.size() != 4) {
92
-            return Result<std::vector<SectorDbo>>::error("Must have 4 keys in sector " + i);
93
-        }
94
-    }
95
     std::vector<SectorDbo> sectors;
85
     std::vector<SectorDbo> sectors;
96
     for (int s = 0; s < keys.size(); ++s) {
86
     for (int s = 0; s < keys.size(); ++s) {
97
-        auto sectorKeys = keys[s];
87
+        auto sectorKey = keys[s];
98
         SectorDbo sector;
88
         SectorDbo sector;
99
         bool keyA = false;
89
         bool keyA = false;
100
         bool keyB = false;
90
         bool keyB = false;
101
-        for (int b = 0; b < sectorKeys.size() - 1; ++b) {
102
-            auto blockKey = sectorKeys[b];
91
+        for (int b = 0; b < 3; ++b) {
103
             std::string data = "";
92
             std::string data = "";
104
-            if (!blockKey.first.empty()) {
105
-                auto blockResult = readBlock(s, b, blockKey.first, MFC_KEY_A);
93
+            if (!sectorKey.first.empty()) {
94
+                auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
106
                 if (blockResult) {
95
                 if (blockResult) {
107
                     data = blockResult.getData();
96
                     data = blockResult.getData();
108
                     keyA = true;
97
                     keyA = true;
109
                 }
98
                 }
110
             }
99
             }
111
-            if (!blockKey.second.empty()) {
112
-                auto blockResult = readBlock(s, b, blockKey.second, MFC_KEY_B);
100
+            if (!sectorKey.second.empty()) {
101
+                auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
113
                 if (blockResult) {
102
                 if (blockResult) {
114
                     if (data.empty()) {
103
                     if (data.empty()) {
115
                         data = blockResult.getData();
104
                         data = blockResult.getData();
119
             }
108
             }
120
             sector.setBlock(b, data);
109
             sector.setBlock(b, data);
121
             if (cb != 0) {
110
             if (cb != 0) {
122
-                cb((s * sectorKeys.size()) + b + 1, keys.size() * sectorKeys.size());
111
+                cb((s * 4) + b + 1, keys.size() * 4);
123
             }
112
             }
124
         }
113
         }
125
-        int b = sectorKeys.size() - 1;
126
-        auto blockKey = sectorKeys[b];
114
+        int b = 3;
127
         std::string dataA = "";
115
         std::string dataA = "";
128
         std::string dataB = "";
116
         std::string dataB = "";
129
-        if (!blockKey.first.empty()) {
130
-            auto blockResult = readBlock(s, b, blockKey.first, MFC_KEY_A);
117
+        if (!sectorKey.first.empty()) {
118
+            auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
131
             if (blockResult) {
119
             if (blockResult) {
132
                 dataA = blockResult.getData();
120
                 dataA = blockResult.getData();
133
                 keyA = true;
121
                 keyA = true;
134
             }
122
             }
135
         }
123
         }
136
-        if (!blockKey.second.empty()) {
137
-            auto blockResult = readBlock(s, b, blockKey.second, MFC_KEY_B);
124
+        if (!sectorKey.second.empty()) {
125
+            auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
138
             if (blockResult) {
126
             if (blockResult) {
139
                 dataB = blockResult.getData();
127
                 dataB = blockResult.getData();
140
                 keyB = true;
128
                 keyB = true;
141
             }
129
             }
142
         }
130
         }
143
         if (cb != 0) {
131
         if (cb != 0) {
144
-            cb((s * sectorKeys.size()) + b + 1, keys.size() * sectorKeys.size());
132
+            cb((s * 4) + b + 1, keys.size() * 4);
145
         }
133
         }
146
 
134
 
147
         sector.setBlock(b, dataA);
135
         sector.setBlock(b, dataA);
148
         AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
136
         AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
149
         sector.setBlock(b, dataB);
137
         sector.setBlock(b, dataB);
150
         AccessBitsDbo accessBitsDboB = sector.getAccessBitsDbo();
138
         AccessBitsDbo accessBitsDboB = sector.getAccessBitsDbo();
151
-        sector.setKeyA(keyA ? blockKey.first : "");
152
-        sector.setKeyB(keyB ? blockKey.second : "");
139
+        sector.setKeyA(keyA ? sectorKey.first : "");
140
+        sector.setKeyB(keyB ? sectorKey.second : "");
153
 
141
 
154
         std::string accessBits;
142
         std::string accessBits;
155
         if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {
143
         if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {

+ 2
- 2
src/Business/FreeFareTagBusiness.h View File

10
 #include <DataAccess/FreeFareTag.h>
10
 #include <DataAccess/FreeFareTag.h>
11
 #include <DataAccess/SectorDbo.h>
11
 #include <DataAccess/SectorDbo.h>
12
 
12
 
13
-typedef std::vector<std::vector<std::pair<std::string, std::string>>> MappedKeys;
13
+typedef std::vector<std::pair<std::string, std::string>> MappedKeys;
14
 
14
 
15
 class FreeFareTagBusiness
15
 class FreeFareTagBusiness
16
 {
16
 {
17
 public:
17
 public:
18
     FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag);
18
     FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag);
19
 
19
 
20
-    ResultBool authenticate(int sector, int block, std::string key, int keyType);
20
+    ResultBool authenticate(int sector, std::string key, int keyType);
21
 
21
 
22
     ResultString readBlock(int sector, int block, std::string key, int keyType);
22
     ResultString readBlock(int sector, int block, std::string key, int keyType);
23
 
23
 

+ 4
- 6
src/DataAccess/FreeFareTag.cpp View File

18
     freefare_free_tag(_tag);
18
     freefare_free_tag(_tag);
19
 }
19
 }
20
 
20
 
21
-ResultBool FreeFareTag::authenticate(int sector, int block, std::string key, int keyType)
21
+ResultBool FreeFareTag::authenticate(int sector, std::string key, int keyType)
22
 {
22
 {
23
+
23
     if (mifare_classic_connect(_tag) != 0) {
24
     if (mifare_classic_connect(_tag) != 0) {
24
         return ResultBool::error("Failed to connect to MIFARE tag");
25
         return ResultBool::error("Failed to connect to MIFARE tag");
25
     }
26
     }
26
-
27
-    block = mifare_classic_sector_first_block((MifareClassicBlockNumber)sector) + block;
27
+    int block = mifare_classic_sector_last_block((MifareClassicBlockNumber)sector);
28
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
28
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
29
                                     (MifareClassicKeyType)keyType) != 0) {
29
                                     (MifareClassicKeyType)keyType) != 0) {
30
         return ResultBool::error("Failed to authenticate to MIFARE tag");
30
         return ResultBool::error("Failed to authenticate to MIFARE tag");
31
     }
31
     }
32
-
33
     mifare_classic_disconnect(_tag);
32
     mifare_classic_disconnect(_tag);
34
     return ResultBool::ok(true);
33
     return ResultBool::ok(true);
35
 }
34
 }
36
 
35
 
37
 ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
36
 ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
38
 {
37
 {
38
+
39
     if (mifare_classic_connect(_tag) != 0) {
39
     if (mifare_classic_connect(_tag) != 0) {
40
         return ResultString::error("Failed to connect to MIFARE tag");
40
         return ResultString::error("Failed to connect to MIFARE tag");
41
     }
41
     }
42
-
43
     block = mifare_classic_sector_first_block((MifareClassicBlockNumber)sector) + block;
42
     block = mifare_classic_sector_first_block((MifareClassicBlockNumber)sector) + block;
44
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
43
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
45
                                     (MifareClassicKeyType)keyType) != 0) {
44
                                     (MifareClassicKeyType)keyType) != 0) {
50
     if (mifare_classic_read(_tag, (MifareClassicBlockNumber)block, &data)) {
49
     if (mifare_classic_read(_tag, (MifareClassicBlockNumber)block, &data)) {
51
         return ResultString::error("Failed to read MIFARE tag data");
50
         return ResultString::error("Failed to read MIFARE tag data");
52
     }
51
     }
53
-
54
     mifare_classic_disconnect(_tag);
52
     mifare_classic_disconnect(_tag);
55
     return ResultString::ok(std::string((const char*)data, sizeof(data)));
53
     return ResultString::ok(std::string((const char*)data, sizeof(data)));
56
 }
54
 }

+ 1
- 1
src/DataAccess/FreeFareTag.h View File

16
     FreeFareTag(FreefareTag tag);
16
     FreeFareTag(FreefareTag tag);
17
     ~FreeFareTag();
17
     ~FreeFareTag();
18
 
18
 
19
-    ResultBool authenticate(int sector, int block, std::string key, int keyType);
19
+    ResultBool authenticate(int sector, std::string key, int keyType);
20
 
20
 
21
     ResultString readBlock(int sector, int block, std::string key, int keyType);
21
     ResultString readBlock(int sector, int block, std::string key, int keyType);
22
 
22
 

Loading…
Cancel
Save