Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Generic Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakr233 committed Jan 9, 2022
1 parent 24ff751 commit 8699cf0
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Application/iridium.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 0;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = GXZ23M5TP2;
ENABLE_BITCODE = NO;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -447,7 +447,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 0;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = GXZ23M5TP2;
ENABLE_BITCODE = NO;
GENERATE_INFOPLIST_FILE = YES;
Expand Down
122 changes: 106 additions & 16 deletions Application/iridium/Backend/AgentWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation
import MachO
import PropertyWrapper
import SPIndicator
import UIKit
import ZipArchive

private let binaryName = "AuxiliaryAgent"
Expand Down Expand Up @@ -107,7 +108,8 @@ class Agent {
}
let recipe = AuxiliaryExecute.spawn(
command: binaryLocation.path,
args: ["exec", "whoami"]
args: ["exec", "whoami"],
timeout: 60
)
return recipe
.stdout
Expand All @@ -120,7 +122,8 @@ class Agent {
}
let recipe = AuxiliaryExecute.spawn(
command: binaryLocation.path,
args: ["list"]
args: ["list"],
timeout: 60
)
var result = [AppListElement]()
if let apps = AppListTransfer.decode(jsonString: recipe.stdout) {
Expand All @@ -132,14 +135,43 @@ class Agent {
}

public func decryptApplication(with app: AppListElement, output: @escaping (String) -> Void) -> URL? {
#if DEBUG
if Thread.isMainThread {
fatalError(
"""
this function should not be called from main thread
because we are asking for user interaction later if failure occurred
"""
)
}
#endif

var possibleFailure = false
var wasInterrupted = false

let originalBundleLocation = app.bundleURL
output("TARGET:\n\(originalBundleLocation.path)\n")
guard let binaryLocation = binaryLocation else {
output("\n\nAuxiliary Binary Not Found\n\n")
return nil
}
defer {
output("\n\nProcess Completed\n\n")
if !wasInterrupted {
output("\n\n")
output(
"""
Resign and install may still need additional patch to package payload. You are on your own making those patches.
"""
)
output("\n\n")
if possibleFailure {
output("\n\n==========\n\n")
output("Invalid recipe was detected from backend!\n")
output("Use this package with caution!\n")
output("\n\n==========\n\n")
}
}
output("\n\n[Process Completed]\n\n")
}

// MARK: - STEP 1 - Make a copy of the bundle container
Expand All @@ -157,25 +189,29 @@ class Agent {
attributes: nil
)
defer {
output("Cleaning temporary directory...\n")
output("\n[*] Cleaning temporary directory...\n")
let recipe = AuxiliaryExecute.spawn(
command: binaryLocation.path,
args: ["delete", zipContainer.path]
args: ["delete", zipContainer.path],
timeout: 60
)
output(recipe.stdout)
output(recipe.stderr)
}
repeat {
let recipe = AuxiliaryExecute.spawn(
command: binaryLocation.path,
args: ["copy", originalBundleLocation.path, processBundleLocation.path]
args: ["copy", originalBundleLocation.path, processBundleLocation.path],
timeout: 60
)
output(recipe.stdout)
output(recipe.stderr)
} while false

// MARK: - STEP 2 - Enumerate entire app bundle to find all mach objects

output("\nSearching for mach objects...\n")

var binaries = [(URL, URL)]() // the orig binary is at .0 and we decrypt it to .1
repeat {
let enumerator = FileManager
Expand All @@ -197,7 +233,7 @@ class Agent {
}
let magic = data.magic
if magic == MH_MAGIC_64 || magic == FAT_MAGIC_64 {
output("\n[*] Binary at \(currentObjectFullPath.path)\n")
output("[*] \(objectPath)\n")
binaries.append(
(
originalBundleLocation.appendingPathComponent(objectPath),
Expand All @@ -214,7 +250,7 @@ class Agent {
let foulBinary = foulOptionToUrl(with: foulOption)
output("\n\n[*] Selecting backend \(foulBinary.path)\n\n")
for (origBinary, destBinary) in binaries {
output("\n[*] Decrypter: \(origBinary.lastPathComponent)\n")
output("\n[*] Calling decryption on \(origBinary.lastPathComponent)\n")
let recipe = AuxiliaryExecute.spawn(
command: binaryLocation.path,
args: [
Expand All @@ -223,11 +259,56 @@ class Agent {
"-v",
origBinary.path,
destBinary.path,
]
) { str in
output(str)
}
],
timeout: 60
)
// no longer output them, too noising on normal return
output("\n[*] Recipe: \(recipe.exitCode)\n")
if recipe.exitCode != 0 || recipe.error != nil {
possibleFailure = true
if let error = recipe.error {
output("\n[*] AuxiliaryExecute Error: \(error.localizedDescription)")
}
output("\n[*] stdout\n")
output(recipe.stdout)
output("\n[*] stderr\n")
output(recipe.stderr)
}
}

// MARK: - STEP 3.5 - Fail if ever recipe none 0 but ask for that

if possibleFailure {
var shouldExit = true
let sem = DispatchSemaphore(value: 0)
DispatchQueue.main.async {
let alert = UIAlertController(title: "⚠️", message: "Error occurred during decryption", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ignore", style: .destructive, handler: { _ in
shouldExit = false
sem.signal()
}))
alert.addAction(UIAlertAction(title: "Exit", style: .cancel, handler: { _ in
shouldExit = true
sem.signal()
}))
guard let controller = UIApplication
.shared
.windows
.first?
.topMostViewController
else {
shouldExit = false
sem.signal()
return
}
controller.present(alert, animated: true, completion: nil)
}
sem.wait()
if shouldExit {
output("\n\n[*] Package process interrupted\n")
wasInterrupted = true
return nil
}
}

// MARK: - STEP 4 - Create installer file
Expand All @@ -252,7 +333,14 @@ class Agent {
try? FileManager.default.removeItem(at: zipTarget)

var currentProgress = [String]()
output("\n\nCreaing archive at \(zipTarget.path)\n")
output("\n\n[*] Creaing archive at \(zipTarget.path)\n\n")

let requiredDot = 25 // 4 percent each lol
output(
[String](repeating: ".", count: requiredDot)
.joined(separator: "")
)
output(" [100%]\n")
SSZipArchive.createZipFile(
atPath: zipTarget.path,
withContentsOfDirectory: zipContainer.path,
Expand All @@ -262,13 +350,14 @@ class Agent {
aes: false
) { entryNumber, total in
let percent = Double(entryNumber) / Double(total)
let requiredDot = Double(20)
let currentDot = Int(percent * requiredDot)
let currentDot = Int(percent * Double(requiredDot))
while currentDot > currentProgress.count {
currentProgress.append("=")
output("=")
}
}
output(" ++++++\n")

output("\n\n")

return zipTarget
Expand Down Expand Up @@ -312,7 +401,8 @@ class Agent {
for url in urls {
let recipe = AuxiliaryExecute.spawn(
command: binaryLocation.path,
args: ["delete", url.path]
args: ["delete", url.path],
timeout: 60
)
debugPrint(recipe)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class AppCell: UITableViewCell {
}

func setApp(_ app: AppListElement) {
if let data = Data(base64Encoded: app.primaryIconDataBase64),
let image = UIImage(data: data)
if app.primaryIconData.count > 2, // at least 2 bytes or empty data
let image = UIImage(data: app.primaryIconData) // just for robustness
{
icon.image = image
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import AppListProto
import AuxiliaryExecute
import SPIndicator
import SwifterSwift
import SwiftThrottle
Expand Down
6 changes: 3 additions & 3 deletions AuxiliaryAgent/AuxiliaryAgent/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if CommandLine.arguments[1] == "exec" {
.infoDictionary?["CFBundleShortVersionString"] as? String
?? "0"

var iconBase64Str = ""
var iconData = Data()
repeat {
guard let contents = try? FileManager
.default
Expand Down Expand Up @@ -96,13 +96,13 @@ if CommandLine.arguments[1] == "exec" {
else {
break
}
iconBase64Str = data.base64EncodedString()
iconData = data
} while false

let element = AppListElement(
bundleURL: bundle.bundleURL,
bundleIdentifier: bundle.bundleIdentifier ?? "",
primaryIconDataBase64: iconBase64Str,
primaryIconData: iconData,
localizedName: localizedDisplayName,
version: version,
shortVersion: shortVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import Foundation
public struct AppListElement: Codable {
public let bundleURL: URL
public let bundleIdentifier: String
public let primaryIconDataBase64: String
public let primaryIconData: Data
public let localizedName: String
public let version: String
public let shortVersion: String

public init(
bundleURL: URL,
bundleIdentifier: String,
primaryIconDataBase64: String,
primaryIconData: Data,
localizedName: String,
version: String,
shortVersion: String
) {
self.bundleURL = bundleURL
self.bundleIdentifier = bundleIdentifier
self.primaryIconDataBase64 = primaryIconDataBase64
self.primaryIconData = primaryIconData
self.localizedName = localizedName
self.version = version
self.shortVersion = shortVersion
Expand Down
5 changes: 4 additions & 1 deletion Workflow/prepare.app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ fi
# make our sign
ldid -S"$WORKING_LOCATION/Workflow/Entitlements.plist" "$TARGET_APP/$APPLICATION_NAME"

# now read from the Info.plist for CFBundleShortVersionString
CONTROL_VERSION="$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$TARGET_APP/Info.plist")"

# ==============================================================================

# build agent
Expand All @@ -70,7 +73,7 @@ cd "$WORKING_LOCATION/build/dpkg"
mkdir ./Applications
cp -r "$TARGET_APP" ./Applications/
cp -r "$WORKING_LOCATION/Workflow/DEBIAN" ./
sed -i '' "s/@@VERSION@@/1.0-REL-$TIMESTAMP/g" ./DEBIAN/control
sed -i '' "s/@@VERSION@@/$CONTROL_VERSION-REL-$TIMESTAMP/g" ./DEBIAN/control

# fix permission
chmod -R 0755 DEBIAN
Expand Down

0 comments on commit 8699cf0

Please sign in to comment.