Skip to content

Commit

Permalink
stuff works need to aufräumen
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Sep 9, 2016
1 parent 52bb1d2 commit 3a40108
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
6 changes: 6 additions & 0 deletions noncestatistics.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
E97845941D7EFD5B00798C24 /* idevicerestore.c in Sources */ = {isa = PBXBuildFile; fileRef = E978458C1D7EFD5B00798C24 /* idevicerestore.c */; };
E97845951D7EFD5B00798C24 /* normal.c in Sources */ = {isa = PBXBuildFile; fileRef = E978458E1D7EFD5B00798C24 /* normal.c */; };
E97845961D7EFD5B00798C24 /* recovery.c in Sources */ = {isa = PBXBuildFile; fileRef = E97845911D7EFD5B00798C24 /* recovery.c */; };
E98204B21D82FEB90005560C /* stats.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E98204B01D82FEB90005560C /* stats.cpp */; };
E9AD3FBD1D82D1CA007C843E /* libirecovery.2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E9AD3FBC1D82D1CA007C843E /* libirecovery.2.dylib */; };
E9AD3FBF1D82D1E6007C843E /* libplist.3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E9AD3FBE1D82D1E6007C843E /* libplist.3.dylib */; };
E9AD3FC31D82D22B007C843E /* libimobiledevice.6.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E9AD3FC21D82D22B007C843E /* libimobiledevice.6.dylib */; };
Expand Down Expand Up @@ -44,6 +45,8 @@
E978458F1D7EFD5B00798C24 /* normal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = normal.h; sourceTree = "<group>"; };
E97845901D7EFD5B00798C24 /* recovery.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = recovery.h; sourceTree = "<group>"; };
E97845911D7EFD5B00798C24 /* recovery.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = recovery.c; sourceTree = "<group>"; };
E98204B01D82FEB90005560C /* stats.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = stats.cpp; sourceTree = "<group>"; };
E98204B11D82FEB90005560C /* stats.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = stats.hpp; sourceTree = "<group>"; };
E9AD3FBC1D82D1CA007C843E /* libirecovery.2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libirecovery.2.dylib; path = ../../../../../usr/local/lib/libirecovery.2.dylib; sourceTree = "<group>"; };
E9AD3FBE1D82D1E6007C843E /* libplist.3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libplist.3.dylib; path = ../../../../../usr/local/Cellar/libplist/1.12/lib/libplist.3.dylib; sourceTree = "<group>"; };
E9AD3FC21D82D22B007C843E /* libimobiledevice.6.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libimobiledevice.6.dylib; path = ../../../../../usr/local/lib/libimobiledevice.6.dylib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,6 +100,8 @@
E97845901D7EFD5B00798C24 /* recovery.h */,
E97845911D7EFD5B00798C24 /* recovery.c */,
E97845801D7EF5F400798C24 /* main.cpp */,
E98204B01D82FEB90005560C /* stats.cpp */,
E98204B11D82FEB90005560C /* stats.hpp */,
);
path = noncestatistics;
sourceTree = "<group>";
Expand Down Expand Up @@ -158,6 +163,7 @@
buildActionMask = 2147483647;
files = (
E97845961D7EFD5B00798C24 /* recovery.c in Sources */,
E98204B21D82FEB90005560C /* stats.cpp in Sources */,
E97845931D7EFD5B00798C24 /* dfu.c in Sources */,
E97845951D7EFD5B00798C24 /* normal.c in Sources */,
E97845921D7EFD5B00798C24 /* common.c in Sources */,
Expand Down
22 changes: 18 additions & 4 deletions noncestatistics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#include <signal.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include "stats.hpp"

#define USEC_PER_SEC 1000000

static struct option longopts[] = {
{ "ecid", no_argument, NULL, 'e' },
{ "times", no_argument, NULL, 't'},
{ "abort", no_argument, NULL, 'a'},
{ "statistics", no_argument, NULL, 's'},
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
Expand All @@ -28,8 +30,9 @@ void cmd_help(){
printf(" -h, --help\t\t\tprints usage information\n");
printf(" -e, --ecid ECID\t\tmanually specify ECID of the device. Uses any device if not specified\n");
printf(" -t, --times amount\t\tspeficy how many NONCES are collected. If not specified it will collect nonces until you enter ctrl+c\n");
printf(" -a, --abort\t\tresets device to normal mode");
printf(" FILE\t\tFile to write statistics to\n");
printf(" -a, --abort\t\tresets device to normal mode\n");
printf(" -s, --statistics\t\tprint statistics from nonce file\n");
printf(" FILE\t\tFile to write nonces to (or read from when using '-s' or '--statistics')\n");
printf("\n");
}

Expand Down Expand Up @@ -86,7 +89,7 @@ int main(int argc, const char * argv[]) {
int times = 0;
int optindex = 0;
int opt = 0;
while ((opt = getopt_long(argc, (char* const *)argv, "he:t:a", longopts, &optindex)) > 0) {
while ((opt = getopt_long(argc, (char* const *)argv, "he:t:as", longopts, &optindex)) > 0) {
switch (opt) {
case 'h': // long option: "help"; can be called as short option
cmd_help();
Expand All @@ -100,6 +103,15 @@ int main(int argc, const char * argv[]) {
case 'a': // long option: "abort"; can be called as short option
only_abort = true;
break;
case 's': // long option: "statistics"; can be called ad short option
if (argc < 2) {
std::cout << "You must specify a filename as last argument!" << std::endl;
cmd_help();
return -1;
}
cmd_statistics(argv[argc-1]);
return 0;
break;
default:
cmd_help();
return -1;
Expand All @@ -122,8 +134,10 @@ int main(int argc, const char * argv[]) {

info("Identified device as %s, %s ", client->device->hardware_model, client->device->product_type);
if (!only_abort) {
if (argc<2) {
if (argc>=2) {
std::cout << "You must specify a filename as last argument!" << std::endl;
cmd_help();
return -1;
}
const char *filename = argv[argc-1];

Expand Down
107 changes: 107 additions & 0 deletions noncestatistics/stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "stats.hpp"


#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <regex>
#include <fstream>

bool isHex(char c){
return isdigit(c) || ('a'<= c && c <= 'f');
}

char* getNonceFromLine(char* buf, int len){
bool foundNonce = false;
int i = 0;
int startpos = 0;
while (i<(len-40) && buf[i+40] != '\n') {
for (int j=0; j<20; j++) {
if (!isHex(buf[j])) {
break;
}else if (j==40){
foundNonce = true;
}
}
if (foundNonce) {
startpos = i;
break;
}
}
if (foundNonce) {
return buf+startpos;
}else{
return nullptr;
}

}


char* getNextLinePtr(char* buf, int len){
int i = 0;
while (i < len && buf[i] != '\n') {
i++;
}
if ((i+1) >= len) {
return nullptr;
}else{
return buf+i+1;
}
}


std::map<std::string, int> createNonceList(char* buf, int len){
std::map<std::string, int> nonceList;
char* position = buf;
int i=0;
while (i<len) {
const char* nonce = getNonceFromLine(position, len);
if (nonce != nullptr) {
std::string nonceStr(nonce, 40);
nonceList[nonceStr] = nonceList[nonceStr]+1;
}
position = getNextLinePtr(buf, len);
if (position == nullptr) {
break;
}
}
return nonceList;
}

std::vector<std::pair<std::string, int>> sortNonceList(std::map<std::string, int>& nonceList){
std::vector<std::pair<std::string, int>> sortedList;

for (auto p : nonceList){ sortedList.push_back(p); }
std::sort(sortedList.begin(), sortedList.end(), [](std::pair<std::string, int> &a, std::pair<std::string, int> &b){
return a.second < b.second;
});

return sortedList;
}


void cmd_statistics(const char* filename){
std::ifstream myfile;
myfile.open(filename);
std::string line;

std::map<std::string, int> nonceList;


while ((myfile >> line)) {
std::regex r("[[:digit:]a-f]{40}");
std::smatch match;
if(std::regex_search(line, match, r)){
nonceList[match[0]]++;
}

}

std::vector<std::pair<std::string, int>> sortedList = sortNonceList(nonceList);
for (auto p: sortedList) {
std::cout << p.first << " -- " << p.second << std::endl;
}
}
15 changes: 15 additions & 0 deletions noncestatistics/stats.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef stats_hpp
#define stats_hpp

#include <map>
#include <vector>
#include <string>

char* getNonceFromLine(char* buf, int len);
char* getNextLinePtr(char* buf, int len);
std::map<std::string, int> createNonceList(char* buf, int len);
std::vector<std::pair<std::string, int>> sortNonceList(std::map<std::string, int>& nonceList);
void cmd_statistics(const char* filename);


#endif /* stats_hpp */

0 comments on commit 3a40108

Please sign in to comment.