Skip to content

Commit 2cf9a32

Browse files
catenacybervictorjulien
authored andcommitted
detect/ssh: move ssh.hassh to rust
Introduces helper SCDetectRegisterBufferLowerMd5Callbacks
1 parent 14c78d8 commit 2cf9a32

12 files changed

Lines changed: 109 additions & 365 deletions

rust/src/ssh/detect.rs

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use std::ptr;
2626
use suricata_sys::sys::{
2727
DetectEngineCtx, SCDetectBufferSetActiveList, SCDetectHelperBufferProgressMpmRegister,
2828
SCDetectHelperKeywordAliasRegister, SCDetectHelperKeywordRegister,
29-
SCDetectSignatureSetAppProto, SCSigMatchSilentErrorEnabled, SCSigTableAppLiteElmt, Signature,
29+
SCDetectRegisterBufferLowerMd5Callbacks, SCDetectSignatureSetAppProto,
30+
SCSigMatchSilentErrorEnabled, SCSigTableAppLiteElmt, Signature,
3031
};
3132

3233
#[no_mangle]
@@ -88,31 +89,31 @@ pub unsafe extern "C" fn SCSshTxGetSoftware(
8889

8990
#[no_mangle]
9091
pub unsafe extern "C" fn SCSshTxGetHassh(
91-
tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, direction: u8,
92-
) -> u8 {
92+
tx: *const c_void, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
93+
) -> bool {
9394
let tx = cast_pointer!(tx, SSHTransaction);
9495
match direction.into() {
9596
Direction::ToServer => {
9697
let m = &tx.cli_hdr.hassh;
9798
if !m.is_empty() {
9899
*buffer = m.as_ptr();
99100
*buffer_len = m.len() as u32;
100-
return 1;
101+
return true;
101102
}
102103
}
103104
Direction::ToClient => {
104105
let m = &tx.srv_hdr.hassh;
105106
if !m.is_empty() {
106107
*buffer = m.as_ptr();
107108
*buffer_len = m.len() as u32;
108-
return 1;
109+
return true;
109110
}
110111
}
111112
}
112113
*buffer = ptr::null();
113114
*buffer_len = 0;
114115

115-
return 0;
116+
return false;
116117
}
117118

118119
#[no_mangle]
@@ -212,6 +213,50 @@ unsafe extern "C" fn ssh_hassh_server_string_setup(
212213
return 0;
213214
}
214215

216+
unsafe extern "C" fn ssh_hassh_setup(
217+
de: *mut DetectEngineCtx, s: *mut Signature, _raw: *const std::os::raw::c_char,
218+
) -> c_int {
219+
if SCDetectSignatureSetAppProto(s, ALPROTO_SSH) != 0 {
220+
return -1;
221+
}
222+
if SCDetectBufferSetActiveList(de, s, G_SSH_HASSH_BUFFER_ID) < 0 {
223+
return -1;
224+
}
225+
/* try to enable Hassh */
226+
SCSshEnableHassh();
227+
228+
/* Check if Hassh is disabled */
229+
if !SCSshHasshIsEnabled() {
230+
if !SCSigMatchSilentErrorEnabled(de, DETECT_SSH_HASSH) {
231+
SCLogError!("hassh support is not enabled");
232+
}
233+
return -2;
234+
}
235+
return 0;
236+
}
237+
238+
unsafe extern "C" fn ssh_hassh_server_setup(
239+
de: *mut DetectEngineCtx, s: *mut Signature, _raw: *const std::os::raw::c_char,
240+
) -> c_int {
241+
if SCDetectSignatureSetAppProto(s, ALPROTO_SSH) != 0 {
242+
return -1;
243+
}
244+
if SCDetectBufferSetActiveList(de, s, G_SSH_HASSH_SRV_BUFFER_ID) < 0 {
245+
return -1;
246+
}
247+
/* try to enable Hassh */
248+
SCSshEnableHassh();
249+
250+
/* Check if Hassh is disabled */
251+
if !SCSshHasshIsEnabled() {
252+
if !SCSigMatchSilentErrorEnabled(de, DETECT_SSH_HASSH_SERVER) {
253+
SCLogError!("hassh support is not enabled");
254+
}
255+
return -2;
256+
}
257+
return 0;
258+
}
259+
215260
unsafe extern "C" fn ssh_software_obsolete_setup(
216261
_de: *mut DetectEngineCtx, _s: *mut Signature, _raw: *const std::os::raw::c_char,
217262
) -> c_int {
@@ -230,9 +275,13 @@ static mut G_SSH_SOFTWARE_BUFFER_ID: c_int = 0;
230275
static mut G_SSH_PROTO_BUFFER_ID: c_int = 0;
231276
static mut G_SSH_HASSH_STR_BUFFER_ID: c_int = 0;
232277
static mut G_SSH_HASSH_SRV_STR_BUFFER_ID: c_int = 0;
278+
static mut G_SSH_HASSH_BUFFER_ID: c_int = 0;
279+
static mut G_SSH_HASSH_SRV_BUFFER_ID: c_int = 0;
233280

234281
static mut DETECT_SSH_HASSH_STRING: u16 = 0;
235282
static mut DETECT_SSH_HASSH_SERVER_STRING: u16 = 0;
283+
static mut DETECT_SSH_HASSH: u16 = 0;
284+
static mut DETECT_SSH_HASSH_SERVER: u16 = 0;
236285

237286
#[no_mangle]
238287
pub unsafe extern "C" fn SCDetectSshRegister() {
@@ -337,4 +386,46 @@ pub unsafe extern "C" fn SCDetectSshRegister() {
337386
DETECT_SSH_HASSH_SERVER_STRING,
338387
b"ssh-hassh-server-string\0".as_ptr() as *const libc::c_char,
339388
);
389+
390+
let kw = SigTableElmtStickyBuffer {
391+
name: String::from("ssh.hassh"),
392+
desc: String::from("ssh.hassh sticky buffer"),
393+
url: String::from("/rules/ssh-keywords.html#hassh"),
394+
setup: ssh_hassh_setup,
395+
};
396+
DETECT_SSH_HASSH = helper_keyword_register_sticky_buffer(&kw);
397+
G_SSH_HASSH_BUFFER_ID = SCDetectHelperBufferProgressMpmRegister(
398+
b"ssh.hassh\0".as_ptr() as *const libc::c_char,
399+
b"Ssh Client Fingerprinting For Ssh Clients\0".as_ptr() as *const libc::c_char,
400+
ALPROTO_SSH,
401+
STREAM_TOSERVER,
402+
Some(SCSshTxGetHassh),
403+
SSHConnectionState::SshStateBannerDone as c_int,
404+
);
405+
SCDetectHelperKeywordAliasRegister(
406+
DETECT_SSH_HASSH,
407+
b"ssh-hassh\0".as_ptr() as *const libc::c_char,
408+
);
409+
SCDetectRegisterBufferLowerMd5Callbacks(b"ssh.hassh\0".as_ptr() as *const libc::c_char);
410+
411+
let kw = SigTableElmtStickyBuffer {
412+
name: String::from("ssh.hassh.server"),
413+
desc: String::from("ssh.hassh.server sticky buffer"),
414+
url: String::from("/rules/ssh-keywords.html#ssh.hassh.server"),
415+
setup: ssh_hassh_server_setup,
416+
};
417+
DETECT_SSH_HASSH_SERVER = helper_keyword_register_sticky_buffer(&kw);
418+
G_SSH_HASSH_SRV_BUFFER_ID = SCDetectHelperBufferProgressMpmRegister(
419+
b"ssh.hassh.server\0".as_ptr() as *const libc::c_char,
420+
b"Ssh Client Fingerprinting For Ssh Servers\0".as_ptr() as *const libc::c_char,
421+
ALPROTO_SSH,
422+
STREAM_TOCLIENT,
423+
Some(SCSshTxGetHassh),
424+
SSHConnectionState::SshStateBannerDone as c_int,
425+
);
426+
SCDetectHelperKeywordAliasRegister(
427+
DETECT_SSH_HASSH_SERVER,
428+
b"ssh-hassh-server\0".as_ptr() as *const libc::c_char,
429+
);
430+
SCDetectRegisterBufferLowerMd5Callbacks(b"ssh.hassh.server\0".as_ptr() as *const libc::c_char);
340431
}

rust/sys/src/sys.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ extern "C" {
477477
kw: *const SCTransformTableElmt,
478478
) -> ::std::os::raw::c_int;
479479
}
480+
extern "C" {
481+
pub fn SCDetectRegisterBufferLowerMd5Callbacks(name: *const ::std::os::raw::c_char);
482+
}
480483
#[repr(C)]
481484
#[derive(Debug, Default, Copy, Clone)]
482485
pub struct DeStateStoreItem_ {

src/Makefile.am

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,6 @@ noinst_HEADERS = \
283283
detect-smb-share.h \
284284
detect-smb-version.h \
285285
detect-smtp.h \
286-
detect-ssh-hassh-server.h \
287-
detect-ssh-hassh.h \
288286
detect-ssl-state.h \
289287
detect-ssl-version.h \
290288
detect-stream_size.h \
@@ -877,8 +875,6 @@ libsuricata_c_a_SOURCES = \
877875
detect-smb-share.c \
878876
detect-smb-version.c \
879877
detect-smtp.c \
880-
detect-ssh-hassh-server.c \
881-
detect-ssh-hassh.c \
882878
detect-ssl-state.c \
883879
detect-ssl-version.c \
884880
detect-stream_size.c \

src/detect-engine-helper.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,9 @@ int SCDetectHelperTransformRegister(const SCTransformTableElmt *kw)
169169

170170
return transform_id;
171171
}
172+
173+
void SCDetectRegisterBufferLowerMd5Callbacks(const char *name)
174+
{
175+
DetectBufferTypeRegisterSetupCallback(name, DetectLowerSetupCallback);
176+
DetectBufferTypeRegisterValidateCallback(name, DetectMd5ValidateCallback);
177+
}

src/detect-engine-helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *d
9393

9494
int SCDetectHelperTransformRegister(const SCTransformTableElmt *kw);
9595

96+
void SCDetectRegisterBufferLowerMd5Callbacks(const char *name);
97+
9698
#endif /* SURICATA_DETECT_ENGINE_HELPER_H */

src/detect-engine-register.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,6 @@
234234
#include "detect-tls.h"
235235
#include "detect-tls-cert-validity.h"
236236
#include "detect-tls-version.h"
237-
#include "detect-ssh-hassh.h"
238-
#include "detect-ssh-hassh-server.h"
239237
#include "detect-http-stat-code.h"
240238
#include "detect-ssl-version.h"
241239
#include "detect-ssl-state.h"
@@ -708,8 +706,6 @@ void SigTableSetup(void)
708706
DetectBsizeRegister();
709707
DetectDetectionFilterRegister();
710708
DetectAsn1Register();
711-
DetectSshHasshRegister();
712-
DetectSshHasshServerRegister();
713709
DetectSslStateRegister();
714710
DetectSslVersionRegister();
715711
DetectByteExtractRegister();

src/detect-engine-register.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ enum DetectKeywordId {
189189
DETECT_HTTP_REQUEST_LINE,
190190
DETECT_HTTP_RESPONSE_LINE,
191191
DETECT_NFS_VERSION,
192-
DETECT_SSH_HASSH,
193-
DETECT_SSH_HASSH_SERVER,
194192
DETECT_SSL_VERSION,
195193
DETECT_SSL_STATE,
196194
DETECT_FILE_DATA,

src/detect-ssh-hassh-server.c

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)