.htaccess is a per-folder configuration file for Apache and for LiteSpeed, which reads the same syntax. The server applies it before handing the request to PHP, so a rule in it decides what happens to a visitor before WordPress — and every WordPress security plugin — has been loaded. Anyone who can write a file in the site’s folders can change it; no server access or control-panel login is needed.
WordPress itself writes one block into the root .htaccess, between # BEGIN WordPress and # END WordPress, and rewrites that block when permalinks are saved. Everything outside those markers is somebody else’s: another plugin’s documented block (caching and security plugins add their own, also with BEGIN and END markers), your host’s, your developer’s — or an attacker’s. Malicious rules are usually placed above the WordPress block, so they run first.
If your site runs on Nginx only, .htaccess files are ignored and none of this applies — but the files are still worth reading, because their presence tells you what the attacker expected, and many hosts put Apache or LiteSpeed behind an Nginx front end.