Getting Started
Write your first custom listener handler in Lua
Custom listeners let GoNetSim simulate almost any TCP or UDP protocol, using small scripts written in Lua. When malware speaks a protocol that DNS or HTTP can’t cover, such as SMTP, IRC or a bespoke C2 channel, you can write a handler for it in a few lines.
The idea is simple. When a client connects, GoNetSim hands the connection to your script. Your script reads what the client sends, decides what to reply, and GoNetSim takes care of logging, timeouts, and saving what the client sent.
How It Works
Section titled “How It Works”A script is just a file that defines a handle function:
function handle(conn) -- your protocol logic hereendA few rules keep scripts predictable:
- Each client gets its own run of the script, so connections do not share state with each other.
- A script can’t touch the file system or run programs. It talks to the network through the provided
connobject, writes to the log withlog, and saves client data withcapture. - If a script fails mid-connection, that connection is closed and the error is logged, but the listener can still serve other clients.
Your First Handler
Section titled “Your First Handler”Let’s write a handler that pretends to be a simple greeting service. Save the following as greet.lua:
function handle(conn) conn:write("Welcome to GoNetSim\r\n")
while true do local line = conn:read_line() if not line then break end -- client closed the connection capture:write("greet", line) conn:write("You said: " .. line) endendEvery line the client sends is captured to the artifacts directory, then echoed back with a prefix.
Running It
Section titled “Running It”Inline
Section titled “Inline”The quickest way to run a script is an inline listener:
gonetsim run greet.lua@:8080gonetsim.exe run greet.lua@:8080The .lua file extension tells GoNetSim this is a Lua handler, so no configuration is needed at all. Inline listeners are perfect for quickly testing a handler before wiring it into your setup.
From Configuration
Section titled “From Configuration”To run the handler every time, add it to your config file as a custom listener:
[[listeners]]name = "greet"type = "tcp"listen = ":8080"handler = "lua:handlers/greet.lua"Script paths are relative to the config file, so keeping your handlers next to the config keeps things tidy. See the listeners reference for the full list of options.
Testing It
Section titled “Testing It”You can test the handler with netcat:
-
Start the listener in one terminal
Terminal window gonetsim run greet.lua@:8080 -
Connect from another terminal and type a message
Terminal window netcat localhost 8080 -
Type
helloand press enter, the server should reply withYou said: hello. Leave withCtrl+C. -
Check the GoNetSim terminal, it should show the message was logged and captured.
For faster iteration, the script command runs a handler over stdin/stdout with no port at all:
echo "hello" | gonetsim script greet.luaThis is the fastest way to try changes.
Next Steps
Section titled “Next Steps”- Read the full Lua API to see everything handlers can do
- Browse ready-made examples for common protocols like IRC & FTP
- See the listeners reference for configuration options like TLS & UDP
