Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform: luaxform transform script #12251

Closed
wants to merge 7 commits into from
Closed

Conversation

jlucovsky
Copy link
Contributor

Continuation of #12247

Add the luaxform transform that uses a Lua script to receive and transform a sticky buffer.

Lua transforms receive arguments

  • (always): length of input buffer and input buffer
  • (always): number of additional arguments (may be 0)
  • (when present): argument array (bounded by argument count)

Arguments are passed to the Lua script as they appear in the rule:

 luaxform:./lua/lua-transform.lua, bytes 4, offset 13, hash sha256;

When the transform is invoked, 3 additional arguments are passed (arg count will be 3)

  • bytes 4
  • offset 13
  • hash sha256

The Lua script is responsible for parsing and using the arguments and returning a tuple: output-buffer, output-buffer-byte-count

I used this Lua script:

function init (args)
  -- Optional, no return value
end

function transform(input_len, input, argc, args)
    SCLogNotice("Input length: " .. input_len .. " arg-count: " .. argc)
    for i = 1, argc do
        print("Argument " .. i .. ":", args[i])
    end
    return string.upper(input), input_len
end

return 0

Example rule using the Lua script:

alert http any any -> any any (msg:"Lua Xform example"; flow:established;  file.data; luaxform:./lua/lua-transform.lua; content: "abc"; sid: 1;)

Link to ticket: https://redmine.openinfosecfoundation.org/issues/2290

Describe changes:

  • Luaxform transform changes

Updates

  • Documentation section headers fixup

Provide values to any of the below to override the defaults.

  • To use an LibHTP, Suricata-Verify or Suricata-Update pull request,
    link to the pull request in the respective _BRANCH variable.
  • Leave unused overrides blank or remove.

SV_REPO=
SV_BRANCH=OISF/suricata-verify#2090
SU_REPO=
SU_BRANCH=
LIBHTP_REPO=
LIBHTP_BRANCH=

This commit makes the detection engine thread context available for
transforms to use. The Lua transform requires this value.

Issue: 2290
Issue: 2290

This commit extends the hash table logic with an alternate free function
that provides the detection engine context.

Users that wish to use the next functionality must use the
HashListTableInitWithCtx function when initializing the hash table.
Using this interface will result in the hash table "free with context"
function (new) being used instead.
Issue: 2290

Defer freeing the keyword hash table until the engine context has
been freed. This eliminates a double-free from occurring.

For the unittests ONLY, clear the keyword_hash to prevent a double
free attempt.
Issue: 2290

This commit adds the source files for the new transform -- luaxform.
git grep -A 1 -w InspectionBufferSetup shows numbers cases of the pattern:
    - InspectionBufferSetup
    - InspectionBufferApplyTransforms

Refactor the implementations of those functions into
InspectionBufferSetupAndApplyTransforms to reduce function call count.

Issuer: 2290
Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 87.64045% with 55 lines in your changes missing coverage. Please review.

Project coverage is 83.20%. Comparing base (a9b36d8) to head (456bf20).
Report is 108 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12251      +/-   ##
==========================================
+ Coverage   83.18%   83.20%   +0.01%     
==========================================
  Files         912      914       +2     
  Lines      257174   257389     +215     
==========================================
+ Hits       213930   214149     +219     
+ Misses      43244    43240       -4     
Flag Coverage Δ
fuzzcorpus 60.99% <47.11%> (-0.02%) ⬇️
livemode 19.44% <13.16%> (+0.03%) ⬆️
pcap 44.37% <30.25%> (-0.03%) ⬇️
suricata-verify 62.79% <85.21%> (+<0.01%) ⬆️
unittests 59.16% <26.51%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

Information: QA ran without warnings.

Pipeline 23733

@victorjulien
Copy link
Member

Testing this gives me some crashes / asan errrors:

