导语
最近发现一个有趣的python包,可以通过连接iCloud来操作自己的iPhone手机,比如获得自己的iPhone的位置信息,让自己的iPhone自动响铃等等。比如你的iPhone被小偷偷走了,你可以写个定时脚本,半夜让iPhone时不时地响次铃,应该还是蛮刺激的。或者如果你知道你朋友的iCloud账户密码,然后你就可以...
废话不多说,让我们愉快地开始吧~
开发工具
Python 版本: 3.7.8
相关模块:
pyicloud模块;
click模块;
以及一些pyt hon 自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
开始玩耍
首先,我们需要先pip安装一下这个第三方包:
pip install pyicloud
然后用你拥有的iCloud账号密码进行认证:
from pyicloud import PyiCloudService
api = PyiCloudService('xxxx@icloud.com', 'xxxx')
如果你开了双重认证的话,则认证所需的代码会更加复杂一些:
import click
from pyicloud import PyiCloudService
api = PyiCloudService('xxxx@icloud.com', 'xxxx')
if api.requires_2fa:
code = input('Enter the code you received of one of your approved devices: ')
result = api.validate_2fa_code(code)
if not result: raise RuntimeError('Failed to verify security code')
if not api.is_trusted_session:
result = api.trust_session()
if not result: raise RuntimeError('Failed to request trust. You will likely be prompted for the code again in the coming weeks')
elif api.requires_2sa:
devices = api.trusted_devices
for i, device in enumerate(devices):
print(
" %s: %s" % (i, device.get('deviceName',
"SMS to %s" % device.get('phoneNumber')))
)
device = click.prompt('Which device would you like to use?', default=0)
device = devices[device]
if not api.send_verification_code(device): raise RuntimeError('Failed to send verification code')
code = click.prompt('Please enter validation code')
if not api.validate_verification_code(device, code): raise RuntimeError('Failed to verify verification code')
成功认证之后,你就可以用这个接口干很多有意思的东西了,比如查询自己的账号在哪些设备上认证过了(这个其实可以间接地帮助你查询自己的账号是否被盗用过):
api.devices
效果如下:
查询对应的设备的物理位置(手机找不到的时候可以用?):
api.iphone.location() # 如果你只有一台关联该账号的苹果设备的话
api.devices[0].location() # []用来索引对应的设备
效果如下:
可以发现你手机的位置信息,例如经纬度,确定经纬度时是使用的WIfi还是蜂窝网络等等信息都是可以返回的。该API其实还可以实现很多有趣的功能(比如获得你的通讯录信息,备份你的iCloud里的文件等等),这个我们后面有时间可以继续介绍,这里为了不跑题,我们直接开始表演让手机自动发出声音吧,其实很简单,你只需要这样调用接口即可进行整蛊:
api.devices[1].play_sound() # 设备一定要选对,别回头吓到自己
过几秒钟之后,你的手机就会播放铃声并显示默认通知了("查找我的 iPhone 警报"),并且将向您发送一封确认电子邮件。手机上的效果类似这样:
接下来,你只需要写个定时程序,指定每天晚上12点-凌晨3点每隔一段时间就让手机响一次就可以实现午夜凶铃的效果啦:
while True:
now_time = datetime.datetime.now()
hour = str(now_time).split(' ')[1].split(':')[0]
if hour in ['00', '01', '02']:
api.devices[1].play_sound()
time.sleep(random.randint(500, 800))
整理一下,完整源代码如下:
import time
import click
import random
import datetime
from pyicloud import PyiCloudService
api = PyiCloudService('xxx', 'xxx')
if api.requires_2fa:
code = input('Enter the code you received of one of your approved devices: ')
result = api.validate_2fa_code(code)
if not result: raise RuntimeError('Failed to verify security code')
if not api.is_trusted_session:
result = api.trust_session()
if not result: raise RuntimeError('Failed to request trust. You will likely be prompted for the code again in the coming weeks')
elif api.requires_2sa:
devices = api.trusted_devices
for i, device in enumerate(devices):
print(
" %s: %s" % (i, device.get('deviceName',
"SMS to %s" % device.get('phoneNumber')))
)
device = click.prompt('Which device would you like to use?', default=0)
device = devices[device]
if not api.send_verification_code(device): raise RuntimeError('Failed to send verification code')
code = click.prompt('Please enter validation code')
if not api.validate_verification_code(device, code): raise RuntimeError('Failed to verify verification code')
while True:
now_time = datetime.datetime.now()
hour = str(now_time).split(' ')[1].split(':')[0]
if hour in ['00', '01', '02']:
api.devices[1].play_sound()
time.sleep(random.randint(500, 800))
效果展示就算了,不方便弄,感兴趣的小伙伴可以自己动手试试。更多好玩有趣的代码点这里获取