Skip to content

Commit

Permalink
Merge pull request #25 from HenryQuan/main
Browse files Browse the repository at this point in the history
Potential fix for mmap fail with Invalid argument
  • Loading branch information
paradiseduo authored Oct 16, 2023
2 parents d985e66 + 358d50d commit 6534265
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.build
/.swiftpm
/*.xcodeproj
/appdecrypt
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ let package = Package(
.executable(name: "appdecrypt", targets: ["appdecrypt"])
],
targets: [
.target(name: "appdecrypt", dependencies: []),
.executableTarget(name: "appdecrypt", dependencies: []),
]
)
50 changes: 38 additions & 12 deletions Sources/appdecrypt/dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ class Dump {
targetUrl += "/Payload"
}
#endif
if !fileManager.fileExists(atPath: targetUrl) {
do{
try fileManager.copyItem(atPath: sourceUrl, toPath: targetUrl)
consoleIO.writeMessage("Success to copy file.")
}catch{
consoleIO.writeMessage("Failed to copy file.", to: .error)
do {
if fileManager.fileExists(atPath: targetUrl) {
// remove old files to ensure the integrity of the dump
try fileManager.removeItem(atPath: targetUrl)
consoleIO.writeMessage("Success to remove old files.")
}
try fileManager.copyItem(atPath: sourceUrl, toPath: targetUrl)
consoleIO.writeMessage("Success to copy file.")
} catch {
consoleIO.writeMessage("Failed to copy file.", to: .error)
}

var needDumpFilePaths = [String]()
Expand Down Expand Up @@ -102,6 +105,8 @@ class Dump {

for (i, sourcePath) in needDumpFilePaths.enumerated() {
let targetPath = dumpedFilePaths[i]
// Please see https://github.com/NyaMisty/fouldecrypt/issues/15#issuecomment-1722561492
let handle = dlopen(targetPath, RTLD_LAZY | RTLD_GLOBAL)
Dump.mapFile(path: sourcePath, mutable: false) { base_size, base_descriptor, base_error, base_raw in
if let base = base_raw {
Dump.mapFile(path: targetPath, mutable: true) { dupe_size, dupe_descriptor, dupe_error, dupe_raw in
Expand Down Expand Up @@ -151,22 +156,43 @@ class Dump {
consoleIO.writeMessage("Read \(sourcePath) Fail with \(base_error)", to: .error)
}
}
dlclose(handle)
}
}

static func dump(descriptor: Int32, dupe: UnsafeMutableRawPointer, info: encryption_info_command_64) -> (Bool, String) {
let base = mmap(nil, Int(info.cryptsize), PROT_READ | PROT_EXEC, MAP_PRIVATE, descriptor, off_t(info.cryptoff))
// https://github.com/Qcloud1223/COMP461905/issues/2#issuecomment-987510518
// Align the offset based on the page size
// See: https://man7.org/linux/man-pages/man2/mmap.2.html
let pageSize = Float(sysconf(_SC_PAGESIZE))
let multiplier = ceil(Float(info.cryptoff) / pageSize)
let alignedOffset = Int(multiplier * pageSize)

let cryptsize = Int(info.cryptsize)
let cryptoff = Int(info.cryptoff)

let cryptid = Int(info.cryptid)
// cryptid 0 doesn't need PROT_EXEC
let prot = PROT_READ | (cryptid == 0 ? 0 : PROT_EXEC)
var base = mmap(nil, cryptsize, prot, MAP_PRIVATE, descriptor, off_t(alignedOffset))
if base == MAP_FAILED {
return (false, "mmap fail with \(String(cString: strerror(errno)))")
}
let error = mremap_encrypted(base!, Int(info.cryptsize), info.cryptid, UInt32(CPU_TYPE_ARM64), UInt32(CPU_SUBTYPE_ARM64_ALL))
let error = mremap_encrypted(base!, cryptsize, info.cryptid, UInt32(CPU_TYPE_ARM64), UInt32(CPU_SUBTYPE_ARM64_ALL))
if error != 0 {
munmap(base, Int(info.cryptsize))
munmap(base, cryptsize)
return (false, "encrypted fail with \(String(cString: strerror(errno)))")
}
memcpy(dupe+UnsafeMutableRawPointer.Stride(info.cryptoff), base, Int(info.cryptsize))
munmap(base, Int(info.cryptsize))


// alignment needs to be adjusted, memmove will have bus error if not aligned
if alignedOffset - cryptoff > cryptsize {
posix_memalign(&base, cryptsize, cryptsize)
memmove(dupe+UnsafeMutableRawPointer.Stride(info.cryptoff), base, cryptsize)
free(base)
} else {
memmove(dupe+UnsafeMutableRawPointer.Stride(info.cryptoff), base, cryptsize)
munmap(base, cryptsize)
}
return (true, "")
}

Expand Down
14 changes: 10 additions & 4 deletions build-iOS.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -e

NAME=appdecrypt
SDK_VERSION=11.0

function build() {
START=$(date +%s)
Expand All @@ -12,14 +13,14 @@ function build() {
-Xswiftc "-sdk" \
-Xswiftc "$(xcrun --sdk iphoneos --show-sdk-path)" \
-Xswiftc "-target" \
-Xswiftc "arm64-apple-ios11.0" \
-Xswiftc "arm64-apple-ios$SDK_VERSION" \
-Xcc "-arch" \
-Xcc "arm64" \
-Xcc "--target=arm64-apple-ios11.0" \
-Xcc "--target=arm64-apple-ios$SDK_VERSION" \
-Xcc "-isysroot" \
-Xcc "$(xcrun --sdk iphoneos --show-sdk-path)" \
-Xcc "-mios-version-min=10.0" \
-Xcc "-miphoneos-version-min=11.0"
-Xcc "-mios-version-min=$SDK_VERSION" \
-Xcc "-miphoneos-version-min=$SDK_VERSION"

END=$(date +%s)
TIME=$(($END - $START))
Expand All @@ -35,3 +36,8 @@ main
mv .build/release/appdecrypt .
chmod +x appdecrypt
ldid -Sglobal.xml appdecrypt

# if ip is provided, send to the device in one go
if [ -n "$1" ]; then
scp appdecrypt mobile@$1:/var/mobile/Documents/appdecrypt
fi

0 comments on commit 6534265

Please sign in to comment.