nixpkgs icon indicating copy to clipboard operation
nixpkgs copied to clipboard

Package request: wazuh-agent

Open ijaketak opened this issue 3 years ago • 112 comments

Project description

Wazuh is a security platform that provides unified XDR and SIEM protection for endpoints and cloud workloads.

Metadata

  • homepage URL: https://wazuh.com/
  • source URL: https://github.com/wazuh/wazuh
  • license: gpl2
  • platforms: linux

ijaketak avatar May 08 '23 07:05 ijaketak

I think it would be beneficial to have it set up as a service right? Especially for managing multiple servers.

I'm imagining something like this for agents:

{
services.wazuh = {
   type = "agent";
   manager = "10.10.13.37";
};
}

And likewise for managers:

{
services.wazuh = {
   type = "manager";
   settings = {
        ....
   };
};
}

I don't have a whole lot of experience packaging stuff for nix yet, but will try and play around with it when i find the time as it a software suite i would love in my homelab. If anyone with more experience packaging stuff for nix, that would either take the lead or be a sparring partner for a newcomer it would be very appriciated.

c3lphie avatar Nov 02 '23 21:11 c3lphie

@c3lphie

It would be interesting. Do you still intend to proceed with packaging?

stnert avatar Nov 15 '23 16:11 stnert

I can definetly try and give it a shot. I have like zero experience as i said so help would be more than apreciated

c3lphie avatar Nov 15 '23 16:11 c3lphie

I'm in the same boat as c3lphie. I would like to contribute, but might need some guidance on where to look for instruction.

For anyone in the NixOS wilderness who's still interested, my attempt at trying to get the agent working is here: https://github.com/V3ntus/nixpkgs/blob/wazuh-agent/pkgs/tools/security/wazuh/default.nix

I am but a beginner as well though. Main issues at hand:

  • install.sh needs to be run as root: Either we eliminate install.sh and manage the installation and build manually, or maybe there's some weird way to give it "root" access
  • configurePhase needs to not be hardcoded and should be given service options. But this is the baseline to get install.sh to run headless

V3ntus avatar Apr 03 '24 02:04 V3ntus

Mainly just a TODO note for me, but maybe it's possible to use buildFHSEnv while installing?

V3ntus avatar Apr 03 '24 15:04 V3ntus

@V3ntus I was able to take some of what you have here and I've got my own fork here: https://github.com/sjdwhiting/nixpkgs/blob/master/pkgs/tools/security/wazuh/default.nix

Couple of notes

  • I was able to get install.sh to run using fakeroot but...
  • I don't think using install.sh as is will work very well. I've tried a few times and I think it will be easier to complile everything and essentially handle it manually.
  • Either way, external dependencies needed to be handled so I took care of that and eliminated the need to run make deps which was pretty error prone and I think this is the more nix way of doing things. (pretty new to Nix so open to feedback here!)
  • I've been slowly applying script patches to handle issues with commands being unavailable or failing to run.

The biggest issue I'm running into now is various times when make or other scripts that get called in the process want to manipulate files and it is blocked from doing so. Here is the latest example

