Files
bitburner-src/markdown/bitburner.ns.hack.md
David Walker b51ed8fd59 BUG: Fix missed cases in offline server handling (#2495)
There were two large holes in the existing offline server handling:

1. It didn't include IPs, so scripts that used IPs instead of hostnames
   would get exceptions thrown for "server not found."
2. Coverage was very low for non-Darknet APIs. Maybe most of them don't
   need to be covered, but many obvious ones like "ps", "killall" and
   "hasRootAccess" were missing. IMO the only reliable answer is one
   that enforces *all* are covered via the type system.

To accomplish the second part, helpers.getServer() was changed to return
null when a server is offline. This intentionally breaks a lot of its
utility, which was to return a server unconditionally. To compensate,
its utility was increased - it now also does unknown argument
processing, allowing it to subsume a common line that all callers were
repeating.

Some callers switched to ctx.workerScript.getServer(), because they
didn't actually need to be using helpers.getServer(). Similarly, a few
callsites switched to GetServerOrThrow(), for the cases where it should
be guaranteed that the server is valid. The rest are returning a
default/failure response when the server is offline. (Except for
contracts, which threw on failure already anyway.)
2026-02-15 10:29:47 -08:00

1.8 KiB
Raw Permalink Blame History

Home > bitburner > NS > hack

NS.hack() method

Steal a server's money.

Signature:

hack(host?: string, opts?: BasicHGWOptions): Promise<number>;

Parameters

Parameter

Type

Description

host

string

(Optional) Hostname/IP of the target server to hack. Optional. Defaults to current server if not provided.

opts

BasicHGWOptions

(Optional) Optional parameters for configuring function behavior.

Returns:

Promise<number>

A promise that resolves to the amount of money stolen (which is zero if the hack is unsuccessful).

Remarks

RAM cost: 0.1 GB

Function that is used to try and hack servers to steal money and gain hacking experience. The runtime for this command depends on your hacking level and the target servers security level when this function is called. In order to hack a server you must first gain root access to that server and also have the required hacking level.

A script can hack a server from anywhere. It does not need to be running on the same server to hack that server. For example, you can create a script that hacks the foodnstuff server and run that script on any server in the game.

A successful hack() on a server will raise that servers security level by 0.002 per thread. You can use hackAnalyzeSecurity to calculate the security increase for a number of threads.

Example

let earnedMoney = await ns.hack("foodnstuff");