| 1 | /* |
| 2 | * |
| 3 | * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory |
| 4 | * trade-off using rainbow tables. |
| 5 | * |
| 6 | * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude |
| 7 | * Hochreutiner, Andreas Huber and Etienne Dysli. |
| 8 | * |
| 9 | * Copyright (c) 2008 Philippe Oechslin, Cedric Tissieres, Bertrand Mesot |
| 10 | * |
| 11 | * Ophcrack is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * Ophcrack is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with Ophcrack; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | * This program is released under the GPL with the additional exemption |
| 26 | * that compiling, linking, and/or using OpenSSL is allowed. |
| 27 | * |
| 28 | * |
| 29 | * |
| 30 | * |
| 31 | */ |
| 32 | #include <assert.h> |
| 33 | #include <math.h> |
| 34 | |
| 35 | #include "tablemodel.h" |
| 36 | //--------------------------------------------------------------------------- |
| 37 | TableModel::TableModel(QObject *parent) : QStandardItemModel(0, 5, parent) { |
| 38 | // Set the header. |
| 39 | |
| 40 | setHorizontalHeaderItem(TABLE_COL_IDX, new QStandardItem("Table")); |
| 41 | setHorizontalHeaderItem(DIR_COL_IDX, new QStandardItem("Directory")); |
| 42 | setHorizontalHeaderItem(STATUS_COL_IDX, new QStandardItem("Status")); |
| 43 | setHorizontalHeaderItem(PRELOAD_COL_IDX, new QStandardItem("Preload")); |
| 44 | setHorizontalHeaderItem(PROGRESS_COL_IDX, new QStandardItem("Progress")); |
| 45 | |
| 46 | for (int i=0; i<columnCount(); ++i) { |
| 47 | QStandardItem *item = horizontalHeaderItem(i); |
| 48 | item->setTextAlignment(Qt::AlignCenter); |
| 49 | } |
| 50 | } |
| 51 | //--------------------------------------------------------------------------- |
| 52 | void TableModel::install(list_t *tables) { |
| 53 | list_nd_t *nd; |
| 54 | |
| 55 | for (nd = tables->head; nd != 0; nd = nd->next) { |
| 56 | table_t *tbl = (table_t*)nd->data; |
| 57 | insertTable(tbl); |
| 58 | } |
| 59 | } |
| 60 | //--------------------------------------------------------------------------- |
| 61 | void TableModel::uninstall(table_kind_t kind) { |
| 62 | // If this kind of table is not installed, then we do not have |
| 63 | // anything to do. |
| 64 | |
| 65 | if (!isInstalled(kind)) return; |
| 66 | |
| 67 | // Otherwise, we free the memory occupied by the tables and remove |
| 68 | // the corresponding items. |
| 69 | |
| 70 | QStandardItem *root = getRoot(kind); |
| 71 | |
| 72 | int ntables = root->rowCount(); |
| 73 | int i; |
| 74 | |
| 75 | for (i=0; i<ntables; ++i) { |
| 76 | QStandardItem *child = root->child(0, TABLE_COL_IDX); |
| 77 | table_t *tbl = (table_t*)child->data(Qt::UserRole).value<void*>(); |
| 78 | |
| 79 | table_free(tbl); |
| 80 | root->removeRow(0); |
| 81 | } |
| 82 | |
| 83 | // Set the user role to -1 to indicate that the table are not |
| 84 | // installed. |
| 85 | |
| 86 | root->setData(int(-1), Qt::UserRole); |
| 87 | } |
| 88 | //--------------------------------------------------------------------------- |
| 89 | QStandardItem *TableModel::insertRoot(table_kind_t kind) { |
| 90 | QStandardItem *item = itemFromKind.value(kind); |
| 91 | |
| 92 | // If there are no items with the given name, then we create one. |
| 93 | |
| 94 | if (item == 0) { |
| 95 | const char *name = table_string(kind); |
| 96 | int nrows = rowCount(); |
| 97 | int ncols = columnCount(); |
| 98 | |
| 99 | QStandardItemModel::insertRow(nrows); |
| 100 | |
| 101 | for (int i=ncols-1; i >= 0; --i) { |
| 102 | item = new QStandardItem(); |
| 103 | setItem(nrows, i, item); |
| 104 | } |
| 105 | |
| 106 | item->setText(name); |
| 107 | |
| 108 | itemFromKind.insert(kind, item); |
| 109 | updateRootItem(item); |
| 110 | } |
| 111 | |
| 112 | return item; |
| 113 | } |
| 114 | //--------------------------------------------------------------------------- |
| 115 | QStandardItem *TableModel::insertTable(table_t *tbl) { |
| 116 | assert(tbl != 0); |
| 117 | |
| 118 | QStandardItem *root = insertRoot(tbl->kind); |
| 119 | QStandardItem *child = 0; |
| 120 | |
| 121 | QVariant data = QVariant::fromValue((void*)tbl); |
| 122 | int ncols = columnCount(); |
| 123 | |
| 124 | for (int i=ncols-1; i >= 0; --i) { |
| 125 | child = new QStandardItem(); |
| 126 | root->setChild(tbl->idx, i, child); |
| 127 | } |
| 128 | |
| 129 | child->setData(data, Qt::UserRole); |
| 130 | updateTable(tbl); |
| 131 | |
| 132 | return child; |
| 133 | } |
| 134 | //--------------------------------------------------------------------------- |
| 135 | void TableModel::enable(QStandardItem *item, bool value) { |
| 136 | // If we deal with a top level item, then we enable all the children. |
| 137 | |
| 138 | if (item->parent() == 0) { |
| 139 | int n = item->rowCount(); |
| 140 | |
| 141 | for (int i=0; i<n; ++i) |
| 142 | enable(item->child(i, TABLE_COL_IDX), value); |
| 143 | } |
| 144 | |
| 145 | // Otherwise, we enable the corresponding table. |
| 146 | |
| 147 | else { |
| 148 | table_t *tbl = tableFromItem(item); |
| 149 | assert(tbl != 0); |
| 150 | |
| 151 | tbl->enabled = value; |
| 152 | tbl->active = tbl->enabled == 0 ? 0 : tbl->active; |
| 153 | |
| 154 | updateTable(tbl); |
| 155 | } |
| 156 | } |
| 157 | //--------------------------------------------------------------------------- |
| 158 | void TableModel::updateTable(table_t *tbl) { |
| 159 | int idx = tbl->idx; |
| 160 | |
| 161 | QStandardItem *root = itemFromKind.value(tbl->kind); |
| 162 | assert(root != 0); |
| 163 | QStandardItem *child = root->child(idx); |
| 164 | |
| 165 | updateChildItem(child); |
| 166 | updateRootItem(root); |
| 167 | } |
| 168 | //--------------------------------------------------------------------------- |
| 169 | void TableModel::updateItem(QStandardItem *item) { |
| 170 | if (item->parent() == 0) |
| 171 | updateRootItem(item); |
| 172 | else |
| 173 | updateChildItem(item); |
| 174 | } |
| 175 | //--------------------------------------------------------------------------- |
| 176 | table_t *TableModel::tableFromItem(QStandardItem *item) { |
| 177 | if (item->parent() == 0) |
| 178 | return 0; |
| 179 | else |
| 180 | return (table_t*)item->data(Qt::UserRole).value<void*>(); |
| 181 | } |
| 182 | //--------------------------------------------------------------------------- |
| 183 | bool TableModel::isInstalled(QStandardItem *item) { |
| 184 | QStandardItem *root = item->parent(); |
| 185 | |
| 186 | if (root != 0) |
| 187 | return isInstalled(root); |
| 188 | else { |
| 189 | int nenabled = item->data(Qt::UserRole).toInt(); |
| 190 | if (nenabled < 0) return false; |
| 191 | } |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | //--------------------------------------------------------------------------- |
| 196 | bool TableModel::isInstalled(table_kind_t kind) { |
| 197 | QStandardItem *item = itemFromKind.value(kind); |
| 198 | |
| 199 | if (item == 0 || item->data(Qt::UserRole).toInt() == -1) |
| 200 | return false; |
| 201 | else |
| 202 | return true; |
| 203 | } |
| 204 | //--------------------------------------------------------------------------- |
| 205 | QStandardItem *TableModel::getRoot(table_kind_t kind) { |
| 206 | return itemFromKind.value(kind); |
| 207 | } |
| 208 | //--------------------------------------------------------------------------- |
| 209 | void TableModel::insertRow(int row, const QList<QStandardItem*> &items) { |
| 210 | QStandardItemModel::insertRow(row, items); |
| 211 | |
| 212 | QStandardItem *root = item(row, TABLE_COL_IDX); |
| 213 | int nchildren = root->rowCount(); |
| 214 | |
| 215 | for (int i=0; i<nchildren; ++i) { |
| 216 | QStandardItem *child = root->child(i); |
| 217 | emit itemChanged(child); |
| 218 | } |
| 219 | |
| 220 | emit itemChanged(root); |
| 221 | } |
| 222 | //--------------------------------------------------------------------------- |
| 223 | void TableModel::updateRootItem(QStandardItem *root) { |
| 224 | assert(root->parent() == 0); |
| 225 | |
| 226 | int row = indexFromItem(root).row(); |
| 227 | int nchildren = root->rowCount(); |
| 228 | table_t *tbl = 0; |
| 229 | |
| 230 | if (nchildren > 0) |
| 231 | tbl = tableFromItem(root->child(0, TABLE_COL_IDX)); |
| 232 | |
| 233 | // Set the first column of the root item. |
| 234 | |
| 235 | if (tbl != 0) root->setText(QString(tbl->name)); |
| 236 | |
| 237 | // Determine if this group of tables is enabled. |
| 238 | |
| 239 | uint64_t size = 0; |
| 240 | uint64_t psize = 0; |
| 241 | int nenabled = 0; |
| 242 | int nactive = 0; |
| 243 | |
| 244 | for (int i=0; i<nchildren; ++i) { |
| 245 | QStandardItem *child = root->child(i, TABLE_COL_IDX); |
| 246 | table_t *tbl = tableFromItem(child); |
| 247 | table_preload_t state = table_preload_state(tbl); |
| 248 | |
| 249 | if (tbl->enabled) ++nenabled; |
| 250 | if (tbl->active) ++nactive; |
| 251 | |
| 252 | size += table_size(tbl) >> 20; |
| 253 | psize += table_preload_size(tbl, state) >> 20; |
| 254 | } |
| 255 | |
| 256 | // Store the number of children enabled into the root or -1 if the |
| 257 | // item has no children, i.e., the table is not installed. |
| 258 | |
| 259 | if (nchildren > 0) |
| 260 | root->setData(int(nenabled), Qt::UserRole); |
| 261 | else |
| 262 | root->setData(int(-1), Qt::UserRole); |
| 263 | |
| 264 | if (nchildren == 0) |
| 265 | root->setIcon(QIcon(":/icons/pixmaps/notinstalled.png")); |
| 266 | else if (nenabled > 0) |
| 267 | root->setIcon(QIcon(":/icons/pixmaps/enabled.png")); |
| 268 | else |
| 269 | root->setIcon(QIcon(":/icons/pixmaps/disabled.png")); |
| 270 | |
| 271 | // Set the path to the tables. |
| 272 | |
| 273 | root = item(row, DIR_COL_IDX); |
| 274 | if (tbl != 0) root->setText(QString(tbl->path)); |
| 275 | |
| 276 | // Set the status of the tables. |
| 277 | |
| 278 | root = item(row, STATUS_COL_IDX); |
| 279 | root->setTextAlignment(Qt::AlignCenter); |
| 280 | |
| 281 | if (nchildren == 0) |
| 282 | root->setText(QString("not installed")); |
| 283 | |
| 284 | else if (nenabled > 0) |
| 285 | if (nactive > 0) |
| 286 | root->setText("active"); |
| 287 | else |
| 288 | root->setText("inactive"); |
| 289 | |
| 290 | else |
| 291 | root->setText("disabled"); |
| 292 | |
| 293 | // Set the preload state. |
| 294 | |
| 295 | root = item(row, PRELOAD_COL_IDX); |
| 296 | root->setTextAlignment(Qt::AlignCenter); |
| 297 | |
| 298 | if (psize > 0) { |
| 299 | int ratio = (int)ceil(100.*psize / size); |
| 300 | QString str = QString("%1% in RAM").arg(ratio); |
| 301 | root->setText(str); |
| 302 | } |
| 303 | else |
| 304 | root->setText("on disk"); |
| 305 | } |
| 306 | //--------------------------------------------------------------------------- |
| 307 | void TableModel::updateChildItem(QStandardItem *child) { |
| 308 | QStandardItem *root = child->parent(); |
| 309 | assert(root != 0); |
| 310 | table_t *tbl = tableFromItem(child); |
| 311 | |
| 312 | assert(tbl != 0); |
| 313 | int idx = tbl->idx; |
| 314 | |
| 315 | // Set the table name and icon. |
| 316 | |
| 317 | child->setText(QString("table%1").arg(idx)); |
| 318 | |
| 319 | if (tbl->enabled) |
| 320 | child->setIcon(QIcon(":/icons/pixmaps/enabled.png")); |
| 321 | else |
| 322 | child->setIcon(QIcon(":/icons/pixmaps/disabled.png")); |
| 323 | |
| 324 | // Set the table status. |
| 325 | |
| 326 | child = root->child(idx, STATUS_COL_IDX); |
| 327 | child->setTextAlignment(Qt::AlignCenter); |
| 328 | |
| 329 | if (tbl->active) |
| 330 | child->setText("active"); |
| 331 | |
| 332 | else if (tbl->enabled) |
| 333 | child->setText("inactive"); |
| 334 | |
| 335 | else |
| 336 | child->setText("disabled"); |
| 337 | |
| 338 | // Set the preload state. |
| 339 | |
| 340 | child = root->child(idx, PRELOAD_COL_IDX); |
| 341 | child->setTextAlignment(Qt::AlignCenter); |
| 342 | |
| 343 | table_preload_t state = table_preload_state(tbl); |
| 344 | |
| 345 | uint64_t size = table_size(tbl) >> 20; |
| 346 | uint64_t psize = table_preload_size(tbl, state) >> 20; |
| 347 | |
| 348 | if (psize > 0) { |
| 349 | int ratio = (int)ceil(100.*psize / size); |
| 350 | QString str = QString("%1% in RAM").arg(ratio); |
| 351 | child->setText(str); |
| 352 | } |
| 353 | else |
| 354 | child->setText("on disk"); |
| 355 | } |
| 356 | //--------------------------------------------------------------------------- |
| 357 | void TableModel::updateProgress(void) { |
| 358 | int nrows = rowCount(); |
| 359 | |
| 360 | int count = 0; |
| 361 | int total_cmin = 0; |
| 362 | int total_cmax = 0; |
| 363 | |
| 364 | for (int i=0; i<nrows; ++i) { |
| 365 | QStandardItem *root = item(i, TABLE_COL_IDX); |
| 366 | |
| 367 | int nenabled = root->data(Qt::UserRole).toInt(); |
| 368 | int nchildren = root->rowCount(); |
| 369 | int mean_cmin = 0; |
| 370 | int mean_cmax = 0; |
| 371 | |
| 372 | if (nenabled <= 0) continue; |
| 373 | |
| 374 | for (int j=0; j<nchildren; ++j) { |
| 375 | QStandardItem *child = root->child(j, TABLE_COL_IDX); |
| 376 | table_t *tbl = tableFromItem(child); |
| 377 | |
| 378 | assert(tbl != 0); |
| 379 | if (!tbl->enabled) continue; |
| 380 | |
| 381 | uint64_t ncols = tbl->ncols; |
| 382 | uint64_t cmin = tbl->cmin; |
| 383 | uint64_t cmax = tbl->cmax; |
| 384 | |
| 385 | int ratio_cmin = 0; |
| 386 | int ratio_cmax = 0; |
| 387 | |
| 388 | if (ncols > 0) { |
| 389 | uint64_t n = ncols * ncols; |
| 390 | uint64_t dcmin = ncols-cmin; |
| 391 | uint64_t dcmax = ncols-cmax; |
| 392 | |
| 393 | ratio_cmin = (1000*dcmin*dcmin) / n; |
| 394 | ratio_cmax = (1000*dcmax*dcmax) / n; |
| 395 | } |
| 396 | |
| 397 | QPoint pt = QPoint(ratio_cmin, ratio_cmax); |
| 398 | |
| 399 | child = root->child(j, PROGRESS_COL_IDX); |
| 400 | child->setData(pt, Qt::UserRole); |
| 401 | |
| 402 | mean_cmin += ratio_cmin; |
| 403 | mean_cmax += ratio_cmax; |
| 404 | } |
| 405 | |
| 406 | count += nenabled; |
| 407 | |
| 408 | total_cmin += mean_cmin; |
| 409 | total_cmax += mean_cmax; |
| 410 | |
| 411 | mean_cmin /= nenabled; |
| 412 | mean_cmax /= nenabled; |
| 413 | |
| 414 | QPoint pt = QPoint(mean_cmin, mean_cmax); |
| 415 | |
| 416 | root = item(i, PROGRESS_COL_IDX); |
| 417 | root->setData(pt, Qt::UserRole); |
| 418 | } |
| 419 | |
| 420 | if (count > 0) { |
| 421 | total_cmin /= count; |
| 422 | total_cmax /= count; |
| 423 | } |
| 424 | |
| 425 | QPoint pt = QPoint(total_cmin, total_cmax); |
| 426 | |
| 427 | QStandardItem *root = invisibleRootItem(); |
| 428 | root->setData(pt, Qt::UserRole); |
| 429 | } |