-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb2ssh2.py
More file actions
307 lines (291 loc) · 15.4 KB
/
Copy pathweb2ssh2.py
File metadata and controls
307 lines (291 loc) · 15.4 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import subprocess
import logging
from flask import Flask, request, render_template_string, Response, redirect
import pwnagotchi.plugins as plugins
from functools import wraps
class web2ssh2(plugins.Plugin):
__author__ = 'WPA2'
__editor__ = 'avipars'
__version__ = '0.1.3.4'
__license__ = 'GPL3'
__description__ = 'A Plugin to issue SSH commands via a browser with some added presets'
__gitHub__ = "https://github.com/wpa-2/Pwnagotchi-Plugins/blob/main/web2ssh.py"
def __init__(self):
super().__init__()
logging.debug("web2ssh2 created")
self.app = Flask(__name__)
self.username = ""
self.password = ""
self.port = 8083
def on_loaded(self):
"""Called when the plugin is loaded."""
logging.info("web2ssh2 loaded")
self.username = self.options.get("username", "changeme")
self.password = self.options.get("password", "changeme")
self.port = int(self.options.get("port", 8083))
logging.info(f"web2ssh2 configured: username={self.username}, port={self.port}")
# Set up Flask routes and start the server
self.app.before_request(self.requires_auth) # Attach auth check to all routes
self._register_routes()
self.app.run(host='::', port=self.options["port"])
def on_webhook(self, path, requestobj):
# open the url to redirect to webpage
"""Handle webhook requests by redirecting to the web2ssh interface."""
return redirect(f"http://{requestobj.host.split(':')[0]}:{self.port}/", code=302)
def _register_routes(self):
"""Register Flask routes."""
@self.app.route('/')
def index():
"""Home page for SSH command input."""
return render_template_string("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WEB2SSH2 Command Executor</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
background-color: #f4f4f9;
}
.container {
text-align: center;
background: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 400px;
margin-top: 20px;
}
h1 {
font-size: 1.5rem;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
}
input[type="text"] {
font-size: 1rem;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
font-size: 1rem;
padding: 10px;
color: #fff;
background-color: #007BFF;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
.shortcuts {
margin-top: 20px;
}
.shortcuts button {
font-size: 1rem;
margin: 5px;
padding: 10px;
color: #fff;
background-color: #28a745;
border: none;
border-radius: 4px;
cursor: pointer;
}
.shortcuts button:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class="container">
<h1>WEB2SSH2 Command Executor</h1>
<form action="/execute" method="post">
<input type="text" id="commandInput" name="command" placeholder="Enter command" required>
<input type="submit" value="Execute">
</form>
<div class="shortcuts">
<h2>Command Shortcuts</h2>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo shutdown -h now">
<button type="submit">Shutdown</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo reboot">
<button type="submit">Reboot</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="ping -c 4 8.8.8.8">
<button type="submit">Ping IP</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="ping -c 4 google.com">
<button type="submit">Ping DNS</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo pwngrid --inbox">
<button type="submit">Inbox</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo killall -USR1 pwnagotchi">
<button type="submit">Pwnkill</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo systemctl restart pwnagotchi">
<button type="submit">Restart pwn service</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="ls /usr/local/share/pwnagotchi/custom-plugins">
<button type="submit">List Custom Plugins Old</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="ls /etc/pwnagotchi/custom-plugins">
<button type="submit">List Custom Plugins New</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo pwnagotchi plugins update">
<button type="submit">Update Plugins</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo pwnagotchi plugins upgrade">
<button type="submit">Upgrade Plugins</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="systemctl status pwnagotchi --no-pager">
<button type="submit">Status</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="dmesg | tail -50">
<button type="submit">dmesg</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="ethtool -i wlan0mon">
<button type="submit">ethtool</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="if ip link show wlan0mon >/dev/null 2>&1; then ethtool -i wlan0mon; elif ip link show wlan0 >/dev/null 2>&1; then ethtool -i wlan0; else echo 'No wlan0mon or wlan0 interface found'; fi">
<button type="submit">ethtool2</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="cat /etc/pwnagotchi/config.toml">
<button type="submit">View Config</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="lsusb && iwconfig">
<button type="submit">Check WiFi</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="ifconfig && iw dev">
<button type="submit">wireless check </button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo airmon-ng start wlan0">
<button type="submit">Enable Monitor Mode</button>
</form>
<form action="/execute" method="post" style="display: inline;">
<input type="hidden" name="command" value="sudo airmon-ng stop wlan0mon">
<button type="submit">Disable Monitor Mode</button>
</form>
</div>
</div>
</body>
</html>
""")
@self.app.route('/execute', methods=['POST'])
def execute_command():
"""Execute SSH command and return output."""
command = request.form['command']
output = self.ssh_execute_command(command)
return render_template_string("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Command Output</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f9;
}
.output-container {
max-width: 800px;
margin: 0 auto;
background: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 1.5rem;
margin-bottom: 20px;
}
pre {
text-align: left;
background: #f8f9fa;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
font-size: 0.9rem;
}
a {
display: inline-block;
margin-top: 15px;
font-size: 1rem;
text-decoration: none;
color: #007BFF;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="output-container">
<h1>Command Output</h1>
<pre>{{ output }}</pre>
<a href="/">Back</a>
</div>
</body>
</html>
""", output=output)
def ssh_execute_command(self, command):
"""Executes the SSH command on the local device."""
try:
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
return result.decode('utf-8')
except subprocess.CalledProcessError as e:
return f"Error executing command: {e.output.decode('utf-8')}"
def check_auth(self, username, password):
"""Check if username and password match."""
return (username == self.username and password == self.password)
def requires_auth(self):
"""Enforce basic authentication."""
auth = request.authorization
if not auth or not self.check_auth(auth.username, auth.password):
return self._unauthorized_response()
return None
def _unauthorized_response(self):
"""Generate a 401 Unauthorized response with the WWW-Authenticate header."""
response = Response(
'Unauthorized access. Please provide valid credentials.',
status=401
)
response.headers['WWW-Authenticate'] = 'Basic realm="web2ssh2"'
return response
def on_unload(self, ui):
"""Called when the plugin is unloaded."""
logging.info("web2ssh2 unloaded")