Explorar el Código

fixed authentication to sector

develop
Robin Thoni hace 7 años
padre
commit
1eed01d23a

+ 39
- 22
cli/Interface/MainClass.cpp Ver fichero

@@ -121,7 +121,7 @@ int MainClass::main()
121 121
 //    std::vector<std::string> keys;
122 122
 //    keys.push_back(StringUtils::humanToRaw("8829da9daf76").getData());
123 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 126
     int res = dump(tag, keys);
127 127
 //    int res = mapKeys(tag, keys);
@@ -134,21 +134,22 @@ int MainClass::main()
134 134
 
135 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 141
     if (!mappedKeysResult) {
140 142
         mappedKeysResult.print();
141 143
     }
142 144
     else {
143 145
         auto mappedKeys = mappedKeysResult.getData();
144 146
         for (int s = 0; s < mappedKeys.size(); ++s) {
145
-            auto sectorKeys = mappedKeys[s];
147
+            auto sectorKey = mappedKeys[s];
146 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 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,8 +158,10 @@ int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std
157 158
 
158 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 165
     if (!dumpResult) {
163 166
         dumpResult.print();
164 167
         return 7;
@@ -191,27 +194,41 @@ int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::s
191 194
 
192 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 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 234
 void MainClass::printVersion() const

+ 5
- 1
cli/Interface/MainClass.h Ver fichero

@@ -31,7 +31,11 @@ public:
31 31
 
32 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 40
 private:
37 41
     int _argc;

+ 34
- 46
src/Business/FreeFareTagBusiness.cpp Ver fichero

@@ -9,9 +9,9 @@ FreeFareTagBusiness::FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag)
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 17
 ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
@@ -52,33 +52,29 @@ std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
52 52
 
53 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 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 80
 Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
@@ -86,30 +82,23 @@ Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::f
86 82
     if (keys.size() != 16) {
87 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 85
     std::vector<SectorDbo> sectors;
96 86
     for (int s = 0; s < keys.size(); ++s) {
97
-        auto sectorKeys = keys[s];
87
+        auto sectorKey = keys[s];
98 88
         SectorDbo sector;
99 89
         bool keyA = false;
100 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 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 95
                 if (blockResult) {
107 96
                     data = blockResult.getData();
108 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 102
                 if (blockResult) {
114 103
                     if (data.empty()) {
115 104
                         data = blockResult.getData();
@@ -119,37 +108,36 @@ Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::f
119 108
             }
120 109
             sector.setBlock(b, data);
121 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 115
         std::string dataA = "";
128 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 119
             if (blockResult) {
132 120
                 dataA = blockResult.getData();
133 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 126
             if (blockResult) {
139 127
                 dataB = blockResult.getData();
140 128
                 keyB = true;
141 129
             }
142 130
         }
143 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 135
         sector.setBlock(b, dataA);
148 136
         AccessBitsDbo accessBitsDboA = sector.getAccessBitsDbo();
149 137
         sector.setBlock(b, dataB);
150 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 142
         std::string accessBits;
155 143
         if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {

+ 2
- 2
src/Business/FreeFareTagBusiness.h Ver fichero

@@ -10,14 +10,14 @@
10 10
 #include <DataAccess/FreeFareTag.h>
11 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 15
 class FreeFareTagBusiness
16 16
 {
17 17
 public:
18 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 22
     ResultString readBlock(int sector, int block, std::string key, int keyType);
23 23
 

+ 4
- 6
src/DataAccess/FreeFareTag.cpp Ver fichero

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

+ 1
- 1
src/DataAccess/FreeFareTag.h Ver fichero

@@ -16,7 +16,7 @@ public:
16 16
     FreeFareTag(FreefareTag tag);
17 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 21
     ResultString readBlock(int sector, int block, std::string key, int keyType);
22 22
 

Loading…
Cancelar
Guardar