./src/suricata -c suricata.yaml -l tmp/ -S lua-xform-dataset.rules -r ~/sync/pcap/sandnet.pcap --set security.lua.allow-rules=true --set default-rule-path=. --runmode=single
Notice: suricata: This is Suricata version 8.0.0-dev (d06bf79848 2025-01-14) running in USER mode [LogVersion:suricata.c:1150]
Warning: suricata: "security.limit-noproc" (setrlimit()) not set when using address sanitizer [SuricataPostInit:suricata.c:3006]
Notice: threads: Threads created -> W: 1 FM: 1 FR: 1   Engine started. [TmThreadWaitOnThreadRunning:tm-threads.c:1900]
AddressSanitizer:DEADLYSIGNAL
=================================================================
==2925186==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x6193b7781980 bp 0x532002ff2fb0 sp 0x72eb9fbfe330 T1)
==2925186==The signal is caused by a READ memory access.
==2925186==Hint: this fault was caused by a dereference of a high value address (see register values below).  Disassemble the provided pc to learn which register was used.
    #0 0x6193b7781980 in luaV_execute (/home/victor/sync/devel/eidps/src/suricata+0x962980) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a)
    #1 0x6193b777336c in luaD_callnoyield (/home/victor/sync/devel/eidps/src/suricata+0x95436c) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a)
    #2 0x6193b7772222 in luaD_rawrunprotected (/home/victor/sync/devel/eidps/src/suricata+0x953222) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a)
    #3 0x6193b7773753 in luaD_pcall (/home/victor/sync/devel/eidps/src/suricata+0x954753) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a)
    #4 0x6193b776fc24 in lua_pcallk (/home/victor/sync/devel/eidps/src/suricata+0x950c24) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a)
    #5 0x6193b72c45c1 in TransformLuaxform /home/victor/sync/devel/eidps/src/detect-transform-luaxform.c:347
    #6 0x6193b71b4632 in InspectionBufferApplyTransformsInternal /home/victor/sync/devel/eidps/src/detect-engine.c:1572
    #7 0x6193b71b4ce6 in InspectionBufferSetupAndApplyTransforms /home/victor/sync/devel/eidps/src/detect-engine.c:1652
    #8 0x6193b7253949 in GetData /home/victor/sync/devel/eidps/src/detect-http-ua.c:177
    #9 0x6193b71fa91d in PrefilterMpm /home/victor/sync/devel/eidps/src/detect-engine-prefilter.c:727
    #10 0x6193b71f66c1 in DetectRunPrefilterTx /home/victor/sync/devel/eidps/src/detect-engine-prefilter.c:125
    #11 0x6193b750a6fa in DetectRunTx /home/victor/sync/devel/eidps/src/detect.c:1509
    #12 0x6193b750358b in DetectRun /home/victor/sync/devel/eidps/src/detect.c:174
    #13 0x6193b750cb2f in DetectFlow /home/victor/sync/devel/eidps/src/detect.c:1877
    #14 0x6193b750d2f6 in Detect /home/victor/sync/devel/eidps/src/detect.c:1948
    #15 0x6193b72fa5df in FlowWorker /home/victor/sync/devel/eidps/src/flow-worker.c:640
    #16 0x6193b708128f in TmThreadsSlotVarRun /home/victor/sync/devel/eidps/src/tm-threads.c:137
    #17 0x6193b73b37a1 in TmThreadsSlotProcessPkt /home/victor/sync/devel/eidps/src/tm-threads.h:199
    #18 0x6193b73b43a2 in PcapFileCallbackLoop /home/victor/sync/devel/eidps/src/source-pcap-file-helper.c:108
    #19 0x72ebaa58163e  (/lib/x86_64-linux-gnu/libpcap.so.0.8+0x2d63e) (BuildId: d0c6c787d35246d7107d600c893454c1fcbaf262)
    #20 0x6193b73b4766 in PcapFileDispatch /home/victor/sync/devel/eidps/src/source-pcap-file-helper.c:153
    #21 0x6193b73ae3c2 in ReceivePcapFileLoop /home/victor/sync/devel/eidps/src/source-pcap-file.c:205
    #22 0x6193b7082331 in TmThreadsSlotPktAcqLoop /home/victor/sync/devel/eidps/src/tm-threads.c:310
    #23 0x72ebaa85ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
    #24 0x72eba969ca93 in start_thread nptl/pthread_create.c:447
    #25 0x72eba9729c3b in clone3 ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/home/victor/sync/devel/eidps/src/suricata+0x962980) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a) in luaV_execute
