GCC_BIN_IOS=`xcrun --sdk iphoneos -f gcc`
GCC_BASE_IOS=$(GCC_BIN_IOS)
SDK_IOS=`xcrun --sdk iphoneos --show-sdk-path`
GCC_IOS=$(GCC_BASE_IOS) -arch arm64 -isysroot $(SDK_IOS) -fno-stack-protector -fno-exceptions -fpie -fPIC -fPIE -fpic
CLANG_IOS=-arch arm64 -mios-version-min=11.0 -isysroot $(SDK_IOS)
CXXFLAGS=-std=c++11 $(CLANG_IOS)

all: clean exploit.bin

loader: loader.c
	$(GCC_IOS) -o loader loader.c

exploit.bin: loader payload.dylib
	ruby create_bin.rb loader

%.o: %.m
	clang -c $(CLANG_IOS) $< -o $@ -fno-stack-protector -fobjc-arc -fmodules

%.o: %.c
	clang -c $(CLANG_IOS) $< -o $@ -fno-stack-protector -fmodules -Iheaders

%.o: liboffsetfinder/%.cpp
	clang -c $(CLANG_IOS) $< -o $@ -fno-stack-protector -fmodules

payload.dylib: payload.o v0rtex.o async_wake.o kmem.o kutils.o koffsets.o kexecute.o find_port.o early_kalloc.o sandbox.o vnode_utils.o offsetof.c patchfinder64.o kernel_utils.o trustcache.o sha1.o sha256.o liboffsetfinder64/getoffsets.o liboffsetfinder64/img4.o liboffsetfinder64/lzssdec.o liboffsetfinder64/exception.o liboffsetfinder64/liboffsetfinder64.o liboffsetfinder64/insn.o liboffsetfinder64/patch.o
	clang $(CLANG_IOS) $^ -shared -o $@ -bind_at_load \
		-fno-stack-protector -fobjc-arc -fmodules -framework IOKit -lc++
	strip -u -r payload.dylib

install: exploit.bin
	mkdir -p ../../../../data/exploits/CVE-2017-13861
	cp exploit.bin ../../../../data/exploits/CVE-2017-13861/exploit

clean:
	rm -f *.o loader exploit.bin payload.dylib liboffsetfinder64/*.o

