Webshell
“web”的含义是显然需要服务器开放web服务,“shell”的含义是取得对服务器某种程度上操作权限。webshell常常被称为通过网站端口对网站服务器的某种程度上操作的权限。
一方面,webshell被站长常常用于网站管理、服务器管理等等,根据FSO权限的不同,作用有在线编辑网页脚本、上传下载文件、查看数据库、执行任意程序命令等。
另一方面,被入侵者利用,从而达到控制网站服务器的目的。这些网页脚本常称为WEB脚本木马,比较流行的asp或php木马,也有基于.NET的脚本木马与JSP脚本木马。国内常用的WebShell有海阳ASP木马,Phpspy,c99shell等。
web
端使用Xterm.js
或者其他的WebShell
组件和websocket
后端只需要支持WebSocket
和SSH
协议的远程登录模块即可
web端实现
1.安装
npm install xterm@3.1.0 --save
指定版本安装,最新版的xterm文件的改动很大,使用下面的方法会报错
2.导包
1 2 3 4 5 6
| import 'xterm/dist/xterm.css'; import { Terminal } from 'xterm';
import * as fit from 'xterm/lib/addons/fit/fit'; Terminal.applyAddon(fit);
|
3.在页面中的显示
1 2 3 4 5
| <template> <div> <div id="terminal" style="width: 500px;height:300px;"></div> </div> </template>
|
4.数据操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| mounted () {
this.terminalSocket = new WebSocket("ws://127.0.0.1:8000/web/"); this.terminalSocket.onmessage = (res) => { console.log(res.data); this.term.writeln("\r\n"+res.data); this.order = "" this.term.write("\r\n$ "); }
let terminalContainer = document.getElementById('terminal') this.term = new Terminal({ cursorBlink: true, cursorStyle: "underline" }) this.term.open(terminalContainer) },
|
5.web端完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <template> <div class="console" id="terminal"></div> </template> <script> import { Terminal } from 'xterm' import * as attach from 'xterm/lib/addons/attach/attach' import * as fit from 'xterm/lib/addons/fit/fit' export default { name: 'webssh', data () { return { term: null, terminalSocket: null, order:'' } }, methods: { }, mounted () {
this.terminalSocket = new WebSocket("ws://127.0.0.1:8000/web/"); this.terminalSocket.onmessage = (res) => { console.log(res.data); this.term.writeln("\r\n"+res.data); this.order = "" this.term.write("\r\n$ "); }
let terminalContainer = document.getElementById('terminal') this.term = new Terminal({ cursorBlink: true, cursorStyle: "underline" }) this.term.open(terminalContainer)
this.term.write('$ ') this.term.on('key', (key, ev)=>{ console.log("key==========", ev.keyCode); this.term.write(key) if (ev.keyCode == 13) { this.terminalSocket.send(this.order) console.log("里面的order",this.order) }else if(ev.keyCode == 8){ this.order = this.order.substr(0,this.order.length-1)
this.term.write("\x1b[2K\r") this.term.write("$ "+this.order)
console.log("截取的字符串"+this.order) typeof this.order }else{ this.order += key console.log("外面的order",this.order)} }) }, } </script>
|
服务器端实现
使用paramiko与服务器建立交互
1.安装
pip install paramiko
2.利用paramiko
进行ssh
远程登录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect( hostname="192.168.184.144",port=22,username='xterm',password='123123' )
stdin, stdout, stderr = client.exec_command("ls")
print(stdout.read().decode('utf-8'))
|
3.结合websocket
实现实时操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from channels.generic.websocket import WebsocketConsumer import paramiko class WebSSHService(WebsocketConsumer):
def connect(self): self.accept() self.sh = paramiko.SSHClient() self.sh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.sh.connect("192.168.184.144", username="root", password="beijing2018") print("连接成功")
def receive(self, text_data=None, bytes_data=None): print(str(text_data)) print(type(text_data))
stdin, stdout, stderr = self.sh.exec_command(text_data)
right_info = stdout.read() err_info = stderr.read() print(right_info)
if right_info: new_data = right_info.decode("utf-8").replace("\n","\r\n") print(new_data) self.send(new_data) elif err_info: new_data = err_info.decode("utf-8").replace("\n", "\r\n") print(new_data) self.send(new_data) else: print(self.send("命令执行成功"))
def disconnect(self, code): print(f'sorry{self},你被女朋友抛弃了')
|