通过stdin/stdout消息远程执行。
implant的Python项目详细描述
植入物
python中异步临时远程过程调用的概念证明。
这是一项正在进行的工作,基本上是一项练习。
功能
- python>;=3.5异步
- 临时可转让远程程序
- implant.core.command的远程部分可以位于单独的模块中
- aimplant.core.commandspecificimplant.core.channel 在本地和远程端之间启用任意协议
- 事件
- 相当小的磁芯
- 测试
限制
- python>;=3.5
- 如果未使用venv,则远程导入仅支持纯python模块
- implant.core.commands必须位于除main>以外的模块中
- 目前,sudo不能要求输入密码
示例
一般应用
importasyncioimportpathlibfromimplantimportcore,connect,commandsasyncdefremote_tasks():# create a connector for a python processconnector=connect.Lxd(container='zesty',hostname='localhost')connector_args={'python_bin':pathlib.Path('/usr/bin/python3')}# connect to a remote python processremote=awaitconnector.launch(**connector_args)# start remote communication taskscom_remote=asyncio.ensure_future(remote.communicate())try:# execute commandcmd=commands.SystemLoad()result=awaitremote.execute(cmd)print("Remote system load:",result)finally:# stop communication taskscom_remote.cancel()awaitcom_remoteif__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(remote_tasks())loop.close()
回声命令示例
importloggingimportosfromimplantimportcorelog=logging.getLogger(__name__)classEcho(core.Command):"""Demonstrate the basic command API."""asyncdeflocal(self,context):"""The local side of the RPC.
:param context: :py:obj:`implant.core.DispatchLocalContext`
"""# custom protocol# first: sendawaitcontext.channel.send_iteration("send to remote")# second: receivefrom_remote=[]asyncforxincontext.channel:from_remote.append(x)log.debug("************ receiving from remote: %s",from_remote)# third: wait for remote to finish and return resultremote_result=awaitcontext.remote_futureresult={'from_remote':''.join(from_remote),}result.update(remote_result)returnresultasyncdefremote(self,context):"""The remote side of the RPC.
:param context: :py:obj:`implant.core.DispatchRemoteContext`
"""# first: receivefrom_local=[]asyncforxincontext.channel:from_local.append(x)log.debug("************ receiving from local: %s",from_local)# second: sendawaitcontext.channel.send_iteration("send to local")# third: return resultreturn{'from_local':''.join(from_local),'remote_self':self,'pid':os.getpid()}
内部
master <-----------------------------------------> remote | stdin/stdout | chunks | channels | --> send ---> | | --> queue --> | module:class/fqin | <-- queue <-- | | <--- send <--