使用python做内网穿透小工具实现花生壳功能!

测试系统:
外网:linux
内网:windows
Python:python3.6(需要安装flask、psutil)
功能介绍:使用flask,实现可一键添加端口映射,可以删除单端口,可删除全部端口映射
需要准备的东西:
1:如上的内网穿透工具:git clone https://gitee.com/stlswm/transponder.git
2:外网服务器 (需要安装 Nginx),开放端口 9000-9999
3:域名,需要泛域名,基本上是添加一个@.domain.com(没有的话可以使用ip+端口来访问)
外网服务器:
把 outer_server 中的二进制linux可执行文件main移动到随意文件夹,文件地址替换python中mainPath变量,
【使用python做内网穿透小工具实现花生壳功能!】python文件
from flask import Flaskfrom flask import requestimport osimport timeimport randomimport sysimport stringimport psutilimport reApp = Flask(__name__)# 文件存放地址mainPath="/root/go/src/transponder/outer_server/"# ng配置文件存放地址ngConfigPath="/www/server/panel/vhost/nginx/"# 你的域名domain="domain.com"#添加端口映射@app.route('/add')def add():# 设置端口信息if os.path.exists("./port.txt") == False:pf=open("./port.txt",'w')pf.write("9000")pf.closeport=9000else:portFile = open("./port.txt",'r')port=portFile.read()portFile.close()serverPort=int(port)+1# 端口信息存会文件中portFile = open("./port.txt",'w')portFile.write(str(serverPort))portFile.close()fileName="main" + str(serverPort)# 操作服务端 打包添加配置文件os.mkdir(mainPath+fileName+"s")os.system('cd '+mainPath+';cp -r main ./'+fileName+"s;cd "+fileName+"s;mv main "+fileName)fp = open(mainPath+fileName+"s/outer.config.json",'w')psd=random.randint(10000000000,90000000000)fp.write('{"InnerServerAddress": "tcp://0.0.0.0:'+str(serverPort)+'","OuterServerAddress": "unix:///var/run/'+fileName+'.sock","AuthKey": "'+str(psd)+'"}')fp.close()#操作ng添加ng配置文件f=open(ngConfigPath+fileName+".conf","w")head=generate_random_str(6)f.write("server {listen 80;server_name"+head+"."+domain+";access_log/var/log/www.abc.com.access.log;error_log/var/log/www.abc.com.error.log;location / {proxy_pass http://unix:/var/run/"+fileName+".sock:/;proxy_redirectoff;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;proxy_max_temp_file_size 0;proxy_connect_timeout90;proxy_send_timeout90;proxy_read_timeout90;proxy_buffer_size4k;proxy_buffers4 32k;proxy_busy_buffers_size64k;proxy_temp_file_write_size 64k;}}")f.close()os.system("nginx -s reload")os.system("cd "+mainPath+fileName+"s;nohup ./"+fileName+" &")f=open("./portAll.txt","a")f.write(","+fileName)f.close()return "<h1 style='margin-bottom:30px;'>启动成功</h1><br>服务器:119.45.136.232:"+str(serverPort)+"<br>客户端域名:http://"+head+"."+domain+"<br>秘钥:"+str(psd)#删除对应的外网端口@app.route('/kill')def kill():port = request.args.get("port")fileName="main"+str(port)pid=processinfo(fileName)if pid==False:return "没有找改进程"os.system("kill -9 "+str(pid))os.system("cd "+mainPath+";rm -rf "+fileName+"s")os.system("cd /run;rm"+fileName+".sock")os.system("cd "+ngConfigPath+";rm "+fileName+".conf;nginx -s reload")return "操作成功!"#删除全部端口映射@app.route('/killAll')def killAll():if os.path.exists("./portAll.txt") == False:return "无端口映射"portFile = open("./portAll.txt",'r')portAllStr=portFile.read()portAllArr=portAllStr.split(",")myStr="<h1 style='margin-bottom:30px;'>操作成功</h1>"for v in portAllArr:if len(v) > 0:fileName=vpid=processinfo(fileName)if pid==False:myStr=myStr+"未找到进程:"+fileName+"<br>"else :myStr=myStr+"成功停止进程:"+fileName+"<br>"os.system("kill -9 "+str(pid))os.system("cd "+mainPath+";rm -rf "+fileName+"s")os.system("cd /run;rm "+fileName+".sock")os.system("cd "+ngConfigPath+";rm "+fileName+".conf;nginx -s reload")f=open("./portAll.txt","w")f.write("")f.close()f=open("./port.txt","w")f.write("9000")f.close()return myStr# 获取进程IDdef processinfo(processName):pids = psutil.pids()for pid in pids:# print(pid)p = psutil.Process(pid)# print(p.name)if p.name() == processName:# print(pid)return pid# 如果找到该进程则打印它的PID,返回truereturn False# 没有找到该进程,返回false# 随机生成字符串def generate_random_str(randomlength):'''string.digits = 0123456789string.ascii_letters = 26个小写,26个大写'''str_list = random.sample(string.digits + string.ascii_letters,randomlength)random_str = ''.join(str_list)return random_strif __name__ == '__main__':app.run(host="0.0.0.0",port="8001")


推荐阅读