Thread T1 (W#01) created by T0 (Suricata-Main) here:
    #0 0x72ebaa8f51f9 in pthread_create ../../../../src/libsanitizer/asan/asan_interceptors.cpp:245
    #1 0x6193b7088ee3 in TmThreadSpawn /home/victor/sync/devel/eidps/src/tm-threads.c:1684
    #2 0x6193b7565a97 in RunModeFilePcapSingle /home/victor/sync/devel/eidps/src/runmode-pcap-file.c:97
    #3 0x6193b737a3b0 in RunModeDispatch /home/victor/sync/devel/eidps/src/runmodes.c:423
    #4 0x6193b707c89e in SuricataInit /home/victor/sync/devel/eidps/src/suricata.c:2970
    #5 0x6193b706d4bd in main /home/victor/sync/devel/eidps/src/main.c:54
    #6 0x72eba962a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #7 0x72eba962a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #8 0x6193b706d324 in _start (/home/victor/sync/devel/eidps/src/suricata+0x24e324) (BuildId: 2a4bd8735d91eaeb1ebd5680605c1fc4bb92442a)

==2925186==ABORTING

rule

alert http1 any any -> any any (http.user_agent; luaxform:lua-xform-ua-srcip.lua; content:"xyz123"; sid:2;)

script

function init (args)
  -- Optional, no return value
end

function transform(input_len, input, argc, args)
    ipver, srcip, dstip, proto, sp, dp = SCFlowTuple()
    str = srcip .. ">:<" .. dp .. ">:<" .. input
    return str
end

return 0

Invoked with

./src/suricata -c suricata.yaml -l tmp/ -S lua-xform-dataset.rules -r ~/sync/pcap/sandnet.pcap --set security.lua.allow-rules=true --set default-rule-path=. --runmode=single

}

error:
lua_pop(tlua->luastate, return_value_count); // Pop the result string/length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the error path the state is not cleared, this is what my asan issue is. State keeps growing with each error until it fails to grow further

const uint8_t *input = buffer->inspect;
const uint32_t input_len = buffer->inspect_len;
/* Lua script args are: buffer length, buffer, arg count, args */
LuaPushInteger(tlua->luastate, (lua_Integer)input_len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed. In the script we can use

input_len = #input

to get the length

/* Lua script args are: buffer length, buffer, arg count, args */
LuaPushInteger(tlua->luastate, (lua_Integer)input_len);
LuaPushStringBuffer(tlua->luastate, input, (size_t)input_len);
LuaPushInteger(tlua->luastate, (lua_Integer)lua->arg_count);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't needed, in the script #args can be used to get the same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

    SCLogNotice("args " .. #args)
    for i = 1, #args do
        SCLogNotice("Argument " .. i .. ":" .. args[i])
    end

* Add provided arguments for lua script (these are optionally
* provided by the rule writer).
*
* Start at offset 1 (arg[0] is the lua script filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not appear to be working. arg[0] is invalid, args[0] can't be printed. attempt to concatenate a nil value (field 'integer index')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lua->args[0] contains the filename; the filename is not passed to the Lua script.

@victorjulien
Copy link
Member

Can you split out the general refactor commits so we can get these in already?

@victorjulien
Copy link
Member

"transform: Add luaxform files" - need a better commit subject. Adding the files isn't the relevant info, plus it is redundant with what the diff already shows.

@victorjulien victorjulien mentioned this pull request Jan 18, 2025
@victorjulien
Copy link
Member

Replaced by #12425

@jlucovsky
Copy link
Contributor Author

Can you split out the general refactor commits so we can get these in already?

#12426 created with general commits from #12425

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants