https://www.cnblogs.com/iAmSoScArEd/p/18140656 - 我超怕的
code
from flask import Flask, request
import csv
app = Flask(__name__)
@app.route('/', methods=['POST'])
def receive_data():
data = request.data.decode('utf-8')
rows = data.split(':::')
with open('output.csv', 'a', newline='',encoding="utf-8-sig") as file:
writer = csv.writer(file)
#writer.writerow(['ip', '用户名', 'public_key'])
user = ""
for row in rows:
row = row.strip()
if not row:
continue
if '|||' in row:
_, *public_keys, _ = row.split('|||')
for public_key in public_keys:
writer.writerow([request.remote_addr, user, public_key])
else:
user = row
return 'Data received and saved to output.csv'
@app.route('/download_bash',methods=['GET'])
def download_bash():
bash_str = '''#!/bin/bash
# 修改远程HTTP服务器URL
remote_server_url="http://127.0.0.1:5000/"
user_list=$(cut -d: -f1 /etc/passwd)
output_file="/tmp/ssh_public_keys.txt"
> ${output_file}
for user in ${user_list}; do
home_dir=$(eval echo ~${user})
known_hosts_file="${home_dir}/.ssh/authorized_keys"
if [ -f "${known_hosts_file}" ]; then
echo ${user}
echo ":::${user}:::" >> ${output_file}
while IFS= read -r line; do
if [[ ! "${line}" =~ ^\s*(#|$) ]]; then
public_key=$(echo "${line}")
echo ${public_key}
echo "|||${public_key}|||" >> ${output_file}
fi
done < "${known_hosts_file}"
echo >> ${output_file}
fi
done
curl -X POST -H "Content-Type: text/plain" --data-binary "@${output_file}" ${remote_server_url}
rm ${output_file}
'''
return bash_str
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
How to use
1、部署代码并运行,并给出地址,如:10.100.100.1:5000
2、修改代码remote_server_url="http://127.0.0.1:5000/" 为上步骤的地址
3、在需要扫描的机器上执行 curl http://10.100.100.1:5000/download_bash | sudo bash即可