suricata
util-lua-dns.c
Go to the documentation of this file.
1 /* Copyright (C) 2014 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 Eric Leblond <eric@regit.org>
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-dns-common.h"
43 #include "app-layer.h"
44 #include "app-layer-parser.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-dns.h"
60 
61 #include "rust-dns-dns-gen.h"
62 #include "rust-dns-lua-gen.h"
63 
64 static int DnsGetDnsRrname(lua_State *luastate)
65 {
66  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
67  return LuaCallbackError(luastate, "error: protocol not dns");
68  RSDNSTransaction *tx = LuaStateGetTX(luastate);
69  if (tx == NULL) {
70  return LuaCallbackError(luastate, "internal error: no tx");
71  }
72  return rs_dns_lua_get_rrname(luastate, tx);
73 }
74 
75 static int DnsGetTxid(lua_State *luastate)
76 {
77  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
78  return LuaCallbackError(luastate, "error: protocol not dns");
79  RSDNSTransaction *tx = LuaStateGetTX(luastate);
80  if (tx == NULL) {
81  return LuaCallbackError(luastate, "internal error: no tx");
82  }
83  rs_dns_lua_get_tx_id(luastate, tx);
84  return 1;
85 }
86 
87 static int DnsGetRcode(lua_State *luastate)
88 {
89  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
90  return LuaCallbackError(luastate, "error: protocol not dns");
91  uint16_t rcode = 0;
92  RSDNSTransaction *tx = LuaStateGetTX(luastate);
93  if (tx == NULL) {
94  return LuaCallbackError(luastate, "internal error: no tx");
95  }
96  uint16_t flags = rs_dns_tx_get_response_flags(tx);
97  rcode = flags & 0x000f;
98  if (rcode) {
99  char rcode_str[16] = "";
100  DNSCreateRcodeString(rcode, rcode_str, sizeof(rcode_str));
101  return LuaPushStringBuffer(luastate, (const uint8_t *)rcode_str, strlen(rcode_str));
102  } else {
103  return 0;
104  }
105 }
106 
107 static int DnsGetRecursionDesired(lua_State *luastate)
108 {
109  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
110  return LuaCallbackError(luastate, "error: protocol not dns");
111  RSDNSTransaction *tx = LuaStateGetTX(luastate);
112  if (tx == NULL) {
113  return LuaCallbackError(luastate, "internal error: no tx");
114  }
115  uint16_t flags = rs_dns_tx_get_response_flags(tx);
116  int recursion_desired = flags & 0x0080 ? 1 : 0;
117  lua_pushboolean(luastate, recursion_desired);
118  return 1;
119 }
120 
121 static int DnsGetQueryTable(lua_State *luastate)
122 {
123  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
124  return LuaCallbackError(luastate, "error: protocol not dns");
125  RSDNSTransaction *tx = LuaStateGetTX(luastate);
126  if (tx == NULL) {
127  return LuaCallbackError(luastate, "internal error: no tx");
128  }
129  return rs_dns_lua_get_query_table(luastate, tx);
130 }
131 
132 static int DnsGetAnswerTable(lua_State *luastate)
133 {
134  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
135  return LuaCallbackError(luastate, "error: protocol not dns");
136  RSDNSTransaction *tx = LuaStateGetTX(luastate);
137  return rs_dns_lua_get_answer_table(luastate, tx);
138 }
139 
140 static int DnsGetAuthorityTable(lua_State *luastate)
141 {
142  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
143  return LuaCallbackError(luastate, "error: protocol not dns");
144  RSDNSTransaction *tx = LuaStateGetTX(luastate);
145  return rs_dns_lua_get_authority_table(luastate, tx);
146 }
147 
148 /** \brief register http lua extensions in a luastate */
149 int LuaRegisterDnsFunctions(lua_State *luastate)
150 {
151  /* registration of the callbacks */
152  lua_pushcfunction(luastate, DnsGetDnsRrname);
153  lua_setglobal(luastate, "DnsGetDnsRrname");
154 
155  lua_pushcfunction(luastate, DnsGetQueryTable);
156  lua_setglobal(luastate, "DnsGetQueries");
157 
158  lua_pushcfunction(luastate, DnsGetAnswerTable);
159  lua_setglobal(luastate, "DnsGetAnswers");
160 
161  lua_pushcfunction(luastate, DnsGetAuthorityTable);
162  lua_setglobal(luastate, "DnsGetAuthorities");
163 
164  lua_pushcfunction(luastate, DnsGetTxid);
165  lua_setglobal(luastate, "DnsGetTxid");
166 
167  lua_pushcfunction(luastate, DnsGetRcode);
168  lua_setglobal(luastate, "DnsGetRcode");
169 
170  lua_pushcfunction(luastate, DnsGetRecursionDesired);
171  lua_setglobal(luastate, "DnsGetRecursionDesired");
172  return 0;
173 }
174 
175 #endif /* HAVE_LUA */
uint16_t flags
void DNSCreateRcodeString(uint8_t rcode, char *str, size_t str_size)
struct RSDNSTransaction_ RSDNSTransaction