suricata
util-lua-ja3.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 
19 /**
20  * \file
21  *
22  * \author Mats Klepsland <mats.klepsland@gmail.com>
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-print.h"
37 #include "util-unittest.h"
38 
39 #include "util-debug.h"
40 
41 #include "output.h"
42 #include "app-layer.h"
43 #include "app-layer-parser.h"
44 #include "app-layer-ssl.h"
45 #include "util-privs.h"
46 #include "util-buffer.h"
47 #include "util-proto-name.h"
48 #include "util-logopenfile.h"
49 #include "util-time.h"
50 
51 #ifdef HAVE_LUA
52 
53 #include <lua.h>
54 #include <lualib.h>
55 #include <lauxlib.h>
56 
57 #include "util-lua.h"
58 #include "util-lua-common.h"
59 #include "util-lua-ja3.h"
60 
61 static int Ja3GetHash(lua_State *luastate)
62 {
63  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
64  return LuaCallbackError(luastate, "error: protocol is not tls");
65 
66  Flow *f = LuaStateGetFlow(luastate);
67  if (f == NULL)
68  return LuaCallbackError(luastate, "internal error: no flow");
69 
70  void *state = FlowGetAppState(f);
71  if (state == NULL)
72  return LuaCallbackError(luastate, "error: no app layer state");
73 
74  SSLState *ssl_state = (SSLState *)state;
75 
76  if (ssl_state->ja3_hash == NULL)
77  return LuaCallbackError(luastate, "error: no JA3 hash");
78 
79  return LuaPushStringBuffer(luastate, (uint8_t *)ssl_state->ja3_hash,
80  strlen(ssl_state->ja3_hash));
81 }
82 
83 static int Ja3GetString(lua_State *luastate)
84 {
85  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
86  return LuaCallbackError(luastate, "error: protocol is not tls");
87 
88  Flow *f = LuaStateGetFlow(luastate);
89  if (f == NULL)
90  return LuaCallbackError(luastate, "internal error: no flow");
91 
92  void *state = FlowGetAppState(f);
93  if (state == NULL)
94  return LuaCallbackError(luastate, "error: no app layer state");
95 
96  SSLState *ssl_state = (SSLState *)state;
97 
98  if (ssl_state->ja3_str == NULL || ssl_state->ja3_str->data == NULL)
99  return LuaCallbackError(luastate, "error: no JA3 str");
100 
101  return LuaPushStringBuffer(luastate, (uint8_t *)ssl_state->ja3_str->data,
102  ssl_state->ja3_str->used);
103 }
104 
105 /** *\brief Register JA3 Lua extensions */
106 int LuaRegisterJa3Functions(lua_State *luastate)
107 {
108  lua_pushcfunction(luastate, Ja3GetHash);
109  lua_setglobal(luastate, "Ja3GetHash");
110 
111  lua_pushcfunction(luastate, Ja3GetString);
112  lua_setglobal(luastate, "Ja3GetString");
113 
114  return 0;
115 }
116 
117 #endif /* HAVE_LUA */
char * data
Definition: util-ja3.h:30
SSLv[2.0|3.[0|1|2|3]] state structure.
size_t used
Definition: util-ja3.h:32
JA3Buffer * ja3_str
char * ja3_hash
void * FlowGetAppState(const Flow *f)
Definition: flow.c:997
Flow data structure.
Definition: flow.h:327