Skip to content

Commit

Permalink
Version 5
Browse files Browse the repository at this point in the history
**Rewritten***

- Now disables iPad blacklist
- Fixed `Read-only file system` error (Thanks to: #1 )
  • Loading branch information
pook authored and pook committed Sep 26, 2019
1 parent 328eb9b commit 6c9a528
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 69 deletions.
Binary file modified README.md
Binary file not shown.
181 changes: 112 additions & 69 deletions main.swift
@@ -1,15 +1,13 @@
/*
SidecarPatcher - Version 4
Enabling Sidecar on old Mac (2015 or older)
Tested on macOS 10.15 Beta 1 (19A471t). But I don't have old Mac so I don't know it works.
THIS SCRIPT DOESN'T MAKE SidecarCore BACKUP SO YOU HAVE TO DO THIS MANUALLY. PLEASE BACKUP /System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore. PLEASE. PLEASE. PLEASE.
And after patching SidecarCore, this script won't work until replacing to original one.
This script requires disabling SIP, and running as root.
*/

import Darwin
SidecarPatcher - Version 5
Enabling Sidecar on old Mac (2015 or older)
But I don't have Sidecar-unsupported Mac so I don't know it works.
THIS SCRIPT DOESN'T MAKE SidecarCore BACKUP SO YOU HAVE TO DO THIS MANUALLY. PLEASE BACKUP /System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore. PLEASE. PLEASE. PLEASE.
And after patching SidecarCore, this script won't work until replacing to original one.
This script requires disabling SIP, and running as root.
*/
import Foundation

func shell(_ command: String) -> String{
Expand All @@ -26,88 +24,133 @@ func shell(_ command: String) -> String{
return output
}

func printError(_ message: String){
print("Ooops! Something went wrong: \(message)")
exit(1)
}

// From https://gist.github.com/brennanMKE/a0a2ee6aa5a2e2e66297c580c4df0d66
fileprivate func directoryExistsAtPath(_ path: String) -> Bool {
var isDirectory = ObjCBool(true)
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory.boolValue
}

let fileManager = FileManager()
let original = shell("xxd -p /System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore | tr -d '\n'")
let original_model_list = "694d616331332c3100694d616331332c3200694d616331332c3300694d616331342c3100694d616331342c3200694d616331342c3300694d616331342c3400694d616331352c3100694d616331362c3100694d616331362c32004d6163426f6f6b382c31004d6163426f6f6b416972352c31004d6163426f6f6b416972352c32004d6163426f6f6b416972362c31004d6163426f6f6b416972362c32004d6163426f6f6b416972372c31004d6163426f6f6b416972372c32004d6163426f6f6b50726f392c31004d6163426f6f6b50726f392c32004d6163426f6f6b50726f31302c31004d6163426f6f6b50726f31302c32004d6163426f6f6b50726f31312c31004d6163426f6f6b50726f31312c32004d6163426f6f6b50726f31312c33004d6163426f6f6b50726f31312c34004d6163426f6f6b50726f31312c35004d6163426f6f6b50726f31322c31004d61636d696e69362c31004d61636d696e69362c32004d61636d696e69372c31004d616350726f352c31004d616350726f362c31"
let patched_model_list = "694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c30004d6163426f6f6b302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b50726f302c30004d6163426f6f6b50726f302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d61636d696e69302c30004d61636d696e69302c30004d61636d696e69302c30004d616350726f302c30004d616350726f302c30"
var patched: String

if #available(macOS 10.15, *){
()
}else{
printError("You're not using macOS 10.15 or later!")
func importCore(path: String) -> String{
return shell("xxd -p \"\(path)/SidecarCore\" | tr -d '\n'")
}

if original.contains(patched_model_list){
printError("Already patched!")
}

if !original.contains(original_model_list){
printError("Not supported SidecarCore! or seems like damaged.")
func checkSystem(core: String, code: [PatchCode]) -> [PatchCode]{
var result: [PatchCode] = []

// Check OS
if #available(macOS 10.15, *){
()
}else{
assertionFailure("You're not using macOS 10.15 or later!")
}

// Check the system is patched (macModel and iPadModel)
for value in code{
if !core.contains(value.patched){
result.append(value)
}
}
if result.isEmpty{
assertionFailure("Already patched!")
}

// Check SidecarCore is damaged
for value in code{
if !core.contains(value.original){
assertionFailure("Not supported SidecarCore! or seems like damaged")
}
}

// Check SIP status
if !(shell("csrutil status") == ("System Integrity Protection status: disabled.\n")){
assertionFailure("System Integrity Protection is enabled")
}

// Check privilege
if !(shell("id -u") == "0\n"){
assertionFailure("Must be run as root")
}

// return patch code
return result
}

if !(shell("csrutil status") == ("System Integrity Protection status: disabled.\n")){
printError("System Integrity Protection is enabled.")
func patch(core: String, code: [PatchCode]) -> String{
var core = core
for value in code{
core.replaceSubrange(core.range(of: value.original)!, with: value.patched)
}
return core
}

if !(shell("id -u") == "0\n"){
printError("Must be run as root.")
}
func exportCore(core: String, path: String){
// Remove existing tmp dir
if directoryExistsAtPath("/tmp/SidecarPatcher"){
do {
try fileManager.removeItem(atPath: "/tmp/SidecarPatcher")
}
catch let error as NSError {
assertionFailure("\(error)")
}
}

patched = original
patched.replaceSubrange(original.range(of: original_model_list)!, with: patched_model_list)
// Create temp dir
let temp_dir = URL(fileURLWithPath: "/tmp/").appendingPathComponent("SidecarPatcher").path
do{
try fileManager.createDirectory(atPath: temp_dir, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
assertionFailure("\(error)")
}

if directoryExistsAtPath("/tmp/SidecarPatcher"){
// Export code
let output_path = URL(fileURLWithPath: "/tmp/SidecarPatcher").appendingPathComponent("output.txt")
do {
try fileManager.removeItem(atPath: "/tmp/SidecarPatcher")
try core.write(to: output_path, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
assertionFailure("\(error)")
}
catch let error as NSError {
printError("\(error)")
}
}
shell("xxd -r -p /tmp/SidecarPatcher/output.txt /tmp/SidecarPatcher/SidecarCore")

let temp_dir = URL(fileURLWithPath: "/tmp/").appendingPathComponent("SidecarPatcher").path
do{
try fileManager.createDirectory(atPath: temp_dir, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
printError("\(error)")
}

let patched_output_path = URL(fileURLWithPath: "/tmp/SidecarPatcher").appendingPathComponent("output.txt")
do {
try patched.write(to: patched_output_path, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
printError("\(error)")
}
shell("xxd -r -p /tmp/SidecarPatcher/output.txt /tmp/SidecarPatcher/SidecarCore")
// Mount system (thanks to: https://github.com/pookjw/SidecarPatcher/issues/1)
shell("sudo mount -uw /")

// Remove existing original system file
if fileManager.fileExists(atPath: "\(path)/SidecarCore") {
do {
try fileManager.removeItem(atPath: "\(path)/SidecarCore")
}
catch let error as NSError {
assertionFailure("\(error)")
}
}


if fileManager.fileExists(atPath: "/System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore") {
// Copy system file
do {
try fileManager.removeItem(atPath: "/System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore")
try fileManager.copyItem(atPath: "/tmp/SidecarPatcher/SidecarCore", toPath: "\(path)/SidecarCore")
}
catch let error as NSError {
printError("\(error)")
assertionFailure("\(error)")
}
}

do {
try fileManager.copyItem(atPath: "/tmp/SidecarPatcher/SidecarCore", toPath: "/System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore")
// codesign
shell("sudo codesign -f -s - \"\(path)/SidecarCore\"")
shell("sudo chmod 755 \"\(path)/SidecarCore\"")
}
catch let error as NSError {
printError("\(error)")

struct PatchCode{
var original: String
var patched: String
}

shell("sudo codesign -f -s - /System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/SidecarCore")
let fileManager = FileManager()
let SidecarCorePath = "/System/Library/PrivateFrameworks/SidecarCore.framework/Versions/A/"
let macModel = PatchCode(original: "694d616331332c3100694d616331332c3200694d616331332c3300694d616331342c3100694d616331342c3200694d616331342c3300694d616331342c3400694d616331352c3100694d616331362c3100694d616331362c32004d6163426f6f6b382c31004d6163426f6f6b416972352c31004d6163426f6f6b416972352c32004d6163426f6f6b416972362c31004d6163426f6f6b416972362c32004d6163426f6f6b416972372c31004d6163426f6f6b416972372c32004d6163426f6f6b50726f392c31004d6163426f6f6b50726f392c32004d6163426f6f6b50726f31302c31004d6163426f6f6b50726f31302c32004d6163426f6f6b50726f31312c31004d6163426f6f6b50726f31312c32004d6163426f6f6b50726f31312c33004d6163426f6f6b50726f31312c34004d6163426f6f6b50726f31312c35004d6163426f6f6b50726f31322c31004d61636d696e69362c31004d61636d696e69362c32004d61636d696e69372c31004d616350726f352c31004d616350726f362c31", patched: "694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c3000694d616330302c30004d6163426f6f6b302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b416972302c30004d6163426f6f6b50726f302c30004d6163426f6f6b50726f302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d6163426f6f6b50726f30302c30004d61636d696e69302c30004d61636d696e69302c30004d61636d696e69302c30004d616350726f302c30004d616350726f302c30")
let iPadModel = PatchCode(original: "69506164342c310069506164342c320069506164342c330069506164342c340069506164342c350069506164342c360069506164342c370069506164342c380069506164342c390069506164352c310069506164352c320069506164352c330069506164352c340069506164362c31310069506164362c3132", patched: "69506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C300069506164302C30300069506164302C3030")

let originalCore = importCore(path: SidecarCorePath) // get code
let patchToCode = checkSystem(core: originalCore, code: [macModel, iPadModel]) // check availability
let patchedCore = patch(core: originalCore, code: patchToCode) // patch code
exportCore(core: patchedCore, path: SidecarCorePath) // copy patched code to system
print("Reboot your macOS.")
exit(0)

0 comments on commit 6c9a528

Please sign in to comment.