# Using packet buffer in Lua

**URL:** <https://forum.suricata.io/t/using-packet-buffer-in-lua/1521>\
**Category:** Rules\
**Created:** [July 15, 2021, 6:19pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521 "2021-07-15T18:19:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_Wills](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/joseph_wills/32/613_2.png) [@Joseph\_Wills](https://forum.suricata.io/u/Joseph_Wills)\
**Post date:** [July 15, 2021, 6:19pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/1 "2021-07-15T18:19:01Z")

</div>

I’m writing a lua script for a rule (not output). Is there a way to get the full packet from lua? If I try something, for example, like this:

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

function match(args)
    p = args["packet"]
    SCLogNotice(p)
    return 1
end

```

Nothing is output to suricata.log. Is there a way to just get the whole packet in hexadecimal characters? What am I missing?

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 15, 2021, 6:38pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/2 "2021-07-15T18:38:06Z")

</div>

Hi Joseph,

From the documentation, I’m not sure there’s something just like that.

You could use `SCPacketPayload` to get access to the packet contents, and `SCPacketTuple` to access IP version, and source and destination IP and ports.

Another thing that may be causing issues is the way you’re retrieving the packet in your `init` function. According to documentation, and from my own experience, it should be: `needs["type"] = "packet"`.

Not sure if that’s another possible way, but maybe that could also help in moving forward with your intent!

Source of what I’ve mentioned:  
[https://suricata.readthedocs.io/en/suricata-6.0.3/lua/lua-functions.html#packet](https://suricata.readthedocs.io/en/suricata-6.0.3/lua/lua-functions.html#packet)

---

<div class="post-metadata">

**Author:** ![Joseph\_Wills](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/joseph_wills/32/613_2.png) [@Joseph\_Wills](https://forum.suricata.io/u/Joseph_Wills)\
**Post date:** [July 15, 2021, 6:39pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/3 "2021-07-15T18:39:16Z")

</div>

well basically the hope was that I could get the IP and the http URI at the same time with the packet, which isn’t really easy without those things.

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 15, 2021, 6:43pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/4 "2021-07-15T18:43:20Z")

</div>

You could retrieve those data and format a string with the information you want with `string.format() ` Would that help?

---

<div class="post-metadata">

**Author:** ![Joseph\_Wills](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/joseph_wills/32/613_2.png) [@Joseph\_Wills](https://forum.suricata.io/u/Joseph_Wills)\
**Post date:** [July 15, 2021, 6:44pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/5 "2021-07-15T18:44:05Z")

</div>

Well as far as I know you _can’t_ retrieve that data at the same time because they’re in different buffers. maybe that’s a better question – can you get the source ip and http uri at the same time?

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 15, 2021, 6:47pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/6 "2021-07-15T18:47:34Z")

</div>

Maybe this script from one of our Suricata-Verify tests for lua output could help in understanding the possibilities on that sense?

> <https://github.com/OISF/suricata-verify/blob/master/tests/lua-output-http/http.lua>

Edit: sorry, had linked to my personal repo. fixed.

---

<div class="post-metadata">

**Author:** ![Joseph\_Wills](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/joseph_wills/32/613_2.png) [@Joseph\_Wills](https://forum.suricata.io/u/Joseph_Wills)\
**Post date:** [July 15, 2021, 6:51pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/7 "2021-07-15T18:51:14Z")

</div>

Thank you. But don;t you have to define a buffer with writing lua scripts for detection? [6.34. Lua Scripting — Suricata 6.0.0 documentation](https://suricata.readthedocs.io/en/suricata-6.0.0/rules/rule-lua-scripting.html)

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 15, 2021, 7:03pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/8 "2021-07-15T19:03:33Z")

</div>

Hm, I think there may be some more freedom as to how you write things within the `match` function in the lua script.

Check this other Suricata-verify test:

> **[OISF/suricata-verify](https://github.com/OISF/suricata-verify/tree/master/tests/dns-lua-rules)**
>
> master/tests/dns-lua-rules

As for a buffer, if you want that, I could be wrong, but I think you could build your own, using `string.format()`. Then you format it the way it would show up in the packet. S-V test for `DNS` output in lua has an example of formatting a string:

> <https://github.com/OISF/suricata-verify/blob/master/tests/lua-output-dns/test.lua>

---

<div class="post-metadata">

**Author:** ![Joseph\_Wills](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/joseph_wills/32/613_2.png) [@Joseph\_Wills](https://forum.suricata.io/u/Joseph_Wills)\
**Post date:** [July 15, 2021, 8:00pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/9 "2021-07-15T20:00:45Z")

</div>

I’m not really sure what you’re getting at. The problem isn’t string formatting, it’s that you simply can’t get the source ip (from the packet buffer) and the http uri (from the http uri buffer) in the same lua script.

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 15, 2021, 9:04pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/10 "2021-07-15T21:04:50Z")

</div>

I’m sorry, I’m just trying to help. I thought I saw that done in that http output suricata-verify test by means of the `SCFlowTuple()`, so I assumed it is possible. But I don’t have more knowledge to help you further, right now.

Edit: It actually does work, I had something wrong in the previous version of my script.

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 15, 2021, 9:23pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/11 "2021-07-15T21:23:29Z")

</div>

Would _something_ like this work for you?

```auto
{ Packet n. 0
10/06/2015-15:16:43.137833
[**]
Packet info:
Source IP: 10.16.1.11 source port: 59733 Destination IP: 104.131.202.103 destination port 80 http uri /base64-hello-world.txt}

```

I tinkered with the script below and that was the output. Something else is still needed for it to log more packets, but could be a start?

```auto
name = "test_packet_info.log"

function init (args)
    local needs = {}
-- needs["type"] = "packet"
    needs["protocol"] = "http"
    return needs
end

function setup(args)
    filename = SCLogPath() .. "/" .. name
    file = assert(io.open(filename, "a"))
    SCLogInfo("Lua test for packet and http info filename " .. filename)
    packet = 0 
end

function log(args)
    timestring = SCPacketTimeString()
    ipver, srcip, dstip, proto, sp, dp = SCPacketTuple()

    http_uri = HttpGetRequestUriRaw()
    if http_uri == nil then
        http_uri = "<unknown>"
    end 
    http_uri = string.gsub(http_uri, "%c", ".")

    file:write ("{ Packet n. " .. packet .. "\n " .. timestring .. "\n[**]\nPacket info: \nSource IP: " .. srcip .. " source port: " .. sp .. " Destination IP: " .. dstip .. " destination port " .. dp .. " http uri " .. http_uri .. "}\n")
    packet = packet + 1 
end

function deinit(args)
    SCLogInfo ("Test finished");
    file:close(file)
end

```

Anyway, good luck!

---

<div class="post-metadata">

**Author:** ![Joseph\_Wills](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/joseph_wills/32/613_2.png) [@Joseph\_Wills](https://forum.suricata.io/u/Joseph_Wills)\
**Post date:** [July 15, 2021, 9:52pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/12 "2021-07-15T21:52:05Z")

</div>

Sorry if I came off as rude, that is not my intention.

Your code does have the right information, unfortunately I’m working with suricata lua detection and not logging. I want to use the lua script I’m writing to trigger a rule, and you can’t use needs[“protocol”] = “http”, you have to use their buffers.

thanks

---

<div class="post-metadata">

**Author:** ![jufajardini](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/jufajardini/32/896_2.png) [@jufajardini](https://forum.suricata.io/u/jufajardini)\
**Post date:** [July 16, 2021, 4:11pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/13 "2021-07-16T16:11:57Z")

</div>

I’m sorry, too, for not being able to help, and for not understanding your request properly. Good luck, and if you come up with a solution, do share! 🙂

---

<div class="post-metadata">

**Author:** ![koraybilgi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/koraybilgi/32/1910_2.png) [@koraybilgi](https://forum.suricata.io/u/koraybilgi)\
**Post date:** [April 4, 2023, 6:41pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/14 "2023-04-04T18:41:37Z")

</div>

Hi, @Joseph_Wills. I believe I’m having a similar problem to yours. Did you manage to come by a solution?

---

<div class="post-metadata">

**Author:** ![koraybilgi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.suricata.io/koraybilgi/32/1910_2.png) [@koraybilgi](https://forum.suricata.io/u/koraybilgi)\
**Post date:** [April 4, 2023, 10:58pm UTC](https://forum.suricata.io/t/using-packet-buffer-in-lua/1521/15 "2023-04-04T22:58:53Z")

</div>

Turns out, in my case **`SCFlowTuple`** should be used instead of **`SCPacketTuple`**.

In case anyone ends up at this post, this is how I managed to get the source ip:

```lua
function init (args)
    local needs = {}
    needs["tls"] = tostring(true)
    return needs
end

function match(args)
    ipver, srcip, dstip, proto, sp, dp = SCFlowTuple()
    SCLogWarning(srcip)
    return 0
end

```
