# Analyze Data packet with PythonScript with Lua Rules

**URL:** <https://forum.suricata.io/t/analyze-data-packet-with-pythonscript-with-lua-rules/3787>\
**Category:** Help\
**Tags:** ips, rules, suricata, lua\
**Created:** [August 4, 2023, 12:34pm UTC](https://forum.suricata.io/t/analyze-data-packet-with-pythonscript-with-lua-rules/3787 "2023-08-04T12:34:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahim](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@rahim](https://forum.suricata.io/u/rahim)\
**Post date:** [August 4, 2023, 12:34pm UTC](https://forum.suricata.io/t/analyze-data-packet-with-pythonscript-with-lua-rules/3787/1 "2023-08-04T12:34:45Z")

</div>

Hello, I am new in suricata IDPS(and lua).

Suricata Build Info

```auto
This is Suricata version 7.0.0 RELEASE
Features: NFQ PCAP_SET_BUFF AF_PACKET HAVE_PACKET_FANOUT LIBCAP_NG LIBNET1.1 HAVE_HTP_URI_NORMALIZE_HOOK PCRE_JIT HAVE_NSS HTTP2_DECOMPRESSION HAVE_LUA HAVE_LUAJIT HAVE_LIBJANSSON TLS TLS_C11 MAGIC RUST 
SIMD support: none
Atomic intrinsics: 1 2 4 8 byte(s)
64-bits, Little-endian architecture
GCC version 11.3.0, C version 201112
compiled with _FORTIFY_SOURCE=2
L1 cache line size (CLS)=64
thread local storage method: _Thread_local
compiled with LibHTP v0.5.45, linked against LibHTP v0.5.45

Suricata Configuration:
  AF_PACKET support: yes
  AF_XDP support: no
  DPDK support: no
  eBPF support: no
  XDP support: no
  PF_RING support: no
  NFQueue support: yes
  NFLOG support: no
  IPFW support: no
  Netmap support: no 
  DAG enabled: no
  Napatech enabled: no
  WinDivert enabled: no

  Unix socket enabled: yes
  Detection enabled: yes

  Libmagic support: yes
  libjansson support: yes
  hiredis support: yes
  hiredis async with libevent: yes
  PCRE jit: yes
  LUA support: yes, through luajit
  libluajit: yes
  GeoIP2 support: yes
  Non-bundled htp: yes
  Hyperscan support: yes
  Libnet support: yes
  liblz4 support: yes
  Landlock support: yes

  Rust support: yes
  Rust strict mode: no
  Rust compiler path: /usr/bin/rustc
  Rust compiler version: rustc 1.66.1 (90743e729 2023-01-10) (built from a source tarball)
  Cargo path: /usr/bin/cargo
  Cargo version: cargo 1.66.1

  Python support: yes
  Python path: /usr/bin/python3
  Install suricatactl: yes
  Install suricatasc: yes
  Install suricata-update: yes

  Profiling enabled: no
  Profiling locks enabled: no
  Profiling rules enabled: no

  Plugin support (experimental): yes
  DPDK Bond PMD: no

Development settings:
  Coccinelle / spatch: no
  Unit tests enabled: no
  Debug output enabled: no
  Debug validation enabled: no
  Fuzz targets enabled: no

Generic build parameters:
  Installation prefix: /usr
  Configuration directory: /etc/suricata/
  Log directory: /var/log/suricata/

  --prefix /usr
  --sysconfdir /etc
  --localstatedir /var
  --datarootdir /usr/share

  Host: x86_64-pc-linux-gnu
  Compiler: gcc (exec name) / g++ (real)
  GCC Protect enabled: yes
  GCC march native enabled: no
  GCC Profile enabled: no
  Position Independent Executable enabled: yes
  CFLAGS -g -O2 -ffile-prefix-map=/build/suricata-L8uOzB/suricata-7.0.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -std=c11 -I${srcdir}/../rust/gen -I${srcdir}/../rust/dist
  PCAP_CFLAGS -I/usr/include
  SECCFLAGS -fstack-protector -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security

```

[https://docs.suricata.io/en/latest/rules/lua-detection.html](https://docs.suricata.io/en/latest/rules/lua-detection.html)  
[https://docs.suricata.io/en/latest/lua/lua-functions.html](https://docs.suricata.io/en/latest/lua/lua-functions.html)

I am trying to some kind of hook to communicate with my python script. In my python script, it will return 1 after analyzing data packet if data packet needs to drop. 0 Otherwise.

So, I have created a lua file in /etc/suricata/rules/py\_hook.lua with below content:

```auto
function init (args)
    local needs = {}
    needs["type"] = "packet"
    return needs
end

function match(args)
    local python_script = "python3 /home/user/nidps/analyze_packet.py"
    local output = os.execute(python_script .. " " .. args["packet"].tostring())
    if output == "1" then
      return 1
    end
    return 0
end

```

To run this rules, I am running Suricata with following command “suricata -i eth0 -s /etc/suricata/rules/py\_hook.lua”

But suricata will saying there is some error in my lua file.

Please help me to solve this issue

Thanks in advanced

---

<div class="post-metadata">

**Author:** ![vjulien](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/vjulien/32/4_2.png) [@vjulien](https://forum.suricata.io/u/vjulien)\
**Post date:** [August 5, 2023, 4:07pm UTC](https://forum.suricata.io/t/analyze-data-packet-with-pythonscript-with-lua-rules/3787/2 "2023-08-05T16:07:28Z")

</div>

Please share the actual error.

Also keep in mind that performance will be awful. Using Lua has quite a performance impact, but calling out to python from Lua will be much worse.
