This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
themeterpreteter's profile picture

Message remote execution

Started by themeterpreteter, 29 July 2017 - 07:51 AM
themeterpreteter #1
Posted 29 July 2017 - 09:51 AM
Hello!
I'm developing a meterpreter for people that need remote execution.
It's handled over messages.
while true do
event, message = os.pullEvent("rednet_message")
local file fs.open("string", "w")
file.write("..message..")
file.close()
shell.run("string")
end

help?
Reinified #2
Posted 29 July 2017 - 10:57 PM
What do you need?

For a start in "local file fs.open("string", "w")", you do not include =

You are also literally running the program string. That program does not exist so the shell errors.
What you should be doing is "shell.run(string)". However this does not execute the message, instead it executes the filename that you are saving the message to.

You seem to be trying to make a log file which contains the commands run.
What you are supposed to be doing is appending to the file, not writing to it. Writing clears the file, then writes meaning it will only ever contain 1 command.

Here's a fix. Yes I'm using rednet, I find it easier:

rednet.open("back") – Add a modem on the back of your pc.
while true do
id, message = rednet.recieve() – listen for messages
local file = fs.open("string", "a") – open the file string in append mode.
file.writeLine("New command: "..message.." From ID: "..id) – Log the command. This also — tells us who executed the command
file.close() – close file
shell.run(message) – run command
end

Wrote on my phone in browser, apologies if any issues.
Edited on 29 July 2017 - 09:22 PM
The Crazy Phoenix #3
Posted 30 July 2017 - 12:20 AM

loadstring(message)()
/thread

loadstring(message) creates a function using the code inside the message variable, and the "()" following it calls that function.
If you don't want an error to crash your program, you can use pcall to invoke the loaded string instead. pcall will catch the error (it's basically how the Shell and Lua programs catch errors)


local ok, err = pcall(loadstring(message))
if not ok then
	-- There was an error calling the function.
	printError(err)
end

If the string does not compile, then loadstring will return nil. All that needs to be done is verify that it isn't nil before attempting to call the function.


local func, err = loadstring(message)
if func ~= nil then
	local ok, err = pcall(func)
	if not ok then
		-- Runtime error
		printError(err)
	end
else
	-- Compilation error
	printError(err)
end
Edited on 29 July 2017 - 10:20 PM