Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Libsodium sodium support for sha256 filehashes #1381

Open
wants to merge 113 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
523acdb
Add USE_LIBSODIUM to the Makefile
ddpbsd Dec 19, 2017
e1468ea
Start to add options for allowing different FIM hashes to be used.
ddpbsd Dec 19, 2017
201bc61
Don't allow sha256 in non-sodium builds.
ddpbsd Dec 19, 2017
1fa3d1e
Silence a clang warning.
ddpbsd Dec 19, 2017
26c27e1
Make sure msg is initialized.
ddpbsd Dec 19, 2017
a9e725b
Merge branch 'master' of https://github.com/ossec/ossec-hids into lib…
ddpbsd Jan 15, 2018
a5452f9
Try to use libsodium to allow us to check sha256 hashes.
ddpbsd Jan 15, 2018
882058b
Fix a typo
ddpbsd Jan 17, 2018
09b44ce
xml_check_sha256sum is not used
ddpbsd Jan 17, 2018
fbcd126
randombytes -> OS_randombytes. There is a randombytes() in libsodium …
ddpbsd Jan 17, 2018
7e91526
Fix-up the sha256 stuff. It compiles. Testing will begin shortly.
ddpbsd Jan 17, 2018
a556191
Merged into md5_sha1_op.c.
ddpbsd Jan 17, 2018
bd9fa4d
randombytes() -> OS_randombytes(). libsodium has a randombytes() too
ddpbsd Jan 17, 2018
fb1ac49
Add some more libsodium bits.
ddpbsd Feb 11, 2018
ce552d3
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Feb 11, 2018
765b499
alg_output -> hash_output
ddpbsd Feb 14, 2018
9b339f8
OS_algorithms_File -> OS_Hash_File
ddpbsd Feb 14, 2018
a978513
Make sure we don't set the output for a hash if we aren't checking
ddpbsd Feb 14, 2018
b728cf5
sprintf->snprintf
ddpbsd Feb 14, 2018
da38953
Don't run the check if the md5 is 'xxx'
ddpbsd Feb 15, 2018
3dbaf31
Not sure what I was thinking, but correct a stupid typo.
ddpbsd Feb 15, 2018
c75e255
Go about this in a slightly different way.
ddpbsd Feb 15, 2018
98b4e64
Closer to supporting sha256
ddpbsd Feb 15, 2018
54cdbd4
The basic idea is to have hash1 and hash2. Then the hash strings
ddpbsd Feb 15, 2018
1134eaa
Add some more libsodium bits to create_db.c.
ddpbsd Mar 6, 2018
9b1b3eb
I think sha256 is working.
ddpbsd Mar 7, 2018
e7e4b1a
Remove some debugging printfs
ddpbsd Mar 7, 2018
a166003
Merge branch 'master' of https://github.com/ossec/ossec-hids into lib…
ddpbsd Mar 7, 2018
2dfc8d6
Make this actually compile.
ddpbsd Mar 11, 2018
4910ca6
sqlite
ddpbsd Mar 13, 2018
80a960a
Add some explicit nul terminators
ddpbsd Mar 13, 2018
d0b0050
More sha256 work. Still lots of debug stuff.
ddpbsd Mar 14, 2018
69945bd
Remove some debugging.
ddpbsd Mar 14, 2018
de70834
Get rid of some debug stuff.
ddpbsd Mar 14, 2018
17470fd
More debugging
ddpbsd Mar 14, 2018
2d488e1
Correctly deal with these variables.
ddpbsd Mar 15, 2018
6fe831a
Oops, I prefer -ggdb to -g, so I often change it and switch back before
ddpbsd Mar 15, 2018
40815a3
Linux didn't like writing a variable to itself with snprintf, so
Mar 15, 2018
8e5401c
Spacing
ddpbsd Mar 15, 2018
241817e
Make sure sodium.h isn't added if we're not in LIBSODIUM_ENABLED
ddpbsd Mar 15, 2018
c323f46
Disable these for now. The idea isn't fully fleshed out yet.
ddpbsd Mar 15, 2018
fd65186
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Mar 15, 2018
d1ca9f3
Try to do tests for libsodium and sqlite
ddpbsd Mar 23, 2018
869d653
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Mar 23, 2018
54b5df1
CID 166736: Ignoring number of bytes read
ddpbsd Apr 11, 2018
1df3fb7
CID 153424: Buffer not null terminated
ddpbsd Apr 11, 2018
01a28e7
CID 28500: Resource leak
ddpbsd Apr 11, 2018
53c586c
CID 28511: Resource leak
ddpbsd Apr 11, 2018
e053db8
Merge branch 'master' of https://github.com/ossec/ossec-hids into cov…
ddpbsd Apr 17, 2018
a451d8e
spaces -> tabs
ddpbsd Apr 17, 2018
efe31a5
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Apr 23, 2018
258fe5d
Merge branch 'coverity_20180411' of github.com:ddpbsd/ossec-hids into…
ddpbsd Apr 23, 2018
9d74ff7
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Apr 30, 2018
4f044a3
Close fp before leaving GetRandomNoise()
ddpbsd Apr 30, 2018
e16624b
Clarify calculation precedence for '&' and '?'.
ddpbsd Apr 30, 2018
e98f71c
The rest of the "Clarify calculation precedence for '&' and '?'" commit.
ddpbsd Apr 30, 2018
02ff1ba
No libsodium-dev or libsodium in Trusty? There's a ppa for that!
ddpbsd May 1, 2018
274b56b
md5->sha256
ddpbsd May 25, 2018
7990e9e
Get rid of hash1 and hash2. Bad idea.
ddpbsd May 25, 2018
4df6e64
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd May 30, 2018
7a9f8a8
From codacy
ddpbsd May 30, 2018
fc54a34
Get rid of hash1, hash2.
ddpbsd May 31, 2018
cd7831c
Adjust
ddpbsd Jun 5, 2018
6dfc111
Force add the libsodium repository
ddpbsd Jun 6, 2018
827a62a
I'll get this right eventually.
ddpbsd Jun 6, 2018
a157c31
Try this again.
ddpbsd Jun 6, 2018
db2c5b7
Makes testing easier
ddpbsd Jun 6, 2018
e8d7873
Don't mention blake2b in case libsodium changes their generic algorithm.
ddpbsd Jun 8, 2018
b2923f6
Add libsodium's generichash (blake2b right now)
ddpbsd Jun 8, 2018
bb3a7e7
Start to add blake2b
ddpbsd Jun 9, 2018
51f61ee
Make opts available everywhere.
ddpbsd Jun 9, 2018
1edbff1
Start to make sure this reports everything properly.
ddpbsd Jun 9, 2018
028ac28
Caused a crash in reportd
ddpbsd Jun 22, 2018
992f532
I need these right now
ddpbsd Jun 22, 2018
bd7cf0a
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Jun 22, 2018
fdadbef
Looks like I was previously truncating the SHA256 and GENERIC hash
ddpbsd Jun 22, 2018
b3088db
Generichash wasn't named in the log output.
ddpbsd Jun 22, 2018
50aa923
Make sure the generic hash is written to new_hashes.
ddpbsd Jun 22, 2018
4347d84
Remove my debug stuff.
ddpbsd Jun 22, 2018
3a230d7
Remove some debugging stuff.
ddpbsd Jun 22, 2018
a36f047
This doesn't have to be extern here
ddpbsd Jun 22, 2018
1682c31
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Jun 26, 2018
80381a0
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Jun 27, 2018
e63681a
The variable should be sha256 not generic.
ddpbsd Jun 27, 2018
ae89927
Increase the alert_msg size from 916 to 2048 in some places.
ddpbsd Jun 28, 2018
6835e83
Try to push the correct sums to run_check.
ddpbsd Jul 12, 2018
a5244f9
Add some more debugging.
ddpbsd Jul 12, 2018
16effe5
Instead of passing syscheck_opts around (which I didn't do correctly)
ddpbsd Jul 12, 2018
72d0ffb
I forgot to copy new_hashes_tmp to new_hashes like a moron.
ddpbsd Jul 13, 2018
6252474
Remove the poorly done syscheck_opts idea. I switched to passing
ddpbsd Jul 13, 2018
6302c1c
Remove more debugging stuff.
ddpbsd Jul 13, 2018
792dea6
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Jul 13, 2018
5d5e806
Add the hash names into the hash output so that we know what we're
ddpbsd Jul 19, 2018
b2af210
Merge branch 'master' of https://github.com/ossec/ossec-hids into lib…
ddpbsd Jul 19, 2018
a38f439
Get rid of some debugging.
ddpbsd Jul 25, 2018
c02f109
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Jul 31, 2018
ab95af3
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Aug 6, 2018
e6f2fd5
Merge branch 'master' of github.com:ossec/ossec-hids into libsodium
ddpbsd Oct 12, 2018
00b6de1
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Oct 15, 2018
9b5ddc7
Missed a conflict.
ddpbsd Oct 16, 2018
f803934
Merge branch 'libsodium' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Oct 23, 2018
4e2b2bf
Merge branch 'master' of github.com:ossec/ossec-hids into libsodium
ddpbsd Oct 29, 2018
5b6f673
Fix this reference.
ddpbsd Nov 5, 2018
fe1dd8f
Handle the different hash types better.
ddpbsd Nov 5, 2018
bd5cc78
Remove some debugging
ddpbsd Nov 5, 2018
136e7f3
Update the hash sizes
ddpbsd Jan 2, 2019
5092227
I'm not sure why I thought strncat worked like that.
ddpbsd Jan 2, 2019
823a0e4
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Feb 19, 2019
86f23ff
Make sure we can handle md5 and sha1 in libsodium mode too.
ddpbsd Apr 18, 2019
425391c
I'm not sure there is enough space for the blake2b hash, so default
ddpbsd Apr 18, 2019
0cc90b4
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Apr 29, 2019
3082ed5
Missed a white->allow change.
ddpbsd Apr 29, 2019
17e2a2b
Merge branch 'master' of github.com:ddpbsd/ossec-hids into libsodium
ddpbsd Oct 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.DS_Store
*.dll
*.exe
*.core

# Auto generated build files
src/LOCATION
Expand Down
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ before_script:
- if [[ "${PRELUDE}" == "yes" ]]; then ( sudo apt-get install libprelude-dev ); fi
- if [[ "${ZEROMQ}" == "yes" ]]; then ( sudo apt-get install libzmq3-dev libtool autoconf libczmq-dev ); fi
- if [[ "${OSSEC_TYPE}" == "winagent" ]]; then ( sudo apt-get install aptitude && sudo aptitude -y install mingw-w64 nsis ); fi
- if [[ "${OSSEC_TYPE}" == "server" ]]; then ( sudo apt-get install libsqlite3-dev sqlite3 ); fi
- if [[ "${USE_SQLITE}" == "yes" ]]; then ( sudo apt-get install libsqlite3-dev sqlite3 ); fi
- if [[ "${USE_LIBSODIUM}" == "yes" ]]; then ( sudo add-apt-repository -y ppa:chris-lea/libsodium && sudo apt-get update
&& sudo apt-get install libsodium-dev libsodium13
); fi
- if [[ "${OSSEC_TYPE}" == "test" ]]; then ( sudo apt-get update && sudo apt-get install check valgrind ); fi


Expand Down
12 changes: 11 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ USE_PRELUDE?=no
USE_ZEROMQ?=no
USE_GEOIP?=no
USE_INOTIFY=no
USE_SQLITE?=no
USE_LIBSODIUM?=no
USE_PCRE2_JIT=yes

ifneq (${TARGET},winagent)
Expand Down Expand Up @@ -143,6 +145,8 @@ OSSEC_CFLAGS=${CFLAGS}

ifdef DEBUG
OSSEC_CFLAGS+=-g
else
OSSEC_CFLAGS+=-O2
endif #DEBUG

ifneq (,$(filter ${CLEANFULL},yes y Y 1))
Expand Down Expand Up @@ -239,6 +243,11 @@ ifneq (,$(filter ${USE_GEOIP},auto yes y Y 1))
OSSEC_LDFLAGS+=-lGeoIP
endif # USE_GEOIP

ifneq (,$(filter ${USE_LIBSODIUM},auto yes y Y 1))
DEFINES+=-DLIBSODIUM_ENABLED
OSSEC_LDFLAGS+=-lsodium
endif # USE_LIBSODIUM

ifneq (,$(filter ${USE_SQLITE},auto yes y Y 1))
DEFINES+=-DSQLITE_ENABLED
ANALYSISD_FLAGS="-lsqlite3"
Expand Down Expand Up @@ -601,6 +610,7 @@ settings:
@echo " USE_PRELUDE: ${USE_PRELUDE}"
@echo " USE_OPENSSL: ${USE_OPENSSL}"
@echo " USE_INOTIFY: ${USE_INOTIFY}"
@echo " USE_LIBSODIUM: ${USE_LIBSODIUM}"
@echo " USE_SQLITE: ${USE_SQLITE}"
@echo " USE_PCRE2_JIT: ${USE_PCRE2_JIT}"
@echo "Mysql settings:"
Expand Down Expand Up @@ -1103,7 +1113,7 @@ syscheck_o := $(syscheck_c:.c=.o)
syscheckd/%.o: syscheckd/%.c
${OSSEC_CC} ${OSSEC_CFLAGS} -DARGV0=\"ossec-syscheckd\" -c $^ -o $@

ossec-syscheckd: ${syscheck_o} rootcheck.a ${ossec_libs} ${ZLIB_LIB}
ossec-syscheckd: ${syscheck_o} rootcheck.a os_crypto.a ${ossec_libs} ${ZLIB_LIB}
${OSSEC_CCBIN} ${OSSEC_CFLAGS} ${ZLIB_INCLUDE} $^ ${OSSEC_LDFLAGS} -o $@

#### Monitor #######
Expand Down
74 changes: 57 additions & 17 deletions src/analysisd/decoders/syscheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ typedef struct __sdb {
char perm[OS_FLSIZE + 1];
char owner[OS_FLSIZE + 1];
char gowner[OS_FLSIZE + 1];
#ifdef LIBSODIUM_ENABLED
char md5[(OS_FLSIZE * 2) + 1];
char sha1[(OS_FLSIZE * 2) + 1];
#else //LIBSODIUM_ENABLED
char md5[OS_FLSIZE + 1];
char sha1[OS_FLSIZE + 1];
#endif

char agent_cp[MAX_AGENTS + 1][1];
char *agent_ips[MAX_AGENTS + 1];
Expand Down Expand Up @@ -536,9 +541,26 @@ static int DB_Search(const char *f_name, const char *c_sum, Eventinfo *lf)
if (!newmd5 || !oldmd5 || strcmp(newmd5, oldmd5) == 0) {
sdb.md5[0] = '\0';
} else {
#ifdef LIBSODIUM_ENABLED
char *hash_type;
if (strncmp(newmd5, "GENERIC", 7) == 0) {
hash_type = "blake2b";
} else if (strncmp(newmd5, "SHA256", 6) == 0) {
hash_type = "sha256";
} else if (strncmp(newmd5, "MD5", 3) == 0) {
hash_type = "md5";
} else if (strncmp(newmd5, "SHA1", 4) == 0) {
hash_type = "sha1";
} else {
hash_type = "unknown";
}
snprintf(sdb.md5, OS_FLSIZE * 2, "Old %s was: '%s'\n"
"New %s is: '%s'\n", hash_type, oldmd5, hash_type, newmd5);
#else //LIBSODIUM_ENABLED
snprintf(sdb.md5, OS_FLSIZE, "Old md5sum was: '%s'\n"
"New md5sum is : '%s'\n",
"New md5sum is: '%s'\n",
oldmd5, newmd5);
#endif //LIBSODIUM_ENABLED
os_strdup(oldmd5, lf->md5_before);
os_strdup(newmd5, lf->md5_after);
}
Expand All @@ -547,9 +569,22 @@ static int DB_Search(const char *f_name, const char *c_sum, Eventinfo *lf)
if (!newsha1 || !oldsha1 || strcmp(newsha1, oldsha1) == 0) {
sdb.sha1[0] = '\0';
} else {
#ifdef LIBSODIUM_ENABLED
char *hash_type;
if(strncmp(newsha1, "GENERIC", 7) == 0) {
hash_type = "blake2b";
} else if(strncmp(newsha1, "SHA256", 6) == 0) {
hash_type = "sha256";
} else {
hash_type = "unknown";
}
snprintf(sdb.sha1, OS_FLSIZE * 2, "Old %s was: '%s'\n"
"New %s is : '%s'\n", hash_type, oldsha1, hash_type, newsha1);
#else //LIBSODIUM_ENABLED
snprintf(sdb.sha1, OS_FLSIZE, "Old sha1sum was: '%s'\n"
"New sha1sum is : '%s'\n",
oldsha1, newsha1);
#endif //LIBSODIUM_ENABLED
os_strdup(oldsha1, lf->sha1_before);
os_strdup(newsha1, lf->sha1_after);
}
Expand Down Expand Up @@ -695,25 +730,30 @@ int DecodeSyscheck(Eventinfo *lf)
if (Config.md5_allowlist) {
extern sqlite3 *conn;
if ((p = extract_token(c_sum, ":", 4))) {
if (!validate_md5(p)) { /* Never trust input from other origin */
merror("%s: Not a valid MD5 hash: '%s'", ARGV0, p);
return(0);
}
debug1("%s: Checking MD5 '%s' in %s", ARGV0, p, Config.md5_allowlist);
sprintf(stmt, "select md5sum from files where md5sum = \"%s\"", p);
error = sqlite3_prepare_v2(conn, stmt, 1000, &res, &tail);
if (error == SQLITE_OK) {
while (sqlite3_step(res) == SQLITE_ROW) {
rec_count++;
}
if (rec_count) {
sqlite3_finalize(res);
//sqlite3_close(conn);
merror(MD5_NOT_CHECKED, ARGV0, p);
if((strncmp(p, "xxx", 3)) != 0) {
if (!validate_md5(p)) { /* Never trust input from other origin */
merror("%s: Not a valid MD5 hash: '%s'", ARGV0, p);
return(0);
}
debug1("%s: Checking MD5 '%s' in %s", ARGV0, p, Config.md5_allowlist);
if((snprintf(stmt, OS_MAXSTR, "select md5sum from files where md5sum = \"%s\"", p)) < 0) {
merror("ERROR: snprintf failed for md5sum: %s", p);
}
stmt[OS_MAXSTR] = '\0';
error = sqlite3_prepare_v2(conn, stmt, 1000, &res, &tail);
if (error == SQLITE_OK) {
while (sqlite3_step(res) == SQLITE_ROW) {
rec_count++;
}
if (rec_count) {
sqlite3_finalize(res);
//sqlite3_close(conn);
merror(MD5_NOT_CHECKED, ARGV0, p);
return(0);
}
}
sqlite3_finalize(res);
}
sqlite3_finalize(res);
}
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/analysisd/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ int Rules_OP_ReadRules(const char *rulefile)
merror("rules_op: Invalid root element \"%s\"."
"Only \"group\" is allowed", node[i]->element);
OS_ClearXML(&xml);
free(node);
return (-1);
}
if ((!node[i]->attributes) || (!node[i]->values) ||
Expand All @@ -208,11 +209,13 @@ int Rules_OP_ReadRules(const char *rulefile)
merror("rules_op: Invalid root element '%s'."
"Only the group name is allowed", node[i]->element);
OS_ClearXML(&xml);
free(node);
return (-1);
}
} else {
merror(XML_READ_ERROR, ARGV0);
OS_ClearXML(&xml);
free(node);
return (-1);
}
i++;
Expand Down
76 changes: 72 additions & 4 deletions src/config/syscheck-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ static int read_attr(syscheck_config *syscheck, const char *dirs, char **g_attrs
const char *xml_check_sum = "check_sum";
const char *xml_check_sha1sum = "check_sha1sum";
const char *xml_check_md5sum = "check_md5sum";
const char *xml_check_sha256sum = "check_sha256sum";
const char *xml_check_genericsum = "check_genericsum";
const char *xml_check_size = "check_size";
const char *xml_check_owner = "check_owner";
const char *xml_check_group = "check_group";
Expand Down Expand Up @@ -220,19 +222,39 @@ static int read_attr(syscheck_config *syscheck, const char *dirs, char **g_attrs
attrs = g_attrs;
values = g_values;

#ifdef LIBSODIUM_ENABLED
#ifdef DEBUG
merror("DEBUG: libsodium enabled");
#endif //DEBUG
#endif //LIBSODIUM_ENABLED

while (*attrs && *values) {
/* Check all */
if (strcmp(*attrs, xml_check_all) == 0) {
if (strcmp(*values, "yes") == 0) {
#ifdef LIBSODIUM_ENABLED
opts |= CHECK_PERM;
opts |= CHECK_SIZE;
opts |= CHECK_OWNER;
opts |= CHECK_GROUP;
opts |= CHECK_SHA256SUM;
opts |= CHECK_MD5SUM;
#else //LIBSODIUM_ENABLED
opts |= CHECK_SHA1SUM;
opts |= CHECK_MD5SUM;
opts |= CHECK_PERM;
opts |= CHECK_SIZE;
opts |= CHECK_OWNER;
opts |= CHECK_GROUP;
#endif //LIBSODIUM_ENABLED
} else if (strcmp(*values, "no") == 0) {
#ifdef LIBSODIUM_ENABLED
opts &= ~ ( CHECK_MD5SUM | CHECK_SHA1SUM | CHECK_PERM
| CHECK_SIZE | CHECK_OWNER | CHECK_GROUP | CHECK_SHA256SUM | CHECK_GENERIC );
#else //LIBSODIUM_ENABLED
opts &= ~ ( CHECK_MD5SUM | CHECK_SHA1SUM | CHECK_PERM
| CHECK_SIZE | CHECK_OWNER | CHECK_GROUP );
#endif //LIBSODIUM_ENABLED
} else {
merror(SK_INV_OPT, __local_name, *values, *attrs);
ret = 0;
Expand All @@ -242,10 +264,20 @@ static int read_attr(syscheck_config *syscheck, const char *dirs, char **g_attrs
/* Check sum */
else if (strcmp(*attrs, xml_check_sum) == 0) {
if (strcmp(*values, "yes") == 0) {
#ifdef LIBSODIUM_ENABLED
opts |= CHECK_SHA256SUM;
opts |= CHECK_GENERIC;
#else //LIBSODIUM_ENABLED
opts |= CHECK_MD5SUM;
opts |= CHECK_SHA1SUM;
#endif //LIBSODIUM_ENABLED

} else if (strcmp(*values, "no") == 0) {
#ifdef LIBSODIUM_ENABLED
opts &= ~ ( CHECK_GENERIC | CHECK_SHA256SUM );
#else //LIBSODIUM_ENALBED
opts &= ~ ( CHECK_MD5SUM | CHECK_SHA1SUM );
#endif
} else {
merror(SK_INV_OPT, __local_name, *values, *attrs);
ret = 0;
Expand Down Expand Up @@ -276,6 +308,30 @@ static int read_attr(syscheck_config *syscheck, const char *dirs, char **g_attrs
goto out_free;
}
}
#ifdef LIBSODIUM_ENABLED
else if(strncmp(*attrs, xml_check_sha256sum, 15) == 0) {
if(strncmp(*values, "yes", 3) == 0) {
opts |= CHECK_SHA256SUM;
} else if(strncmp(*values, "no", 2) == 0) {
opts &= ~ CHECK_SHA256SUM;
} else {
merror(SK_INV_OPT, __local_name, *values, *attrs);
ret = 0;
goto out_free;
}
}
else if(strncmp(*attrs, xml_check_genericsum, 16) == 0) {
if(strncmp(*values, "yes", 3) == 0) {
opts |= CHECK_GENERIC;
} else if(strncmp(*values, "no", 2) == 0) {
opts &= ~ CHECK_GENERIC;
} else {
merror(SK_INV_OPT, __local_name, *values, *attrs);
ret = 0;
goto out_free;
}
}
#endif //LIBSODIUM_ENABLED
/* Check permission */
else if (strcmp(*attrs, xml_check_perm) == 0) {
if (strcmp(*values, "yes") == 0) {
Expand Down Expand Up @@ -495,6 +551,7 @@ int Read_Syscheck(XML_NODE node, void *configp, __attribute__((unused)) void *ma
ExpandEnvironmentStrings(node[i]->content, dirs, sizeof(dirs) - 1);
#else
strncpy(dirs, node[i]->content, sizeof(dirs) - 1);
dirs[sizeof(dirs) - 1] = '\0';
#endif

if (!read_attr(syscheck,
Expand Down Expand Up @@ -793,19 +850,22 @@ int Read_Syscheck(XML_NODE node, void *configp, __attribute__((unused)) void *ma
ExpandEnvironmentStrings(node[i]->content, cmd, sizeof(cmd) - 1);
#else
strncpy(cmd, node[i]->content, sizeof(cmd) - 1);
cmd[sizeof(cmd) - 1] = '\0';
#endif

if (strlen(cmd) > 0) {
char statcmd[OS_MAXSTR];
char *ix;
strncpy(statcmd, cmd, sizeof(statcmd) - 1);
statcmd[sizeof(statcmd) - 1] = '\0';
if (NULL != (ix = strchr(statcmd, ' '))) {
*ix = '\0';
}
if (stat(statcmd, &statbuf) == 0) {
/* More checks needed (perms, owner, etc.) */
os_calloc(1, strlen(cmd) + 1, syscheck->prefilter_cmd);
strncpy(syscheck->prefilter_cmd, cmd, strlen(cmd));
syscheck->prefilter_cmd[sizeof(syscheck->prefilter_cmd) - 1] = '\0';
} else {
merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
return (OS_INVALID);
Expand Down Expand Up @@ -833,24 +893,32 @@ char *syscheck_opts2str(char *buf, int buflen, int opts) {
CHECK_SIZE,
CHECK_OWNER,
CHECK_GROUP,
CHECK_MD5SUM,
CHECK_MD5SUM,
CHECK_SHA1SUM,
CHECK_REALTIME,
CHECK_SEECHANGES,
#ifdef LIBSODIUM_ENABLED

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having MD5 and SHA1 together makes some sense as both can have collisions, but dual-mode collisions are much less likely. SHA256 on the other hand is already computationally expensive, and not determined to have predictable collisions far as i know. Would it make more sense to

#ifdef LIBSODIUM_ENABLED
CHECK_SHA256SUM,
#else
CHECK_MD5SUM,
CHECK_SHA1SUM,
#end

so as to avoid the redundancy in processing and storage?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against that, I was just continuing the tradition of using 2 hashes. I hope to replace md5 with blake2b (or whatever libsodium has as default at that point) in the future. md5 and sha256 give an analyst more opportunities to compare the hashes to "known bad" files out there. I've seen plenty of Indicators of Compromise reports still using md5.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, and some still use sha1, which sounds like "we might want them all." Maybe a semantic in which we store one hash and create other-algo-derived hashes on a change event for the alert would address that? Users could configure generation of event hashes in the fmt used by their malware engine, or just to pass the unk file to viper or whatever they're using for sample management to let another component deal with that rabbit hole.

CHECK_SHA256SUM,
CHECK_GENERIC,
#endif //LIBSODIUM_ENABLED
CHECK_NORECURSE,
0
0
};
char *check_strings[] = {
"perm",
"size",
"owner",
"group",
"md5sum",
"md5sum",
"sha1sum",
"realtime",
"report_changes",
#ifdef LIBSODIUM_ENABLED

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above

"sha256sum",
"genericsum",
#endif //LIBSODIUM_ENABLED
"no_recurse",
NULL
NULL
};

buf[0] = '\0';
Expand Down
5 changes: 4 additions & 1 deletion src/config/syscheck-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#define CHECK_GENERIC 0001000
#define CHECK_NORECURSE 0002000


#include <stdio.h>

#include "os_regex/os_regex.h"
Expand All @@ -54,6 +53,10 @@ typedef struct _config {

int *opts; /* attributes set in the <directories> tag element */

char *algorithms; /* Algorithms to use for FIM */
char *hash1_alg;
char *hash2_alg;

char *remote_db;
char *db;

Expand Down
2 changes: 1 addition & 1 deletion src/headers/file_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int IsDir(const char *file) __attribute__((nonnull));

int CreatePID(const char *name, int pid) __attribute__((nonnull));

char *GetRandomNoise();
char *GetRandomNoise(void);

int DeletePID(const char *name) __attribute__((nonnull));

Expand Down
Loading