make[1]: Leaving directory '/nix/store/xb5hbjld2gpi1jf3f06mwpw85klbdagq-wazuh-agent-4.7.3/src/external/libarchive'
cp /nix/store/a3zlvnswi1p8cg7i9w4lpnvaankc7dxx-gcc-12.3.0-lib/lib/libstdc++.so.6 libstdc++.so.6
strip -x libstdc++.so.6
/nix/store/3z013mdl9cvpgvavpj19rbilihz4clvi-binutils-2.40/bin/strip: unable to copy file 'libstdc++.so.6'; reason: Permission denied
make: *** [Makefile:775: libstdc++.so.6] Error 1
error: builder for '/nix/store/ydqi49bbi2hmsnd5zhr25xfvjqah0an4-wazuh-agent-4.7.3.drv' failed with exit code 2;
       last 10 log lines:
       >   CCLD     bsdcpio
       >   CC       cat/bsdcat-bsdcat.o
       >   CC       cat/bsdcat-cmdline.o
       >   CCLD     bsdcat
       > make[2]: Leaving directory '/nix/store/xb5hbjld2gpi1jf3f06mwpw85klbdagq-wazuh-agent-4.7.3/src/external/libarchive'
       > make[1]: Leaving directory '/nix/store/xb5hbjld2gpi1jf3f06mwpw85klbdagq-wazuh-agent-4.7.3/src/external/libarchive'
       > cp /nix/store/a3zlvnswi1p8cg7i9w4lpnvaankc7dxx-gcc-12.3.0-lib/lib/libstdc++.so.6 libstdc++.so.6
       > strip -x libstdc++.so.6
       > /nix/store/3z013mdl9cvpgvavpj19rbilihz4clvi-binutils-2.40/bin/strip: unable to copy file 'libstdc++.so.6'; reason: Permission denied
       > make: *** [Makefile:775: libstdc++.so.6] Error 1
       For full logs, run 'nix-store -l /nix/store/ydqi49bbi2hmsnd5zhr25xfvjqah0an4-wazuh-agent-4.7.3.drv'.```

sjdwhiting avatar Apr 09 '24 18:04 sjdwhiting

@sjdwhiting That's awesome! Appreciate your efforts.

Yeah I was starting to come to the conclusion that bypassing install.sh would be more beneficial than trying to get it to work on Nix. That leaves us with reverse-engineering install.sh and adapting its operations to the declarative Nix approach. Big respect to who gets that working.

Aside from building wazuh packages from source, I also considered utilizing the .deb package directly (I've seen some Nix packages install with this method), but again, not sure how that would work mapping FHS to non-FHS directories, linking, etc.

I'll check out your fork, I'm excited to see someone else's approach

V3ntus avatar Apr 09 '24 18:04 V3ntus

I'm also curious about the fakeroot usage. wazuh, in principle, needs root access for many of it's operations, I would assume. Looking at the Debian docs, looks like fakeroot is useful for package builds when, for example, one would need to package files with root ownership. If we're installing with fakeroot, it sounds like the make install call is deviating away from the Nix environment. Would this interfere with wazuh being installed incorrectly (wrong location, wrong permissions, etc.)?

V3ntus avatar Apr 09 '24 18:04 V3ntus

I don't think there is a need to call make install. I think we can just move everything as needed to the correct directories in the nix store and setup symlinks where needed. For example. /var/ossec/ can be sym linked to the nix store entry.

Wazuh has some notes here on manual install and they make no mention of running make install https://github.com/wazuh/wazuh/blob/master/INSTALL

sjdwhiting avatar Apr 09 '24 19:04 sjdwhiting

Update: Sorted out that error I posted, now I'm iterating through fixing similar ones as they come up. I'm just copying the files over in an earlier phase. Worst part is that it is progressing far enough in the build process that my cycle time is a couple of minutes with each fix.

sjdwhiting avatar Apr 09 '24 19:04 sjdwhiting

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/packaging-wazuh-for-nixos/43124/1

nixos-discourse avatar Apr 10 '24 14:04 nixos-discourse

Hi @sjdwhiting @V3ntus I got this working based off your code. Please see the my comment on how to reproduce it. There's still more work to get the agent working into a service, but at least the agent is compiling and executing now.

nealfennimore avatar Apr 13 '24 18:04 nealfennimore

Awesome, thanks for the work you put in! @nealfennimore

I'm assuming due to the need for sudo while creating a new user and setting up permissions, the commands in your comment are not present in the postInstall phase?

V3ntus avatar Apr 13 '24 18:04 V3ntus

I'm assuming due to the need for sudo while creating a new user and setting up permissions, the commands in your comment are not present in the postInstall phase?

Those commands are just to get it working to iterate on in case anyone wants to do more work on this.

Ownership will probably come in when it's made into a service (see clamav for an example), as I do not believe the build generation has access to the other users/groups other than nixbld (at least it errored out when I tried to modify).

nealfennimore avatar Apr 13 '24 19:04 nealfennimore

Curious on opinions of how to approach translating declared options into the ossec.conf file. ClamAV is a simplistic key value pair while Wazuh utilizes XML [1].

Aside from copying and generating the entire sample ossec.conf within the nix expression, I'm attempting to explore other ways to go about this. I'm aware there's a toXML built-in, but the output is way verbose, I don't think it would work. I'm not sure if it would be worth overengineering that process using something like the json2xml Python tool. Another path to take would just be sed and replacing the default entries (required/essentials for the agent to function and register). Thoughts?

Sample, default ossec.conf agent config: https://github.com/wazuh/wazuh/blob/master/etc/ossec-agent.conf

V3ntus avatar Apr 14 '24 00:04 V3ntus

I drafted a WIP module that defines a wazuh-agent service. https://github.com/V3ntus/nixpkgs/commit/9878cd4e196424fe497303d20ef68fe4bfc578e8

~~I'm not quite sure how to test it, I've set my repo as the only channel, yet I can't get my config to see the service or the package. Probably just my lack of nix knowledge/experience, could also be syntax issues I introduced.~~ I pushed a commit that adds wazuh to the top-level all-packages.nix as well as the wazuh-agent module to module-list.nix, but I should've read the docs first

V3ntus avatar Apr 14 '24 02:04 V3ntus

Another path to take would just be sed and replacing the default entries (required/essentials for the agent to function and register). Thoughts?

Sample, default ossec.conf agent config: https://github.com/wazuh/wazuh/blob/master/etc/ossec-agent.conf

You might want to use pkgs.writeText and interpolate the config as a string. That should make it easier to append to later

let
  ipAddr = "127.0.0.1";
  ossecConf = pkgs.writeText "ossec.conf" ''
    <ossec_config>
      <client>
        <server>
          <address>${ipAddr}</address>
        </server>
        <config-profile>debian, debian8</config-profile>
        <crypto_method>aes</crypto_method>
      </client>
    </ossec_config>
  '';

nealfennimore avatar Apr 14 '24 03:04 nealfennimore

@nealfennimore Awesome, I saw your reply on Discourse but I'm just now finding time this weekend to dig into it. @V3ntus Good stuff on getting started on the service.

Glad this is moving forward!

sjdwhiting avatar Apr 15 '24 00:04 sjdwhiting

I debugged the service module a bit, can't figure out why it won't start with systemctl: image

The env is needed, else awk isn't found which is required to discover available wazuh daemons. Couldn't start with /usr/bin/env either

https://github.com/V3ntus/nixpkgs/blob/a4dff64404c23e9ac5d6c278c5478afe14a7115e/nixos/modules/services/security/wazuh-agent.nix#L54-L56

V3ntus avatar Apr 17 '24 03:04 V3ntus