Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Merge pull request #30 from xatuke/patch-1

Lakr774182169883
diff --git a/README.md b/README.md
index 91c0ba8f..aab8e28c 100644
--- a/README.md
+++ b/README.md
@@ -46,54 +46,89 @@ make build # build + sign vphone-cli
make vm_new # create vm/ directory (ROMs, disk, SEP storage)
make fw_prepare # download IPSWs, extract, merge, generate manifest
make fw_patch # patch boot chain (6 components, 41+ modifications)
-make boot_dfu # boot VM in DFU mode
+```
+
+## Restore
+
+You'll need **two terminals** for the restore process. Keep terminal 1 running while using terminal 2.
+
+```bash
+# terminal 1
+make boot_dfu # boot VM in DFU mode (keep running)
+```
+
+```bash
+# terminal 2
make restore_get_shsh # fetch SHSH blob
make restore # flash firmware via idevicerestore
```
## Ramdisk and CFW
-After restoring, boot into DFU again and load the SSH ramdisk:
+Stop the DFU boot in terminal 1 (Ctrl+C), then boot into DFU again for the ramdisk:
```bash
-make boot_dfu # terminal 1
+# terminal 1
+make boot_dfu # keep running
+```
+
+```bash
+# terminal 2
make ramdisk_build # build signed SSH ramdisk
-make ramdisk_send # terminal 2 — send to device
+make ramdisk_send # send to device
```
-Install CFW (Cryptexes, patched binaries, jailbreak tools, LaunchDaemons):
+Once connected, install CFW:
```bash
+# terminal 2
iproxy 2222 22
make cfw_install
```
-## Boot
+## First Boot
+
+Stop the DFU boot in terminal 1 (Ctrl+C), then:
```bash
make boot
```
-On first boot, initialize the shell environment:
+This gives you a **direct console** on the VM. When you see `bash-4.4#`, press Enter and run these commands to initialize the shell environment and generate SSH host keys:
```bash
-# binaries are looking for each others via PATH so do not ignore this one
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games:/iosbinpack64/usr/local/sbin:/iosbinpack64/usr/local/bin:/iosbinpack64/usr/sbin:/iosbinpack64/usr/bin:/iosbinpack64/sbin:/iosbinpack64/bin'
-# call with fullpath
-/iosbinpack64/bin/mkdir -p /var/dropbear
-/iosbinpack64/bin/cp /iosbinpack64/etc/profile /var/profile
-/iosbinpack64/bin/cp /iosbinpack64/etc/motd /var/motd
+mkdir -p /var/dropbear
+cp /iosbinpack64/etc/profile /var/profile
+cp /iosbinpack64/etc/motd /var/motd
+
+# generate SSH host keys (required for SSH to work)
+dropbearkey -t rsa -f /var/dropbear/dropbear_rsa_host_key
+dropbearkey -t ecdsa -f /var/dropbear/dropbear_ecdsa_host_key
+
shutdown -h now
```
-After subsequent boots, connect via:
+> **Note:** Without the host key generation step, dropbear (SSH server) will accept connections but immediately close them because it has no keys to perform the SSH handshake.
+
+## Subsequent Boots
+
+```bash
+make boot
+```
+
+In a separate terminal, start iproxy tunnels:
```bash
iproxy 22222 22222 # SSH
iproxy 5901 5901 # VNC
```
+Connect via:
+- **SSH:** `ssh -p 22222 [email protected]` (password: `alpine`)
+- **VNC:** `vnc://127.0.0.1:5901`
+
## All Make Targets
Run `make help` for the full list. Key targets:
@@ -124,6 +159,23 @@ AMFI is not disabled. Set the boot-arg and restart:
sudo nvram boot-args="amfi_get_out_of_my_way=1 -v"
```
+**Q: I'm stuck on the "Press home to continue" screen.**
+
+Connect via VNC (`vnc://127.0.0.1:5901`) and right-click anywhere on the screen (two-finger click on a Mac trackpad). This simulates the home button press.
+
+**Q: SSH connects but immediately closes (`Connection closed by 127.0.0.1`).**
+
+Dropbear host keys were not generated during first boot. Connect via VNC or the `make boot` console and run:
+
+```bash
+export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games:/iosbinpack64/usr/local/sbin:/iosbinpack64/usr/local/bin:/iosbinpack64/usr/sbin:/iosbinpack64/usr/bin:/iosbinpack64/sbin:/iosbinpack64/bin'
+mkdir -p /var/dropbear
+dropbearkey -t rsa -f /var/dropbear/dropbear_rsa_host_key
+dropbearkey -t ecdsa -f /var/dropbear/dropbear_ecdsa_host_key
+killall dropbear
+dropbear -R -p 22222
+```
+
**Q: Can I update to a newer iOS version?**
Yes. Override `fw_prepare` with the IPSW URL for the version you want:
diff --git a/sources/vphone-cli/VPhoneVMView.swift b/sources/vphone-cli/VPhoneVMView.swift
index 9f652419..1629f8cf 100644
--- a/sources/vphone-cli/VPhoneVMView.swift
+++ b/sources/vphone-cli/VPhoneVMView.swift
@@ -5,6 +5,53 @@ import Virtualization
class VPhoneVMView: VZVirtualMachineView {
var keyHelper: VPhoneKeyHelper?
+
+ private var currentTouchSwipeAim: Int = 0
+
+ // MARK: - Private API Accessors
+
+ // https://github.com/wh1te4ever/super-tart-vphone-writeup/blob/main/contents/ScreenSharingVNC.swift
+ private var multiTouchDevice: AnyObject? {
+ guard let vm = self.virtualMachine else { return nil }
+ guard let devices = Dynamic(vm)._multiTouchDevices.asObject as? NSArray,
+ devices.count > 0
+ else {
+ return nil
+ }
+ return devices.object(at: 0) as AnyObject
+ }
+
+ // MARK: - Event Handling
+
+ override func mouseDown(with event: NSEvent) {
+ let location = self.convert(event.locationInWindow, from: nil)
+
+ self.currentTouchSwipeAim = hitTestEdge(at: location)
+
+ sendTouchEvent(
+ phase: 0, // Began
+ locationInWindow: event.locationInWindow,
+ timestamp: event.timestamp
+ )
+ }
+
+ override func mouseDragged(with event: NSEvent) {
+ sendTouchEvent(
+ phase: 1, // Moved
+ locationInWindow: event.locationInWindow,
+ timestamp: event.timestamp
+ )
+ super.mouseDragged(with: event)
+ }
+
+ override func mouseUp(with event: NSEvent) {
+ sendTouchEvent(
+ phase: 3, // Ended
+ locationInWindow: event.locationInWindow,
+ timestamp: event.timestamp
+ )
+ super.mouseUp(with: event)
+ }
override func rightMouseDown(with _: NSEvent) {
guard let keyHelper else {
@@ -13,4 +60,88 @@ class VPhoneVMView: VZVirtualMachineView {
}
keyHelper.sendHome()
}
-}
+
+ // MARK: - Touch Injection Logic
+
+ private func sendTouchEvent(phase: Int, locationInWindow: NSPoint, timestamp: TimeInterval) {
+ guard let device = multiTouchDevice,
+ let vm = self.virtualMachine
+ else { return }
+
+ let normalizedPoint = normalizeCoordinate(locationInWindow)
+
+ let touch = Dynamic._VZTouch(
+ view: vm,
+ index: 0,
+ phase: phase,
+ location: normalizedPoint,
+ swipeAim: currentTouchSwipeAim,
+ timestamp: timestamp
+ )
+
+ guard let touchObj = touch.asObject else {
+ print("[vphone] Error: Failed to create _VZTouch")
+ return
+ }
+
+ let touchEvent = Dynamic._VZMultiTouchEvent(touches: [touchObj])
+ guard let eventObj = touchEvent.asObject else { return }
+
+ Dynamic(device).sendMultiTouchEvents([eventObj] as NSArray)
+ }
+
+ // MARK: - Coordinate Helpers
+
+ private func normalizeCoordinate(_ locationInWindow: NSPoint) -> CGPoint {
+ let localPoint = self.convert(locationInWindow, from: nil)
+ let w = self.bounds.width
+ let h = self.bounds.height
+
+ guard w > 0, h > 0 else { return .zero }
+
+ var nx = Double(localPoint.x / w)
+ var ny = Double(localPoint.y / h)
+
+ // Clamp
+ nx = max(0.0, min(1.0, nx))
+ ny = max(0.0, min(1.0, ny))
+
+ if !self.isFlipped {
+ ny = 1.0 - ny
+ }
+
+ return CGPoint(x: nx, y: ny)
+ }
+
+ private func hitTestEdge(at point: CGPoint) -> Int {
+ let w = self.bounds.width
+ let h = self.bounds.height
+
+ let edgeThreshold: CGFloat = 32.0
+
+ let distLeft = point.x
+ let distRight = w - point.x
+ let distTop = self.isFlipped ? point.y : (h - point.y)
+ let distBottom = self.isFlipped ? (h - point.y) : point.y
+
+ var minDist = distLeft
+ var edgeCode = 8 // Left
+
+ if distRight < minDist {
+ minDist = distRight
+ edgeCode = 4 // Right
+ }
+
+ if distBottom < minDist {
+ minDist = distBottom
+ edgeCode = 2 // Bottom (Home bar swipe up)
+ }
+
+ if distTop < minDist {
+ minDist = distTop
+ edgeCode = 1 // Top (Notification Center)
+ }
+
+ return minDist < edgeThreshold ? edgeCode : 0
+ }
+}
\ No newline at end of file