├── Makefile.am ├── autogen.sh ├── configure.ac ├── igetnonce.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── igetnonce ├── Makefile.am ├── all_igetnonce.h ├── common.c ├── common.h ├── dfu.c ├── dfu.h ├── endianness.h ├── idevicerestore.c ├── idevicerestore.h ├── main.c ├── normal.c ├── normal.h ├── recovery.c └── recovery.h └── setBuildVersion.sh /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS=igetnonce 2 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gprefix=`which glibtoolize 2>&1 >/dev/null` 3 | if [ $? -eq 0 ]; then 4 | glibtoolize --force 5 | else 6 | libtoolize --force 7 | fi 8 | aclocal -I m4 9 | autoconf 10 | autoheader 11 | automake --add-missing 12 | 13 | if [ -z $NOCONFIGURE ]; then 14 | ./configure "$@" 15 | fi 16 | ./setBuildVersion.sh 17 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_PREREQ([2.69]) 2 | AC_INIT([igetnonce], [1.0], [tihmstar@gmail.com]) 3 | 4 | AM_INIT_AUTOMAKE([foreign]) 5 | 6 | AC_CONFIG_SRCDIR([igetnonce/all_igetnonce.h]) 7 | AC_CONFIG_HEADERS([config.h]) 8 | 9 | # Checks for programs. 10 | AC_PROG_CC 11 | CFLAGS+=" -std=c11" 12 | 13 | # Checks for libraries. 14 | PKG_CHECK_MODULES(libimobiledevice, libimobiledevice-1.0 >= 1.2.1) 15 | PKG_CHECK_MODULES(libirecovery, libirecovery >= 0.2.0) 16 | PKG_CHECK_MODULES(libusbmuxd, libusbmuxd >= 0.29.1) 17 | PKG_CHECK_MODULES(openssl, openssl >= 0.9.8) 18 | 19 | # Checks for header files. 20 | AC_CHECK_HEADERS([stdint.h stdlib.h string.h unistd.h]) 21 | 22 | # Checks for typedefs, structures, and compiler characteristics. 23 | AC_TYPE_INT64_T 24 | AC_TYPE_SIZE_T 25 | AC_TYPE_UINT32_T 26 | AC_TYPE_UINT64_T 27 | 28 | # Checks for library functions. 29 | AC_FUNC_ERROR_AT_LINE 30 | AC_FUNC_FSEEKO 31 | AC_FUNC_MALLOC 32 | AC_CHECK_FUNCS([memset strdup strrchr]) 33 | 34 | AC_OUTPUT([ 35 | Makefile 36 | igetnonce/Makefile 37 | ]) 38 | -------------------------------------------------------------------------------- /igetnonce.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 873FC1291E22D7B40089AE60 /* common.c in Sources */ = {isa = PBXBuildFile; fileRef = 873FC11E1E22D7B40089AE60 /* common.c */; }; 11 | 873FC12A1E22D7B40089AE60 /* dfu.c in Sources */ = {isa = PBXBuildFile; fileRef = 873FC1201E22D7B40089AE60 /* dfu.c */; }; 12 | 873FC12B1E22D7B40089AE60 /* idevicerestore.c in Sources */ = {isa = PBXBuildFile; fileRef = 873FC1231E22D7B40089AE60 /* idevicerestore.c */; }; 13 | 873FC12C1E22D7B40089AE60 /* normal.c in Sources */ = {isa = PBXBuildFile; fileRef = 873FC1251E22D7B40089AE60 /* normal.c */; }; 14 | 873FC12D1E22D7B40089AE60 /* recovery.c in Sources */ = {isa = PBXBuildFile; fileRef = 873FC1271E22D7B40089AE60 /* recovery.c */; }; 15 | 873FC1301E22D7E40089AE60 /* libimobiledevice.6.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 873FC12F1E22D7E40089AE60 /* libimobiledevice.6.dylib */; }; 16 | 873FC1321E22D7F60089AE60 /* libirecovery.2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 873FC1311E22D7F60089AE60 /* libirecovery.2.dylib */; }; 17 | 873FC1341E22D80A0089AE60 /* libplist.3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 873FC1331E22D80A0089AE60 /* libplist.3.dylib */; }; 18 | 87FBE6521D2BCC0700FB5E05 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 87FBE6511D2BCC0700FB5E05 /* main.c */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXCopyFilesBuildPhase section */ 22 | 87FBE64C1D2BCC0700FB5E05 /* CopyFiles */ = { 23 | isa = PBXCopyFilesBuildPhase; 24 | buildActionMask = 2147483647; 25 | dstPath = /usr/share/man/man1/; 26 | dstSubfolderSpec = 0; 27 | files = ( 28 | ); 29 | runOnlyForDeploymentPostprocessing = 1; 30 | }; 31 | /* End PBXCopyFilesBuildPhase section */ 32 | 33 | /* Begin PBXFileReference section */ 34 | 873FC11E1E22D7B40089AE60 /* common.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = common.c; sourceTree = ""; }; 35 | 873FC11F1E22D7B40089AE60 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = ""; }; 36 | 873FC1201E22D7B40089AE60 /* dfu.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dfu.c; sourceTree = ""; }; 37 | 873FC1211E22D7B40089AE60 /* dfu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dfu.h; sourceTree = ""; }; 38 | 873FC1221E22D7B40089AE60 /* endianness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = endianness.h; sourceTree = ""; }; 39 | 873FC1231E22D7B40089AE60 /* idevicerestore.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = idevicerestore.c; sourceTree = ""; }; 40 | 873FC1241E22D7B40089AE60 /* idevicerestore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = idevicerestore.h; sourceTree = ""; }; 41 | 873FC1251E22D7B40089AE60 /* normal.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = normal.c; sourceTree = ""; }; 42 | 873FC1261E22D7B40089AE60 /* normal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = normal.h; sourceTree = ""; }; 43 | 873FC1271E22D7B40089AE60 /* recovery.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = recovery.c; sourceTree = ""; }; 44 | 873FC1281E22D7B40089AE60 /* recovery.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = recovery.h; sourceTree = ""; }; 45 | 873FC12F1E22D7E40089AE60 /* libimobiledevice.6.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libimobiledevice.6.dylib; path = ../../../../usr/local/lib/libimobiledevice.6.dylib; sourceTree = ""; }; 46 | 873FC1311E22D7F60089AE60 /* libirecovery.2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libirecovery.2.dylib; path = ../../../../usr/local/lib/libirecovery.2.dylib; sourceTree = ""; }; 47 | 873FC1331E22D80A0089AE60 /* libplist.3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libplist.3.dylib; path = ../../../../usr/local/lib/libplist.3.dylib; sourceTree = ""; }; 48 | 873FC1351E22D81A0089AE60 /* all_igetnonce.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = all_igetnonce.h; sourceTree = ""; }; 49 | 87FBE64E1D2BCC0700FB5E05 /* igetnonce */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = igetnonce; sourceTree = BUILT_PRODUCTS_DIR; }; 50 | 87FBE6511D2BCC0700FB5E05 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = ""; }; 51 | /* End PBXFileReference section */ 52 | 53 | /* Begin PBXFrameworksBuildPhase section */ 54 | 87FBE64B1D2BCC0700FB5E05 /* Frameworks */ = { 55 | isa = PBXFrameworksBuildPhase; 56 | buildActionMask = 2147483647; 57 | files = ( 58 | 873FC1341E22D80A0089AE60 /* libplist.3.dylib in Frameworks */, 59 | 873FC1321E22D7F60089AE60 /* libirecovery.2.dylib in Frameworks */, 60 | 873FC1301E22D7E40089AE60 /* libimobiledevice.6.dylib in Frameworks */, 61 | ); 62 | runOnlyForDeploymentPostprocessing = 0; 63 | }; 64 | /* End PBXFrameworksBuildPhase section */ 65 | 66 | /* Begin PBXGroup section */ 67 | 873FC12E1E22D7E40089AE60 /* Frameworks */ = { 68 | isa = PBXGroup; 69 | children = ( 70 | 873FC1331E22D80A0089AE60 /* libplist.3.dylib */, 71 | 873FC1311E22D7F60089AE60 /* libirecovery.2.dylib */, 72 | 873FC12F1E22D7E40089AE60 /* libimobiledevice.6.dylib */, 73 | ); 74 | name = Frameworks; 75 | sourceTree = ""; 76 | }; 77 | 87FBE6451D2BCC0700FB5E05 = { 78 | isa = PBXGroup; 79 | children = ( 80 | 87FBE6501D2BCC0700FB5E05 /* igetnonce */, 81 | 87FBE64F1D2BCC0700FB5E05 /* Products */, 82 | 873FC12E1E22D7E40089AE60 /* Frameworks */, 83 | ); 84 | sourceTree = ""; 85 | }; 86 | 87FBE64F1D2BCC0700FB5E05 /* Products */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 87FBE64E1D2BCC0700FB5E05 /* igetnonce */, 90 | ); 91 | name = Products; 92 | sourceTree = ""; 93 | }; 94 | 87FBE6501D2BCC0700FB5E05 /* igetnonce */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 873FC1351E22D81A0089AE60 /* all_igetnonce.h */, 98 | 873FC11E1E22D7B40089AE60 /* common.c */, 99 | 873FC11F1E22D7B40089AE60 /* common.h */, 100 | 873FC1201E22D7B40089AE60 /* dfu.c */, 101 | 873FC1211E22D7B40089AE60 /* dfu.h */, 102 | 873FC1221E22D7B40089AE60 /* endianness.h */, 103 | 873FC1231E22D7B40089AE60 /* idevicerestore.c */, 104 | 873FC1241E22D7B40089AE60 /* idevicerestore.h */, 105 | 873FC1251E22D7B40089AE60 /* normal.c */, 106 | 873FC1261E22D7B40089AE60 /* normal.h */, 107 | 873FC1271E22D7B40089AE60 /* recovery.c */, 108 | 873FC1281E22D7B40089AE60 /* recovery.h */, 109 | 87FBE6511D2BCC0700FB5E05 /* main.c */, 110 | ); 111 | path = igetnonce; 112 | sourceTree = ""; 113 | }; 114 | /* End PBXGroup section */ 115 | 116 | /* Begin PBXNativeTarget section */ 117 | 87FBE64D1D2BCC0700FB5E05 /* igetnonce */ = { 118 | isa = PBXNativeTarget; 119 | buildConfigurationList = 87FBE6551D2BCC0700FB5E05 /* Build configuration list for PBXNativeTarget "igetnonce" */; 120 | buildPhases = ( 121 | 87FBE64A1D2BCC0700FB5E05 /* Sources */, 122 | 87FBE64B1D2BCC0700FB5E05 /* Frameworks */, 123 | 87FBE64C1D2BCC0700FB5E05 /* CopyFiles */, 124 | ); 125 | buildRules = ( 126 | ); 127 | dependencies = ( 128 | ); 129 | name = igetnonce; 130 | productName = igetnonce; 131 | productReference = 87FBE64E1D2BCC0700FB5E05 /* igetnonce */; 132 | productType = "com.apple.product-type.tool"; 133 | }; 134 | /* End PBXNativeTarget section */ 135 | 136 | /* Begin PBXProject section */ 137 | 87FBE6461D2BCC0700FB5E05 /* Project object */ = { 138 | isa = PBXProject; 139 | attributes = { 140 | LastUpgradeCheck = 0720; 141 | ORGANIZATIONNAME = tihmstar; 142 | TargetAttributes = { 143 | 87FBE64D1D2BCC0700FB5E05 = { 144 | CreatedOnToolsVersion = 7.2; 145 | }; 146 | }; 147 | }; 148 | buildConfigurationList = 87FBE6491D2BCC0700FB5E05 /* Build configuration list for PBXProject "igetnonce" */; 149 | compatibilityVersion = "Xcode 3.2"; 150 | developmentRegion = English; 151 | hasScannedForEncodings = 0; 152 | knownRegions = ( 153 | en, 154 | ); 155 | mainGroup = 87FBE6451D2BCC0700FB5E05; 156 | productRefGroup = 87FBE64F1D2BCC0700FB5E05 /* Products */; 157 | projectDirPath = ""; 158 | projectRoot = ""; 159 | targets = ( 160 | 87FBE64D1D2BCC0700FB5E05 /* igetnonce */, 161 | ); 162 | }; 163 | /* End PBXProject section */ 164 | 165 | /* Begin PBXSourcesBuildPhase section */ 166 | 87FBE64A1D2BCC0700FB5E05 /* Sources */ = { 167 | isa = PBXSourcesBuildPhase; 168 | buildActionMask = 2147483647; 169 | files = ( 170 | 873FC12D1E22D7B40089AE60 /* recovery.c in Sources */, 171 | 873FC12A1E22D7B40089AE60 /* dfu.c in Sources */, 172 | 873FC12C1E22D7B40089AE60 /* normal.c in Sources */, 173 | 873FC1291E22D7B40089AE60 /* common.c in Sources */, 174 | 873FC12B1E22D7B40089AE60 /* idevicerestore.c in Sources */, 175 | 87FBE6521D2BCC0700FB5E05 /* main.c in Sources */, 176 | ); 177 | runOnlyForDeploymentPostprocessing = 0; 178 | }; 179 | /* End PBXSourcesBuildPhase section */ 180 | 181 | /* Begin XCBuildConfiguration section */ 182 | 87FBE6531D2BCC0700FB5E05 /* Debug */ = { 183 | isa = XCBuildConfiguration; 184 | buildSettings = { 185 | ALWAYS_SEARCH_USER_PATHS = NO; 186 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 187 | CLANG_CXX_LIBRARY = "libc++"; 188 | CLANG_ENABLE_MODULES = YES; 189 | CLANG_ENABLE_OBJC_ARC = YES; 190 | CLANG_WARN_BOOL_CONVERSION = YES; 191 | CLANG_WARN_CONSTANT_CONVERSION = YES; 192 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 193 | CLANG_WARN_EMPTY_BODY = YES; 194 | CLANG_WARN_ENUM_CONVERSION = YES; 195 | CLANG_WARN_INT_CONVERSION = YES; 196 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 197 | CLANG_WARN_UNREACHABLE_CODE = YES; 198 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 199 | CODE_SIGN_IDENTITY = "-"; 200 | COPY_PHASE_STRIP = NO; 201 | DEBUG_INFORMATION_FORMAT = dwarf; 202 | ENABLE_STRICT_OBJC_MSGSEND = YES; 203 | ENABLE_TESTABILITY = YES; 204 | GCC_C_LANGUAGE_STANDARD = gnu99; 205 | GCC_DYNAMIC_NO_PIC = NO; 206 | GCC_NO_COMMON_BLOCKS = YES; 207 | GCC_OPTIMIZATION_LEVEL = 0; 208 | GCC_PREPROCESSOR_DEFINITIONS = ( 209 | "DEBUG=1", 210 | "$(inherited)", 211 | ); 212 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 213 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 214 | GCC_WARN_UNDECLARED_SELECTOR = YES; 215 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 216 | GCC_WARN_UNUSED_FUNCTION = YES; 217 | GCC_WARN_UNUSED_VARIABLE = YES; 218 | MACOSX_DEPLOYMENT_TARGET = 10.11; 219 | MTL_ENABLE_DEBUG_INFO = YES; 220 | ONLY_ACTIVE_ARCH = YES; 221 | SDKROOT = macosx; 222 | }; 223 | name = Debug; 224 | }; 225 | 87FBE6541D2BCC0700FB5E05 /* Release */ = { 226 | isa = XCBuildConfiguration; 227 | buildSettings = { 228 | ALWAYS_SEARCH_USER_PATHS = NO; 229 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 230 | CLANG_CXX_LIBRARY = "libc++"; 231 | CLANG_ENABLE_MODULES = YES; 232 | CLANG_ENABLE_OBJC_ARC = YES; 233 | CLANG_WARN_BOOL_CONVERSION = YES; 234 | CLANG_WARN_CONSTANT_CONVERSION = YES; 235 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 236 | CLANG_WARN_EMPTY_BODY = YES; 237 | CLANG_WARN_ENUM_CONVERSION = YES; 238 | CLANG_WARN_INT_CONVERSION = YES; 239 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 240 | CLANG_WARN_UNREACHABLE_CODE = YES; 241 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 242 | CODE_SIGN_IDENTITY = "-"; 243 | COPY_PHASE_STRIP = NO; 244 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 245 | ENABLE_NS_ASSERTIONS = NO; 246 | ENABLE_STRICT_OBJC_MSGSEND = YES; 247 | GCC_C_LANGUAGE_STANDARD = gnu99; 248 | GCC_NO_COMMON_BLOCKS = YES; 249 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 250 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 251 | GCC_WARN_UNDECLARED_SELECTOR = YES; 252 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 253 | GCC_WARN_UNUSED_FUNCTION = YES; 254 | GCC_WARN_UNUSED_VARIABLE = YES; 255 | MACOSX_DEPLOYMENT_TARGET = 10.11; 256 | MTL_ENABLE_DEBUG_INFO = NO; 257 | SDKROOT = macosx; 258 | }; 259 | name = Release; 260 | }; 261 | 87FBE6561D2BCC0700FB5E05 /* Debug */ = { 262 | isa = XCBuildConfiguration; 263 | buildSettings = { 264 | ALWAYS_SEARCH_USER_PATHS = YES; 265 | HEADER_SEARCH_PATHS = /usr/local/include; 266 | LIBRARY_SEARCH_PATHS = /usr/local/lib; 267 | PRODUCT_NAME = "$(TARGET_NAME)"; 268 | }; 269 | name = Debug; 270 | }; 271 | 87FBE6571D2BCC0700FB5E05 /* Release */ = { 272 | isa = XCBuildConfiguration; 273 | buildSettings = { 274 | ALWAYS_SEARCH_USER_PATHS = YES; 275 | HEADER_SEARCH_PATHS = /usr/local/include; 276 | LIBRARY_SEARCH_PATHS = /usr/local/lib; 277 | PRODUCT_NAME = "$(TARGET_NAME)"; 278 | }; 279 | name = Release; 280 | }; 281 | /* End XCBuildConfiguration section */ 282 | 283 | /* Begin XCConfigurationList section */ 284 | 87FBE6491D2BCC0700FB5E05 /* Build configuration list for PBXProject "igetnonce" */ = { 285 | isa = XCConfigurationList; 286 | buildConfigurations = ( 287 | 87FBE6531D2BCC0700FB5E05 /* Debug */, 288 | 87FBE6541D2BCC0700FB5E05 /* Release */, 289 | ); 290 | defaultConfigurationIsVisible = 0; 291 | defaultConfigurationName = Release; 292 | }; 293 | 87FBE6551D2BCC0700FB5E05 /* Build configuration list for PBXNativeTarget "igetnonce" */ = { 294 | isa = XCConfigurationList; 295 | buildConfigurations = ( 296 | 87FBE6561D2BCC0700FB5E05 /* Debug */, 297 | 87FBE6571D2BCC0700FB5E05 /* Release */, 298 | ); 299 | defaultConfigurationIsVisible = 0; 300 | defaultConfigurationName = Release; 301 | }; 302 | /* End XCConfigurationList section */ 303 | }; 304 | rootObject = 87FBE6461D2BCC0700FB5E05 /* Project object */; 305 | } 306 | -------------------------------------------------------------------------------- /igetnonce.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /igetnonce/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CFLAGS = $(libirecovery_CFLAGS) $(libimobiledevice_CFLAGS) $(libusbmuxd_CFLAGS) $(openssl_CFLAGS) 2 | AM_LDFLAGS = $(libirecovery_LIBS) $(libimobiledevice_LIBS) $(libusbmuxd_LIBS) $(openssl_LIBS) 3 | 4 | bin_PROGRAMS = igetnonce 5 | igetnonce_CFLAGS = $(AM_CFLAGS) 6 | igetnonce_LDADD = $(AM_LDFLAGS) 7 | igetnonce_SOURCES = common.c dfu.c idevicerestore.c normal.c recovery.c main.c 8 | 9 | -------------------------------------------------------------------------------- /igetnonce/all_igetnonce.h: -------------------------------------------------------------------------------- 1 | // 2 | // all_igetnonce.h 3 | // igetnonce 4 | // 5 | // Created by tihmstar on 08.01.17. 6 | // Copyright © 2017 tihmstar. All rights reserved. 7 | // 8 | 9 | #ifndef all_igetnonce_h 10 | #define all_igetnonce_h 11 | 12 | 13 | #define VERSION_COMMIT_COUNT_IGETNONCE "undefined version" 14 | #define VERSION_COMMIT_SHA_IGETNONCE "undefined commit" 15 | 16 | 17 | #endif /* all_igetnonce_h */ 18 | -------------------------------------------------------------------------------- /igetnonce/common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * common.c 3 | * Misc functions used in idevicerestore 4 | * 5 | * Copyright (c) 2012 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "common.h" 32 | 33 | #define MAX_PRINT_LEN 64*1024 34 | 35 | struct idevicerestore_mode_t idevicerestore_modes[] = { 36 | { 0, "WTF" }, 37 | { 1, "DFU" }, 38 | { 2, "Recovery" }, 39 | { 3, "Restore" }, 40 | { 4, "Normal" }, 41 | { -1, NULL } 42 | }; 43 | 44 | int idevicerestore_debug = 0; 45 | 46 | #define idevicerestore_err_buff_size 256 47 | static char idevicerestore_err_buff[idevicerestore_err_buff_size] = {0, }; 48 | 49 | static FILE* info_stream = NULL; 50 | static FILE* error_stream = NULL; 51 | static FILE* debug_stream = NULL; 52 | 53 | static int info_disabled = 0; 54 | static int error_disabled = 0; 55 | static int debug_disabled = 0; 56 | 57 | void info(const char* format, ...) 58 | { 59 | if (info_disabled) return; 60 | va_list vargs; 61 | va_start(vargs, format); 62 | vfprintf((info_stream) ? info_stream : stdout, format, vargs); 63 | va_end(vargs); 64 | } 65 | 66 | void error(const char* format, ...) 67 | { 68 | va_list vargs, vargs2; 69 | va_start(vargs, format); 70 | va_copy(vargs2, vargs); 71 | vsnprintf(idevicerestore_err_buff, idevicerestore_err_buff_size, format, vargs); 72 | va_end(vargs); 73 | if (!error_disabled) { 74 | vfprintf((error_stream) ? error_stream : stderr, format, vargs2); 75 | } 76 | va_end(vargs2); 77 | } 78 | 79 | void debug(const char* format, ...) 80 | { 81 | if (debug_disabled) return; 82 | if (!idevicerestore_debug) { 83 | return; 84 | } 85 | va_list vargs; 86 | va_start(vargs, format); 87 | vfprintf((debug_stream) ? debug_stream : stderr, format, vargs); 88 | va_end(vargs); 89 | } 90 | 91 | void idevicerestore_set_info_stream(FILE* strm) 92 | { 93 | if (strm) { 94 | info_disabled = 0; 95 | info_stream = strm; 96 | } else { 97 | info_disabled = 1; 98 | } 99 | } 100 | 101 | void idevicerestore_set_error_stream(FILE* strm) 102 | { 103 | if (strm) { 104 | error_disabled = 0; 105 | error_stream = strm; 106 | } else { 107 | error_disabled = 1; 108 | } 109 | } 110 | 111 | void idevicerestore_set_debug_stream(FILE* strm) 112 | { 113 | if (strm) { 114 | debug_disabled = 0; 115 | debug_stream = strm; 116 | } else { 117 | debug_disabled = 1; 118 | } 119 | } 120 | 121 | const char* idevicerestore_get_error(void) 122 | { 123 | if (idevicerestore_err_buff[0] == 0) { 124 | return NULL; 125 | } else { 126 | char* p = NULL; 127 | while ((strlen(idevicerestore_err_buff) > 0) && (p = strrchr(idevicerestore_err_buff, '\n'))) { 128 | p[0] = '\0'; 129 | } 130 | return (const char*)idevicerestore_err_buff; 131 | } 132 | } 133 | 134 | int write_file(const char* filename, const void* data, size_t size) { 135 | size_t bytes = 0; 136 | FILE* file = NULL; 137 | 138 | debug("Writing data to %s\n", filename); 139 | file = fopen(filename, "wb"); 140 | if (file == NULL) { 141 | error("write_file: Unable to open file %s\n", filename); 142 | return -1; 143 | } 144 | 145 | bytes = fwrite(data, 1, size, file); 146 | fclose(file); 147 | 148 | if (bytes != size) { 149 | error("ERROR: Unable to write entire file: %s: %d of %d\n", filename, (int)bytes, (int)size); 150 | return -1; 151 | } 152 | 153 | return (int)size; 154 | } 155 | 156 | int read_file(const char* filename, void** data, size_t* size) { 157 | size_t bytes = 0; 158 | size_t length = 0; 159 | FILE* file = NULL; 160 | char* buffer = NULL; 161 | debug("Reading data from %s\n", filename); 162 | 163 | *size = 0; 164 | *data = NULL; 165 | 166 | file = fopen(filename, "rb"); 167 | if (file == NULL) { 168 | error("read_file: File %s not found\n", filename); 169 | return -1; 170 | } 171 | 172 | fseeko(file, 0, SEEK_END); 173 | length = ftello(file); 174 | rewind(file); 175 | 176 | buffer = (char*) malloc(length); 177 | if (buffer == NULL) { 178 | error("ERROR: Out of memory\n"); 179 | fclose(file); 180 | return -1; 181 | } 182 | bytes = fread(buffer, 1, length, file); 183 | fclose(file); 184 | 185 | if (bytes != length) { 186 | error("ERROR: Unable to read entire file\n"); 187 | free(buffer); 188 | return -1; 189 | } 190 | 191 | *size = length; 192 | *data = buffer; 193 | return 0; 194 | } 195 | 196 | void debug_plist(plist_t plist) { 197 | uint32_t size = 0; 198 | char* data = NULL; 199 | plist_to_xml(plist, &data, &size); 200 | if (size <= MAX_PRINT_LEN) 201 | info("%s:printing %i bytes plist:\n%s", __FILE__, size, data); 202 | else 203 | info("%s:supressed printing %i bytes plist...\n", __FILE__, size); 204 | free(data); 205 | } 206 | 207 | void print_progress_bar(double progress) { 208 | #ifndef WIN32 209 | if (info_disabled) return; 210 | int i = 0; 211 | if(progress < 0) return; 212 | if(progress > 100) progress = 100; 213 | info("\r["); 214 | for(i = 0; i < 50; i++) { 215 | if(i < progress / 2) info("="); 216 | else info(" "); 217 | } 218 | info("] %5.1f%%", progress); 219 | if(progress == 100) info("\n"); 220 | fflush((info_stream) ? info_stream : stdout); 221 | #endif 222 | } 223 | 224 | #define GET_RAND(min, max) ((rand() % (max - min)) + min) 225 | 226 | char *generate_guid(void) 227 | { 228 | char *guid = (char *) malloc(sizeof(char) * 37); 229 | const char *chars = "ABCDEF0123456789"; 230 | srand((unsigned int)time(NULL)); 231 | int i = 0; 232 | 233 | for (i = 0; i < 36; i++) { 234 | if (i == 8 || i == 13 || i == 18 || i == 23) { 235 | guid[i] = '-'; 236 | continue; 237 | } else { 238 | guid[i] = chars[GET_RAND(0, 16)]; 239 | } 240 | } 241 | guid[36] = '\0'; 242 | return guid; 243 | } 244 | 245 | -------------------------------------------------------------------------------- /igetnonce/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * common.h 3 | * Misc functions used in idevicerestore 4 | * 5 | * Copyright (c) 2012 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifndef IDEVICERESTORE_COMMON_H 25 | #define IDEVICERESTORE_COMMON_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | #include 33 | 34 | #include "idevicerestore.h" 35 | 36 | #define MODE_UNKNOWN -1 37 | #define MODE_WTF 0 38 | #define MODE_DFU 1 39 | #define MODE_RECOVERY 2 40 | #define MODE_RESTORE 3 41 | #define MODE_NORMAL 4 42 | 43 | #define FLAG_QUIT 1 44 | 45 | #define CPFM_FLAG_SECURITY_MODE 1 << 0 46 | #define CPFM_FLAG_PRODUCTION_MODE 1 << 1 47 | 48 | #define IBOOT_FLAG_IMAGE4_AWARE 1 << 2 49 | #define IBOOT_FLAG_EFFECTIVE_SECURITY_MODE 1 << 3 50 | #define IBOOT_FLAG_EFFECTIVE_PRODUCTION_MODE 1 << 4 51 | 52 | struct dfu_client_t; 53 | struct normal_client_t; 54 | struct restore_client_t; 55 | struct recovery_client_t; 56 | 57 | struct idevicerestore_mode_t { 58 | int index; 59 | const char* string; 60 | }; 61 | 62 | struct idevicerestore_entry_t { 63 | char* name; 64 | char* path; 65 | char* filename; 66 | char* blob_data; 67 | uint32_t blob_size; 68 | struct idevicerestore_entry* next; 69 | struct idevicerestore_entry* prev; 70 | }; 71 | 72 | struct idevicerestore_client_t { 73 | int flags; 74 | char *otamanifest; 75 | plist_t tss; 76 | char* tss_url; 77 | plist_t version_data; 78 | uint64_t ecid; 79 | unsigned char* nonce; 80 | int nonce_size; 81 | int image4supported; 82 | plist_t preflight_info; 83 | char* udid; 84 | char* srnm; 85 | char* ipsw; 86 | const char* filesystem; 87 | struct dfu_client_t* dfu; 88 | struct normal_client_t* normal; 89 | struct restore_client_t* restore; 90 | struct recovery_client_t* recovery; 91 | irecv_device_t device; 92 | struct idevicerestore_entry_t** entries; 93 | struct idevicerestore_mode_t* mode; 94 | char* version; 95 | char* build; 96 | int build_major; 97 | char* restore_boot_args; 98 | char* cache_dir; 99 | idevicerestore_progress_cb_t progress_cb; 100 | void* progress_cb_data; 101 | }; 102 | 103 | extern struct idevicerestore_mode_t idevicerestore_modes[]; 104 | 105 | extern int idevicerestore_debug; 106 | 107 | void info(const char* format, ...); 108 | void error(const char* format, ...); 109 | void debug(const char* format, ...); 110 | char *generate_guid(void); 111 | 112 | void idevicerestore_progress(struct idevicerestore_client_t* client, int step, double progress); 113 | 114 | #ifdef __cplusplus 115 | } 116 | #endif 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /igetnonce/dfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dfu.c 3 | * Functions for handling idevices in DFU mode 4 | * 5 | * Copyright (c) 2010-2013 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012-2015 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "dfu.h" 31 | #include "recovery.h" 32 | #include "idevicerestore.h" 33 | #include "common.h" 34 | 35 | int dfu_progress_callback(irecv_client_t client, const irecv_event_t* event) { 36 | // if (event->type == IRECV_PROGRESS) { 37 | // print_progress_bar(event->progress); 38 | // } 39 | return 0; 40 | } 41 | 42 | int dfu_client_new(struct idevicerestore_client_t* client) { 43 | int i = 0; 44 | int attempts = 10; 45 | irecv_client_t dfu = NULL; 46 | irecv_error_t dfu_error = IRECV_E_UNKNOWN_ERROR; 47 | 48 | if (client->dfu == NULL) { 49 | client->dfu = (struct dfu_client_t*)malloc(sizeof(struct dfu_client_t)); 50 | memset(client->dfu, 0, sizeof(struct dfu_client_t)); 51 | if (client->dfu == NULL) { 52 | error("ERROR: Out of memory\n"); 53 | return -1; 54 | } 55 | } 56 | 57 | for (i = 1; i <= attempts; i++) { 58 | dfu_error = irecv_open_with_ecid(&dfu, client->ecid); 59 | if (dfu_error == IRECV_E_SUCCESS) { 60 | break; 61 | } 62 | 63 | if (i >= attempts) { 64 | error("ERROR: Unable to connect to device in DFU mode\n"); 65 | return -1; 66 | } 67 | 68 | sleep(1); 69 | debug("Retrying connection...\n"); 70 | } 71 | 72 | irecv_event_subscribe(dfu, IRECV_PROGRESS, &dfu_progress_callback, NULL); 73 | client->dfu->client = dfu; 74 | return 0; 75 | } 76 | 77 | void dfu_client_free(struct idevicerestore_client_t* client) { 78 | if(client != NULL) { 79 | if (client->dfu != NULL) { 80 | if(client->dfu->client != NULL) { 81 | irecv_close(client->dfu->client); 82 | client->dfu->client = NULL; 83 | } 84 | free(client->dfu); 85 | } 86 | client->dfu = NULL; 87 | } 88 | } 89 | 90 | int dfu_check_mode(struct idevicerestore_client_t* client, int* mode) { 91 | irecv_client_t dfu = NULL; 92 | irecv_error_t dfu_error = IRECV_E_SUCCESS; 93 | int probe_mode = -1; 94 | 95 | irecv_init(); 96 | dfu_error = irecv_open_with_ecid(&dfu, client->ecid); 97 | if (dfu_error != IRECV_E_SUCCESS) { 98 | return -1; 99 | } 100 | 101 | irecv_get_mode(dfu, &probe_mode); 102 | 103 | if ((probe_mode != IRECV_K_DFU_MODE) && (probe_mode != IRECV_K_WTF_MODE)) { 104 | irecv_close(dfu); 105 | return -1; 106 | } 107 | 108 | *mode = (probe_mode == IRECV_K_WTF_MODE) ? MODE_WTF : MODE_DFU; 109 | 110 | irecv_close(dfu); 111 | 112 | return 0; 113 | } 114 | 115 | const char* dfu_check_hardware_model(struct idevicerestore_client_t* client) { 116 | irecv_client_t dfu = NULL; 117 | irecv_error_t dfu_error = IRECV_E_SUCCESS; 118 | irecv_device_t device = NULL; 119 | 120 | irecv_init(); 121 | dfu_error = irecv_open_with_ecid(&dfu, client->ecid); 122 | if (dfu_error != IRECV_E_SUCCESS) { 123 | return NULL; 124 | } 125 | 126 | dfu_error = irecv_devices_get_device_by_client(dfu, &device); 127 | if (dfu_error != IRECV_E_SUCCESS) { 128 | return NULL; 129 | } 130 | 131 | irecv_close(dfu); 132 | 133 | return device->hardware_model; 134 | } 135 | 136 | int dfu_send_buffer(struct idevicerestore_client_t* client, unsigned char* buffer, unsigned int size) 137 | { 138 | irecv_error_t err = 0; 139 | 140 | info("Sending data (%d bytes)...\n", size); 141 | 142 | err = irecv_send_buffer(client->dfu->client, buffer, size, 1); 143 | if (err != IRECV_E_SUCCESS) { 144 | error("ERROR: Unable to send data: %s\n", irecv_strerror(err)); 145 | return -1; 146 | } 147 | 148 | return 0; 149 | } 150 | 151 | 152 | 153 | int dfu_get_cpid(struct idevicerestore_client_t* client, unsigned int* cpid) { 154 | if(client->dfu == NULL) { 155 | if (dfu_client_new(client) < 0) { 156 | return -1; 157 | } 158 | } 159 | 160 | const struct irecv_device_info *device_info = irecv_get_device_info(client->dfu->client); 161 | if (!device_info) { 162 | return -1; 163 | } 164 | 165 | *cpid = device_info->cpid; 166 | 167 | return 0; 168 | } 169 | 170 | int dfu_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) { 171 | if(client->dfu == NULL) { 172 | if (dfu_client_new(client) < 0) { 173 | return -1; 174 | } 175 | } 176 | 177 | const struct irecv_device_info *device_info = irecv_get_device_info(client->dfu->client); 178 | if (!device_info) { 179 | return -1; 180 | } 181 | 182 | *ecid = device_info->ecid; 183 | 184 | return 0; 185 | } 186 | 187 | int dfu_is_image4_supported(struct idevicerestore_client_t* client) 188 | { 189 | if(client->dfu == NULL) { 190 | if (dfu_client_new(client) < 0) { 191 | return 0; 192 | } 193 | } 194 | 195 | const struct irecv_device_info *device_info = irecv_get_device_info(client->dfu->client); 196 | if (!device_info) { 197 | return 0; 198 | } 199 | 200 | return (device_info->ibfl & IBOOT_FLAG_IMAGE4_AWARE); 201 | } 202 | 203 | int dfu_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 204 | if(client->dfu == NULL) { 205 | if (dfu_client_new(client) < 0) { 206 | return -1; 207 | } 208 | } 209 | 210 | const struct irecv_device_info *device_info = irecv_get_device_info(client->dfu->client); 211 | if (!device_info) { 212 | return -1; 213 | } 214 | 215 | if (device_info->ap_nonce && device_info->ap_nonce_size > 0) { 216 | *nonce = (unsigned char*)malloc(device_info->ap_nonce_size); 217 | if (!*nonce) { 218 | return -1; 219 | } 220 | *nonce_size = device_info->ap_nonce_size; 221 | memcpy(*nonce, device_info->ap_nonce, *nonce_size); 222 | } 223 | 224 | return 0; 225 | } 226 | 227 | int dfu_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 228 | if(client->dfu == NULL) { 229 | if (dfu_client_new(client) < 0) { 230 | return -1; 231 | } 232 | } 233 | 234 | const struct irecv_device_info *device_info = irecv_get_device_info(client->dfu->client); 235 | if (!device_info) { 236 | return -1; 237 | } 238 | 239 | if (device_info->sep_nonce && device_info->sep_nonce_size > 0) { 240 | *nonce = (unsigned char*)malloc(device_info->sep_nonce_size); 241 | if (!*nonce) { 242 | return -1; 243 | } 244 | *nonce_size = device_info->sep_nonce_size; 245 | memcpy(*nonce, device_info->sep_nonce, *nonce_size); 246 | } 247 | 248 | return 0; 249 | } 250 | -------------------------------------------------------------------------------- /igetnonce/dfu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * dfu.h 3 | * Functions for handling idevices in DFU mode 4 | * 5 | * Copyright (c) 2010-2013 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012-2015 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifndef IDEVICERESTORE_DFU_H 25 | #define IDEVICERESTORE_DFU_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | #include "common.h" 33 | 34 | struct dfu_client_t { 35 | irecv_client_t client; 36 | const char* ipsw; 37 | plist_t tss; 38 | }; 39 | 40 | int dfu_client_new(struct idevicerestore_client_t* client); 41 | void dfu_client_free(struct idevicerestore_client_t* client); 42 | int dfu_check_mode(struct idevicerestore_client_t* client, int* mode); 43 | const char* dfu_check_hardware_model(struct idevicerestore_client_t* client); 44 | int dfu_get_cpid(struct idevicerestore_client_t* client, unsigned int* cpid); 45 | int dfu_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid); 46 | int dfu_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 47 | int dfu_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 48 | int dfu_enter_recovery(struct idevicerestore_client_t* client, plist_t build_identity); 49 | 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /igetnonce/endianness.h: -------------------------------------------------------------------------------- 1 | #ifndef ENDIANNESS_H 2 | #define ENDIANNESS_H 3 | 4 | #ifndef __LITTLE_ENDIAN 5 | #define __LITTLE_ENDIAN 1234 6 | #endif 7 | 8 | #ifndef __BIG_ENDIAN 9 | #define __BIG_ENDIAN 4321 10 | #endif 11 | 12 | #ifndef __BYTE_ORDER 13 | #ifdef __LITTLE_ENDIAN__ 14 | #define __BYTE_ORDER __LITTLE_ENDIAN 15 | #else 16 | #ifdef __BIG_ENDIAN__ 17 | #define __BYTE_ORDER __BIG_ENDIAN 18 | #endif 19 | #endif 20 | #endif 21 | 22 | #ifndef be16toh 23 | #if __BYTE_ORDER == __BIG_ENDIAN 24 | #define be16toh(x) (x) 25 | #else 26 | #define be16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) 27 | #endif 28 | #endif 29 | 30 | #ifndef le16toh 31 | #if __BYTE_ORDER == __BIG_ENDIAN 32 | #define le16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) 33 | #else 34 | #define le16toh(x) (x) 35 | #endif 36 | #endif 37 | 38 | 39 | #ifndef __bswap_32 40 | #define __bswap_32(x) ((((x) & 0xFF000000) >> 24) \ 41 | | (((x) & 0x00FF0000) >> 8) \ 42 | | (((x) & 0x0000FF00) << 8) \ 43 | | (((x) & 0x000000FF) << 24)) 44 | #endif 45 | 46 | #ifndef be32toh 47 | #if __BYTE_ORDER == __BIG_ENDIAN 48 | #define be32toh(x) (x) 49 | #else 50 | #define be32toh(x) __bswap_32(x) 51 | #endif 52 | #endif 53 | 54 | #ifndef htobe32 55 | #define htobe32 be32toh 56 | #endif 57 | 58 | #ifndef le32toh 59 | #if __BYTE_ORDER == __BIG_ENDIAN 60 | #define le32toh(x) __bswap_32(x) 61 | #else 62 | #define le32toh(x) (x) 63 | #endif 64 | #endif 65 | 66 | #ifndef htole32 67 | #define htole32 le32toh 68 | #endif 69 | 70 | #ifndef __bswap_64 71 | #define __bswap_64(x) ((((x) & 0xFF00000000000000ull) >> 56) \ 72 | | (((x) & 0x00FF000000000000ull) >> 40) \ 73 | | (((x) & 0x0000FF0000000000ull) >> 24) \ 74 | | (((x) & 0x000000FF00000000ull) >> 8) \ 75 | | (((x) & 0x00000000FF000000ull) << 8) \ 76 | | (((x) & 0x0000000000FF0000ull) << 24) \ 77 | | (((x) & 0x000000000000FF00ull) << 40) \ 78 | | (((x) & 0x00000000000000FFull) << 56)) 79 | #endif 80 | 81 | #ifndef htobe64 82 | #if __BYTE_ORDER == __BIG_ENDIAN 83 | #define htobe64(x) (x) 84 | #else 85 | #define htobe64(x) __bswap_64(x) 86 | #endif 87 | #endif 88 | 89 | #ifndef be64toh 90 | #define be64toh htobe64 91 | #endif 92 | 93 | #ifndef le64toh 94 | #if __BYTE_ORDER == __LITTLE_ENDIAN 95 | #define le64toh(x) (x) 96 | #else 97 | #define le64toh(x) __bswap_64(x) 98 | #endif 99 | #endif 100 | 101 | #ifndef htole64 102 | #define htole64 le64toh 103 | #endif 104 | 105 | #endif /* ENDIANNESS_H */ 106 | -------------------------------------------------------------------------------- /igetnonce/idevicerestore.c: -------------------------------------------------------------------------------- 1 | /* 2 | * idevicerestore.c 3 | * Restore device firmware and filesystem 4 | * 5 | * Copyright (c) 2010-2015 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012-2015 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifdef HAVE_CONFIG_H 25 | #include "config.h" 26 | #endif 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "dfu.h" 38 | #include "common.h" 39 | #include "normal.h" 40 | #include "recovery.h" 41 | #include "idevicerestore.h" 42 | 43 | struct idevicerestore_client_t* idevicerestore_client_new(void) 44 | { 45 | struct idevicerestore_client_t* client = (struct idevicerestore_client_t*) malloc(sizeof(struct idevicerestore_client_t)); 46 | if (client == NULL) { 47 | error("ERROR: Out of memory\n"); 48 | return NULL; 49 | } 50 | memset(client, '\0', sizeof(struct idevicerestore_client_t)); 51 | return client; 52 | } 53 | 54 | void idevicerestore_client_free(struct idevicerestore_client_t* client) 55 | { 56 | if (!client) { 57 | return; 58 | } 59 | 60 | if (client->tss_url) { 61 | free(client->tss_url); 62 | } 63 | if (client->version_data) { 64 | plist_free(client->version_data); 65 | } 66 | if (client->nonce) { 67 | free(client->nonce); 68 | } 69 | if (client->udid) { 70 | free(client->udid); 71 | } 72 | if (client->srnm) { 73 | free(client->srnm); 74 | } 75 | if (client->ipsw) { 76 | free(client->ipsw); 77 | } 78 | if (client->version) { 79 | free(client->version); 80 | } 81 | if (client->build) { 82 | free(client->build); 83 | } 84 | if (client->restore_boot_args) { 85 | free(client->restore_boot_args); 86 | } 87 | if (client->cache_dir) { 88 | free(client->cache_dir); 89 | } 90 | free(client); 91 | } 92 | 93 | 94 | int check_mode(struct idevicerestore_client_t* client) { 95 | int mode = MODE_UNKNOWN; 96 | int dfumode = MODE_UNKNOWN; 97 | 98 | if (recovery_check_mode(client) == 0) { 99 | mode = MODE_RECOVERY; 100 | } 101 | 102 | else if (dfu_check_mode(client, &dfumode) == 0) { 103 | mode = dfumode; 104 | } 105 | 106 | else if (normal_check_mode(client) == 0) { 107 | mode = MODE_NORMAL; 108 | } 109 | 110 | // else if (restore_check_mode(client) == 0) { 111 | // mode = MODE_RESTORE; 112 | // } 113 | 114 | if (mode == MODE_UNKNOWN) { 115 | client->mode = NULL; 116 | } else { 117 | client->mode = &idevicerestore_modes[mode]; 118 | } 119 | return mode; 120 | } 121 | 122 | const char* check_hardware_model(struct idevicerestore_client_t* client) { 123 | const char* hw_model = NULL; 124 | int mode = MODE_UNKNOWN; 125 | 126 | if (client->mode) { 127 | mode = client->mode->index; 128 | } 129 | 130 | switch (mode) { 131 | // case MODE_RESTORE: 132 | // hw_model = restore_check_hardware_model(client); 133 | // break; 134 | 135 | case MODE_NORMAL: 136 | hw_model = normal_check_hardware_model(client); 137 | break; 138 | 139 | case MODE_DFU: 140 | case MODE_RECOVERY: 141 | hw_model = dfu_check_hardware_model(client); 142 | break; 143 | default: 144 | break; 145 | } 146 | 147 | if (hw_model != NULL) { 148 | irecv_devices_get_device_by_hardware_model(hw_model, &client->device); 149 | } 150 | 151 | return hw_model; 152 | } 153 | 154 | int get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) { 155 | int mode = MODE_UNKNOWN; 156 | 157 | if (client->mode) { 158 | mode = client->mode->index; 159 | } 160 | 161 | switch (mode) { 162 | case MODE_NORMAL: 163 | if (normal_get_ecid(client, ecid) < 0) { 164 | *ecid = 0; 165 | return -1; 166 | } 167 | break; 168 | 169 | case MODE_DFU: 170 | if (dfu_get_ecid(client, ecid) < 0) { 171 | *ecid = 0; 172 | return -1; 173 | } 174 | break; 175 | 176 | case MODE_RECOVERY: 177 | if (recovery_get_ecid(client, ecid) < 0) { 178 | *ecid = 0; 179 | return -1; 180 | } 181 | break; 182 | 183 | default: 184 | error("ERROR: Device is in an invalid state\n"); 185 | *ecid = 0; 186 | return -1; 187 | } 188 | 189 | return 0; 190 | } 191 | 192 | int get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 193 | int mode = MODE_UNKNOWN; 194 | 195 | *nonce = NULL; 196 | *nonce_size = 0; 197 | if (client->mode) { 198 | mode = client->mode->index; 199 | } 200 | 201 | switch (mode) { 202 | case MODE_NORMAL: 203 | if (normal_get_ap_nonce(client, nonce, nonce_size) < 0) { 204 | info("failed\n"); 205 | return -1; 206 | } 207 | break; 208 | case MODE_DFU: 209 | if (dfu_get_ap_nonce(client, nonce, nonce_size) < 0) { 210 | info("failed\n"); 211 | return -1; 212 | } 213 | break; 214 | case MODE_RECOVERY: 215 | if (recovery_get_ap_nonce(client, nonce, nonce_size) < 0) { 216 | info("failed\n"); 217 | return -1; 218 | } 219 | break; 220 | 221 | default: 222 | info("failed\n"); 223 | error("ERROR: Device is in an invalid state\n"); 224 | return -1; 225 | } 226 | 227 | int i = 0; 228 | info("ApNonce="); 229 | for (i = 0; i < *nonce_size; i++) { 230 | info("%02x", (*nonce)[i]); 231 | } 232 | info("\n"); 233 | 234 | return 0; 235 | } 236 | 237 | int get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 238 | int mode = MODE_UNKNOWN; 239 | 240 | *nonce = NULL; 241 | *nonce_size = 0; 242 | 243 | if (client->mode) { 244 | mode = client->mode->index; 245 | } 246 | 247 | switch (mode) { 248 | case MODE_NORMAL: 249 | if (normal_get_sep_nonce(client, nonce, nonce_size) < 0) { 250 | info("failed\n"); 251 | return -1; 252 | } 253 | break; 254 | case MODE_DFU: 255 | if (dfu_get_sep_nonce(client, nonce, nonce_size) < 0) { 256 | info("failed\n"); 257 | return -1; 258 | } 259 | break; 260 | case MODE_RECOVERY: 261 | if (recovery_get_sep_nonce(client, nonce, nonce_size) < 0) { 262 | info("failed\n"); 263 | return -1; 264 | } 265 | break; 266 | 267 | default: 268 | info("failed\n"); 269 | error("ERROR: Device is in an invalid state\n"); 270 | return -1; 271 | } 272 | 273 | int i = 0; 274 | info("SepNonce="); 275 | for (i = 0; i < *nonce_size; i++) { 276 | info("%02x", (*nonce)[i]); 277 | } 278 | info("\n"); 279 | 280 | return 0; 281 | } 282 | -------------------------------------------------------------------------------- /igetnonce/idevicerestore.h: -------------------------------------------------------------------------------- 1 | /* 2 | * idevicerestore.h 3 | * Restore device firmware and filesystem 4 | * 5 | * Copyright (c) 2010-2012 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012-2015 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifndef IDEVICERESTORE_H 25 | #define IDEVICERESTORE_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | #include 33 | 34 | // the flag with value 1 is reserved for internal use only. don't use it. 35 | #define FLAG_DEBUG 1 << 1 36 | #define FLAG_ERASE 1 << 2 37 | #define FLAG_CUSTOM 1 << 3 38 | #define FLAG_EXCLUDE 1 << 4 39 | #define FLAG_PWN 1 << 5 40 | #define FLAG_NOACTION 1 << 6 41 | #define FLAG_SHSHONLY 1 << 7 42 | #define FLAG_LATEST 1 << 8 43 | #define FLAG_DOWNGRADE 1 << 9 44 | #define FLAG_OTAMANIFEST 1 << 10 45 | #define FLAG_BOOT 1 << 11 46 | #define FLAG_PANICLOG 1 << 12 47 | #define FLAG_NOBOOTX 1 << 13 48 | 49 | struct idevicerestore_client_t; 50 | 51 | enum { 52 | RESTORE_STEP_DETECT = 0, 53 | RESTORE_STEP_PREPARE, 54 | RESTORE_STEP_UPLOAD_FS, 55 | RESTORE_STEP_VERIFY_FS, 56 | RESTORE_STEP_FLASH_FW, 57 | RESTORE_STEP_FLASH_BB, 58 | RESTORE_NUM_STEPS 59 | }; 60 | 61 | typedef void (*idevicerestore_progress_cb_t)(int step, double step_progress, void* userdata); 62 | 63 | struct idevicerestore_client_t* idevicerestore_client_new(void); 64 | void idevicerestore_client_free(struct idevicerestore_client_t* client); 65 | 66 | int idevicerestore_start(struct idevicerestore_client_t* client); 67 | 68 | int check_mode(struct idevicerestore_client_t* client); 69 | const char* check_hardware_model(struct idevicerestore_client_t* client); 70 | int get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid); 71 | int get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 72 | int get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /igetnonce/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // igetnonce 4 | // 5 | // Created by tihmstar on 05.07.16. 6 | // Copyright © 2016 tihmstar. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include "idevicerestore.h" 12 | #include "common.h" 13 | #include "all_igetnonce.h" 14 | 15 | int64_t parseECID(const char *ecid){ 16 | const char *ecidBK = ecid; 17 | int isHex = 0; 18 | int64_t ret = 0; 19 | 20 | while (*ecid && !isHex) { 21 | char c = *(ecid++); 22 | if (c >= '0' && c<='9') { 23 | ret *=10; 24 | ret += c - '0'; 25 | }else{ 26 | isHex = 1; 27 | ret = 0; 28 | } 29 | } 30 | 31 | if (isHex) { 32 | while (*ecidBK) { 33 | char c = *(ecidBK++); 34 | ret *=16; 35 | if (c >= '0' && c<='9') { 36 | ret += c - '0'; 37 | }else if (c >= 'a' && c <= 'f'){ 38 | ret += 10 + c - 'a'; 39 | }else if (c >= 'A' && c <= 'F'){ 40 | ret += 10 + c - 'A'; 41 | }else{ 42 | return 0; //ERROR parsing failed 43 | } 44 | } 45 | } 46 | 47 | return ret; 48 | } 49 | 50 | int main(int argc, const char * argv[]) { 51 | printf("Version: " VERSION_COMMIT_SHA_IGETNONCE " - " VERSION_COMMIT_COUNT_IGETNONCE "\n"); 52 | struct idevicerestore_client_t* client = idevicerestore_client_new(); 53 | 54 | if (argc >= 3){ 55 | if (strncmp(argv[1],"-e",2) == 0){ 56 | if ((client->ecid = parseECID(argv[2])) == 0){ 57 | printf("Error: can't parse ecid \"%s\", continuing without ecid\n",argv[2]); 58 | }else{ 59 | printf("User specified ecid=%llx\n",client->ecid); 60 | } 61 | } 62 | } 63 | 64 | 65 | if (check_mode(client) < 0 || client->mode->index == MODE_UNKNOWN || 66 | ((client->flags & FLAG_DOWNGRADE) && client->mode->index != MODE_DFU && client->mode->index != MODE_RECOVERY)) { 67 | error("ERROR: Unable to discover device mode. Please make sure a device is attached.\n"); 68 | return -1; 69 | } 70 | 71 | if (check_hardware_model(client) == NULL || client->device == NULL) { 72 | error("ERROR: Unable to discover device model\n"); 73 | return -1; 74 | } 75 | 76 | info("Identified device as %s, %s ", client->device->hardware_model, client->device->product_type); 77 | 78 | 79 | switch (client->mode->index) { 80 | case MODE_NORMAL: 81 | info("in normal mode... "); 82 | break; 83 | case MODE_DFU: 84 | info("in dfu mode... "); 85 | break; 86 | case MODE_RECOVERY: 87 | info("in recovery mode... "); 88 | break; 89 | 90 | default: 91 | info("failed\n"); 92 | error("ERROR: Device is in an invalid state\n"); 93 | return -1; 94 | } 95 | info("\n"); 96 | 97 | if ((client->flags & FLAG_PWN) && (client->mode->index != MODE_DFU)) { 98 | error("ERROR: you need to put your device into DFU mode to pwn it.\n"); 99 | return -1; 100 | } 101 | 102 | if (!client->ecid && get_ecid(client, &client->ecid) < 0) { 103 | error("ERROR: Unable to find device ECID\n"); 104 | return -1; 105 | } 106 | info("ecid=%llx\n",client->ecid); 107 | 108 | unsigned char* nonce = NULL; 109 | int nonce_size = 0; 110 | 111 | if (get_ap_nonce(client, &nonce, &nonce_size) < 0) { 112 | error("NOTE: Unable to get nonce from device\n"); 113 | } 114 | if (get_sep_nonce(client, &nonce, &nonce_size) < 0) { 115 | error("NOTE: Unable to get nonce from device\n"); 116 | } 117 | 118 | return 0; 119 | } 120 | -------------------------------------------------------------------------------- /igetnonce/normal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * normal.h 3 | * Functions for handling idevices in normal mode 4 | * 5 | * Copyright (c) 2012-2015 Nikias Bassen. All Rights Reserved. 6 | * Copyright (c) 2012 Martin Szulecki. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "common.h" 33 | #include "normal.h" 34 | #include "recovery.h" 35 | 36 | static int normal_device_connected = 0; 37 | 38 | void normal_device_callback(const idevice_event_t* event, void* userdata) { 39 | struct idevicerestore_client_t* client = (struct idevicerestore_client_t*) userdata; 40 | if (event->event == IDEVICE_DEVICE_ADD) { 41 | normal_device_connected = 1; 42 | 43 | } else if (event->event == IDEVICE_DEVICE_REMOVE) { 44 | normal_device_connected = 0; 45 | client->flags &= FLAG_QUIT; 46 | } 47 | } 48 | 49 | int normal_client_new(struct idevicerestore_client_t* client) { 50 | struct normal_client_t* normal = (struct normal_client_t*) malloc(sizeof(struct normal_client_t)); 51 | if (normal == NULL) { 52 | error("ERROR: Out of memory\n"); 53 | return -1; 54 | } 55 | 56 | if (normal_open_with_timeout(client) < 0) { 57 | normal_client_free(client); 58 | return -1; 59 | } 60 | 61 | client->normal = normal; 62 | return 0; 63 | } 64 | 65 | void normal_client_free(struct idevicerestore_client_t* client) { 66 | struct normal_client_t* normal = NULL; 67 | if (client) { 68 | normal = client->normal; 69 | if(normal) { 70 | if(normal->client) { 71 | lockdownd_client_free(normal->client); 72 | normal->client = NULL; 73 | } 74 | if(normal->device) { 75 | idevice_free(normal->device); 76 | normal->device = NULL; 77 | } 78 | } 79 | free(normal); 80 | client->normal = NULL; 81 | } 82 | } 83 | 84 | static int normal_idevice_new(struct idevicerestore_client_t* client, idevice_t* device) 85 | { 86 | int num_devices = 0; 87 | char **devices = NULL; 88 | idevice_t dev = NULL; 89 | idevice_error_t device_error; 90 | 91 | *device = NULL; 92 | 93 | if (client->udid) { 94 | device_error = idevice_new(&dev, client->udid); 95 | if (device_error != IDEVICE_E_SUCCESS) { 96 | error("ERROR: %s: can't open device with UDID %s\n", __func__, client->udid); 97 | return -1; 98 | } 99 | *device = dev; 100 | return 0; 101 | } 102 | 103 | idevice_get_device_list(&devices, &num_devices); 104 | if (num_devices == 0) { 105 | return -1; 106 | } 107 | lockdownd_client_t lockdown = NULL; 108 | int j; 109 | for (j = 0; j < num_devices; j++) { 110 | if (lockdown != NULL) { 111 | lockdownd_client_free(lockdown); 112 | lockdown = NULL; 113 | } 114 | if (dev != NULL) { 115 | idevice_free(dev); 116 | dev = NULL; 117 | } 118 | device_error = idevice_new(&dev, devices[j]); 119 | if (device_error != IDEVICE_E_SUCCESS) { 120 | error("ERROR: %s: can't open device with UDID %s\n", __func__, devices[j]); 121 | continue; 122 | } 123 | 124 | if (lockdownd_client_new(dev, &lockdown, "idevicerestore") != LOCKDOWN_E_SUCCESS) { 125 | error("ERROR: %s: can't connect to lockdownd on device with UDID %s\n", __func__, devices[j]); 126 | continue; 127 | 128 | } 129 | char* type = NULL; 130 | if (lockdownd_query_type(lockdown, &type) != LOCKDOWN_E_SUCCESS) { 131 | continue; 132 | } 133 | if (strcmp(type, "com.apple.mobile.lockdown") != 0) { 134 | free(type); 135 | continue; 136 | } 137 | free(type); 138 | 139 | plist_t node = NULL; 140 | if ((lockdownd_get_value(lockdown, NULL, "UniqueChipID", &node) != LOCKDOWN_E_SUCCESS) || !node || (plist_get_node_type(node) != PLIST_UINT)){ 141 | if (node) { 142 | plist_free(node); 143 | } 144 | continue; 145 | } 146 | lockdownd_client_free(lockdown); 147 | lockdown = NULL; 148 | 149 | uint64_t this_ecid = 0; 150 | plist_get_uint_val(node, &this_ecid); 151 | plist_free(node); 152 | 153 | if (client->ecid != 0) { 154 | if (this_ecid != client->ecid) { 155 | continue; 156 | } 157 | } else { 158 | client->ecid = this_ecid; 159 | } 160 | client->udid = strdup(devices[j]); 161 | *device = dev; 162 | break; 163 | } 164 | idevice_device_list_free(devices); 165 | 166 | return 0; 167 | } 168 | 169 | int normal_check_mode(struct idevicerestore_client_t* client) { 170 | idevice_t device = NULL; 171 | 172 | normal_idevice_new(client, &device); 173 | if (!device) { 174 | return -1; 175 | } 176 | idevice_free(device); 177 | 178 | return 0; 179 | } 180 | 181 | int normal_open_with_timeout(struct idevicerestore_client_t* client) { 182 | int i = 0; 183 | int attempts = 10; 184 | idevice_t device = NULL; 185 | 186 | // no context exists so bail 187 | if(client == NULL) { 188 | return -1; 189 | } 190 | 191 | normal_device_connected = 0; 192 | 193 | // create our normal client if it doesn't yet exist 194 | if(client->normal == NULL) { 195 | client->normal = (struct normal_client_t*) malloc(sizeof(struct normal_client_t)); 196 | if(client->normal == NULL) { 197 | error("ERROR: Out of memory\n"); 198 | return -1; 199 | } 200 | } 201 | 202 | for (i = 1; i <= attempts; i++) { 203 | normal_idevice_new(client, &device); 204 | if (device) { 205 | normal_device_connected = 1; 206 | break; 207 | } 208 | 209 | if (i == attempts) { 210 | error("ERROR: Unable to connect to device in normal mode\n"); 211 | return -1; 212 | } 213 | sleep(2); 214 | } 215 | 216 | client->normal->device = device; 217 | 218 | return 0; 219 | } 220 | 221 | const char* normal_check_hardware_model(struct idevicerestore_client_t* client) { 222 | idevice_t device = NULL; 223 | char* product_type = NULL; 224 | irecv_device_t irecv_device = NULL; 225 | lockdownd_client_t lockdown = NULL; 226 | lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; 227 | 228 | normal_idevice_new(client, &device); 229 | if (!device) { 230 | return product_type; 231 | } 232 | 233 | lockdown_error = lockdownd_client_new_with_handshake(device, &lockdown, "idevicerestore"); 234 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 235 | lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); 236 | } 237 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 238 | idevice_free(device); 239 | return product_type; 240 | } 241 | 242 | plist_t pval = NULL; 243 | lockdownd_get_value(lockdown, NULL, "HardwareModel", &pval); 244 | if (pval && (plist_get_node_type(pval) == PLIST_STRING)) { 245 | char* strval = NULL; 246 | plist_get_string_val(pval, &strval); 247 | if (strval) { 248 | irecv_devices_get_device_by_hardware_model(strval, &irecv_device); 249 | free(strval); 250 | } 251 | } 252 | if (pval) { 253 | plist_free(pval); 254 | } 255 | 256 | return (irecv_device) ? irecv_device->hardware_model : NULL; 257 | } 258 | 259 | int normal_enter_recovery(struct idevicerestore_client_t* client) { 260 | idevice_t device = NULL; 261 | lockdownd_client_t lockdown = NULL; 262 | idevice_error_t device_error = IDEVICE_E_SUCCESS; 263 | lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; 264 | 265 | device_error = idevice_new(&device, client->udid); 266 | if (device_error != IDEVICE_E_SUCCESS) { 267 | error("ERROR: Unable to find device\n"); 268 | return -1; 269 | } 270 | 271 | lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); 272 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 273 | error("ERROR: Unable to connect to lockdownd service\n"); 274 | idevice_free(device); 275 | return -1; 276 | } 277 | 278 | lockdown_error = lockdownd_enter_recovery(lockdown); 279 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 280 | error("ERROR: Unable to place device in recovery mode\n"); 281 | lockdownd_client_free(lockdown); 282 | idevice_free(device); 283 | return -1; 284 | } 285 | 286 | lockdownd_client_free(lockdown); 287 | idevice_free(device); 288 | lockdown = NULL; 289 | device = NULL; 290 | 291 | if (recovery_client_new(client) < 0) { 292 | error("ERROR: Unable to enter recovery mode\n"); 293 | return -1; 294 | } 295 | 296 | client->mode = &idevicerestore_modes[MODE_RECOVERY]; 297 | return 0; 298 | } 299 | 300 | static int normal_get_nonce_by_key(struct idevicerestore_client_t* client, const char* key, unsigned char** nonce, int* nonce_size) { 301 | idevice_t device = NULL; 302 | plist_t nonce_node = NULL; 303 | lockdownd_client_t lockdown = NULL; 304 | idevice_error_t device_error = IDEVICE_E_SUCCESS; 305 | lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; 306 | 307 | device_error = idevice_new(&device, client->udid); 308 | if (device_error != IDEVICE_E_SUCCESS) { 309 | return -1; 310 | } 311 | 312 | lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); 313 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 314 | error("ERROR: Unable to connect to lockdownd\n"); 315 | idevice_free(device); 316 | return -1; 317 | } 318 | 319 | lockdown_error = lockdownd_get_value(lockdown, NULL, key, &nonce_node); 320 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 321 | error("Unable to get %s from lockdownd\n", key); 322 | lockdownd_client_free(lockdown); 323 | idevice_free(device); 324 | return -1; 325 | } 326 | 327 | if (!nonce_node || plist_get_node_type(nonce_node) != PLIST_DATA) { 328 | error("Unable to get %s\n", key); 329 | lockdownd_client_free(lockdown); 330 | idevice_free(device); 331 | return -1; 332 | } 333 | 334 | uint64_t n_size = 0; 335 | plist_get_data_val(nonce_node, (char**)nonce, &n_size); 336 | *nonce_size = (int)n_size; 337 | plist_free(nonce_node); 338 | 339 | lockdownd_client_free(lockdown); 340 | idevice_free(device); 341 | lockdown = NULL; 342 | device = NULL; 343 | 344 | return 0; 345 | } 346 | 347 | int normal_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 348 | return normal_get_nonce_by_key(client, "SEPNonce", nonce, nonce_size); 349 | } 350 | 351 | int normal_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 352 | return normal_get_nonce_by_key(client, "ApNonce", nonce, nonce_size); 353 | } 354 | 355 | int normal_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) { 356 | idevice_t device = NULL; 357 | plist_t unique_chip_node = NULL; 358 | lockdownd_client_t lockdown = NULL; 359 | idevice_error_t device_error = IDEVICE_E_SUCCESS; 360 | lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; 361 | 362 | device_error = idevice_new(&device, client->udid); 363 | if (device_error != IDEVICE_E_SUCCESS) { 364 | return -1; 365 | } 366 | 367 | lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); 368 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 369 | error("ERROR: Unable to connect to lockdownd\n"); 370 | idevice_free(device); 371 | return -1; 372 | } 373 | 374 | lockdown_error = lockdownd_get_value(lockdown, NULL, "UniqueChipID", &unique_chip_node); 375 | if (lockdown_error != LOCKDOWN_E_SUCCESS) { 376 | error("ERROR: Unable to get UniqueChipID from lockdownd\n"); 377 | lockdownd_client_free(lockdown); 378 | idevice_free(device); 379 | return -1; 380 | } 381 | 382 | if (!unique_chip_node || plist_get_node_type(unique_chip_node) != PLIST_UINT) { 383 | error("ERROR: Unable to get ECID\n"); 384 | lockdownd_client_free(lockdown); 385 | idevice_free(device); 386 | return -1; 387 | } 388 | plist_get_uint_val(unique_chip_node, ecid); 389 | plist_free(unique_chip_node); 390 | 391 | lockdownd_client_free(lockdown); 392 | idevice_free(device); 393 | lockdown = NULL; 394 | device = NULL; 395 | return 0; 396 | } 397 | -------------------------------------------------------------------------------- /igetnonce/normal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * normal.h 3 | * Functions for handling idevices in normal mode 4 | * 5 | * Copyright (c) 2012 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012-2015 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifndef IDEVICERESTORE_NORMAL_H 25 | #define IDEVICERESTORE_NORMAL_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | struct normal_client_t { 36 | idevice_t device; 37 | lockdownd_client_t client; 38 | const char* ipsw; 39 | plist_t tss; 40 | }; 41 | 42 | 43 | int normal_check_mode(struct idevicerestore_client_t* client); 44 | const char* normal_check_hardware_model(struct idevicerestore_client_t* client); 45 | int normal_client_new(struct idevicerestore_client_t* client); 46 | void normal_client_free(struct idevicerestore_client_t* client); 47 | int normal_open_with_timeout(struct idevicerestore_client_t* client); 48 | int normal_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid); 49 | int normal_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 50 | int normal_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /igetnonce/recovery.c: -------------------------------------------------------------------------------- 1 | /* 2 | * recovery.c 3 | * Functions for handling idevices in recovery mode 4 | * 5 | * Copyright (c) 2010-2012 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "idevicerestore.h" 33 | #include "recovery.h" 34 | 35 | int recovery_progress_callback(irecv_client_t client, const irecv_event_t* event) { 36 | if (event->type == IRECV_PROGRESS) { 37 | //print_progress_bar(event->progress); 38 | } 39 | return 0; 40 | } 41 | 42 | void recovery_client_free(struct idevicerestore_client_t* client) { 43 | if(client) { 44 | if (client->recovery) { 45 | if(client->recovery->client) { 46 | irecv_close(client->recovery->client); 47 | client->recovery->client = NULL; 48 | } 49 | free(client->recovery); 50 | client->recovery = NULL; 51 | } 52 | } 53 | } 54 | 55 | int recovery_client_new(struct idevicerestore_client_t* client) { 56 | int i = 0; 57 | int attempts = 20; 58 | irecv_client_t recovery = NULL; 59 | irecv_error_t recovery_error = IRECV_E_UNKNOWN_ERROR; 60 | 61 | if(client->recovery == NULL) { 62 | client->recovery = (struct recovery_client_t*)malloc(sizeof(struct recovery_client_t)); 63 | if (client->recovery == NULL) { 64 | error("ERROR: Out of memory\n"); 65 | return -1; 66 | } 67 | memset(client->recovery, 0, sizeof(struct recovery_client_t)); 68 | } 69 | 70 | for (i = 1; i <= attempts; i++) { 71 | recovery_error = irecv_open_with_ecid(&recovery, client->ecid); 72 | if (recovery_error == IRECV_E_SUCCESS) { 73 | break; 74 | } 75 | 76 | if (i >= attempts) { 77 | error("ERROR: Unable to connect to device in recovery mode\n"); 78 | return -1; 79 | } 80 | 81 | sleep(4); 82 | debug("Retrying connection...\n"); 83 | } 84 | 85 | if (client->srnm == NULL) { 86 | const struct irecv_device_info *device_info = irecv_get_device_info(recovery); 87 | if (device_info && device_info->srnm) { 88 | client->srnm = strdup(device_info->srnm); 89 | info("INFO: device serial number is %s\n", client->srnm); 90 | } 91 | } 92 | 93 | irecv_event_subscribe(recovery, IRECV_PROGRESS, &recovery_progress_callback, NULL); 94 | client->recovery->client = recovery; 95 | return 0; 96 | } 97 | 98 | int recovery_check_mode(struct idevicerestore_client_t* client) { 99 | irecv_client_t recovery = NULL; 100 | irecv_error_t recovery_error = IRECV_E_SUCCESS; 101 | int mode = 0; 102 | 103 | irecv_init(); 104 | recovery_error=irecv_open_with_ecid(&recovery, client->ecid); 105 | 106 | if (recovery_error != IRECV_E_SUCCESS) { 107 | return -1; 108 | } 109 | 110 | irecv_get_mode(recovery, &mode); 111 | 112 | if ((mode == IRECV_K_DFU_MODE) || (mode == IRECV_K_WTF_MODE)) { 113 | irecv_close(recovery); 114 | return -1; 115 | } 116 | 117 | irecv_close(recovery); 118 | recovery = NULL; 119 | 120 | return 0; 121 | } 122 | 123 | 124 | int recovery_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) { 125 | if(client->recovery == NULL) { 126 | if (recovery_client_new(client) < 0) { 127 | return -1; 128 | } 129 | } 130 | 131 | const struct irecv_device_info *device_info = irecv_get_device_info(client->recovery->client); 132 | if (!device_info) { 133 | return -1; 134 | } 135 | 136 | *ecid = device_info->ecid; 137 | 138 | return 0; 139 | } 140 | 141 | int recovery_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 142 | if(client->recovery == NULL) { 143 | if (recovery_client_new(client) < 0) { 144 | return -1; 145 | } 146 | } 147 | 148 | const struct irecv_device_info *device_info = irecv_get_device_info(client->recovery->client); 149 | if (!device_info) { 150 | return -1; 151 | } 152 | 153 | if (device_info->ap_nonce && device_info->ap_nonce_size > 0) { 154 | *nonce = (unsigned char*)malloc(device_info->ap_nonce_size); 155 | if (!*nonce) { 156 | return -1; 157 | } 158 | *nonce_size = device_info->ap_nonce_size; 159 | memcpy(*nonce, device_info->ap_nonce, *nonce_size); 160 | } 161 | 162 | return 0; 163 | } 164 | 165 | int recovery_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { 166 | if(client->recovery == NULL) { 167 | if (recovery_client_new(client) < 0) { 168 | return -1; 169 | } 170 | } 171 | 172 | const struct irecv_device_info *device_info = irecv_get_device_info(client->recovery->client); 173 | if (!device_info) { 174 | return -1; 175 | } 176 | 177 | if (device_info->sep_nonce && device_info->sep_nonce_size > 0) { 178 | *nonce = (unsigned char*)malloc(device_info->sep_nonce_size); 179 | if (!*nonce) { 180 | return -1; 181 | } 182 | *nonce_size = device_info->sep_nonce_size; 183 | memcpy(*nonce, device_info->sep_nonce, *nonce_size); 184 | } 185 | 186 | return 0; 187 | } 188 | 189 | int recovery_send_reset(struct idevicerestore_client_t* client) 190 | { 191 | irecv_send_command(client->recovery->client, "reset"); 192 | return 0; 193 | } 194 | 195 | -------------------------------------------------------------------------------- /igetnonce/recovery.h: -------------------------------------------------------------------------------- 1 | /* 2 | * recovery.h 3 | * Functions for handling idevices in recovery mode 4 | * 5 | * Copyright (c) 2010-2012 Martin Szulecki. All Rights Reserved. 6 | * Copyright (c) 2012 Nikias Bassen. All Rights Reserved. 7 | * Copyright (c) 2010 Joshua Hill. All Rights Reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifndef IDEVICERESTORE_RECOVERY_H 25 | #define IDEVICERESTORE_RECOVERY_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include "common.h" 36 | 37 | struct recovery_client_t { 38 | irecv_client_t client; 39 | const char* ipsw; 40 | plist_t tss; 41 | }; 42 | 43 | int recovery_check_mode(struct idevicerestore_client_t* client); 44 | int recovery_client_new(struct idevicerestore_client_t* client); 45 | void recovery_client_free(struct idevicerestore_client_t* client); 46 | int recovery_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid); 47 | int recovery_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 48 | int recovery_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); 49 | 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /setBuildVersion.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sed -i '.bak' "s/.*VERSION_COMMIT_COUNT_IGETNONCE.*/#define VERSION_COMMIT_COUNT_IGETNONCE \"$(git rev-list --count HEAD)\"/" ./igetnonce/all_igetnonce.h 2>/dev/null || sed -i "s/.*VERSION_COMMIT_COUNT_IGETNONCE.*/#define VERSION_COMMIT_COUNT_IGETNONCE \"$(git rev-list --count HEAD)\"/" ./igetnonce/all_igetnonce.h 2>/dev/null 3 | sed -i '.bak' "s/.*VERSION_COMMIT_SHA_IGETNONCE.*/#define VERSION_COMMIT_SHA_IGETNONCE \"$(git rev-parse HEAD)\"/" ./igetnonce/all_igetnonce.h 2>/dev/null || sed -i "s/.*VERSION_COMMIT_SHA_IGETNONCE.*/#define VERSION_COMMIT_SHA_IGETNONCE \"$(git rev-parse HEAD)\"/" ./igetnonce/all_igetnonce.h 2>/dev/null 4 | --------------------------------------------------------------------------------