Docsity

Bro Cheat Sheet for Linux Users, Cheat Sheet of Linux skills

Toggle sidebar
bg1
Bro2.1 Cheat Sheet
Version: December 13, 2012
Website: http://www.bro-ids.org
Download: https://github.com/broids/cheat-sheet
License: Attribution-NonCommercial-ShareAlike 3.0 Unported
Startup
bro [options] [file ...]
file . . . . . . . . . . . . . . . . . . . . .Bro policy script or stdin
-e code . . . . . . . . . . . . Augment policies by given code
-h . . . . . . . . . . . . . . . . . . . Display command line options
-i iface . . . . . . . . . . . . . . . . Read from given interface
-p pfx . . . . . . . . Add given prefix to policy resolution
-r file . . . . . . . . . . . . . . . . Read from given PCAP file
-w file . . . . . . . . Write to given file in PCAP format
-x file . . . . . . . . . . . . . . . . . Print contents of state file
-C . . . . . . . . . . . . . . . . . . . . . . . . . . Ignore invalid checksum
Language
Lowercase letters represent instance variables and up-
percase letters represent types. In general, xis an
instance of type Tand yan instance of type U. Argu-
ment names and record fields begin with a,b, . . . , and
zrepresents a default instance variable which takes on
the type of the right-hand side expression. For nota-
tional convenience, xcan often be replaced with an
expression of type T.
Variables
Constant qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . const
Constant redefinition . . . . . . . . . . . . redef x op expr
Scope qualifier . . . . . . . . . . . . . . . . . . . . . . . local,global
Declaration ............................scope x: T
Declaration & Definition . . . . . . . . . . scope z = expr
Declarations
Type ................................type name :T
Function . . . . . . . . . . . . . . function f(a: T, ...): R
Event .........................event e(a: T, ...)
Modules
Scriptimport .......................... @load path
Set current namespace to ns ............module ns
Export global symbols . . . . . . . . . . . . . export { ... }
Access module or enum namespace . . . . . . . . . . . . . T::a
Statements
Basic statement . . . . . . . . . . . . . . . . . . . . stmt ;or expr ;
Codeblock ........................{stmt ; ... }
Assignment ..............................z = expr
Function assignment . z = function(...): R{..}
Event queuing ....................... event e(...)
Event scheduling . . . schedule 10 secs { e(...) }
Print expression to stdout .............print expr
Branching
if (expr )
{ ... }
else if (expr )
{ ... }
else
{ ... }
Iteration
for (i in x)
{ ... }
Asynchronous
when (expr ) { ... }
when (local x = expr ) { ... }
Control
break
continue
next
return
Expressions
Operators
!..........................................Negation
$,?$ . . . . . . . . . . . . . Dereference, record field existence
+,-,*,/,%..............................Arithmetic
++,-- . . . . . . . . . . . . . . Post-increment, p ost-decrement
+=,-=,*=,/= . . . . . . . . . . . Arithmetic and assignment
==,!= . . . . . . . . . . . . . . . . . . . . . . . . . . Equality, inequality
<,<=,>=,>. . . . . . . . . . . . Less/greater than (or equal)
&&,|| . . . . . . . . . . . . . . . . . . . . . Conjunction, disjunction
in,!in . . . . . . . . . . . .Membership or pattern matching
[x] . . . . . . . . . . . . . . . . . . . . Index strings and containers
|x| Cardinality/size for addresses, strings, containers
f(...) ................................Function call
expr ?expr :expr . . . . . . . . . . . . Ternary if-then-else
Types
Basic
addr IP address (v4: 127.0.0.1, v6: [fe80::db15])
bool .............................Boolean flag (T,F)
count . . . . . . . . . . . . . . . . . . 64-bit unsigned integer (42)
double . . . . . . . Double-precision floating p oint (99.9)
int . . . . . . . . . . . . . . . . . . . . . . . 64-bit signed integer (-7)
interval . . . . Time interval (8 sec/min/hr/day[s])
pattern . . . . . . . . . Regular expression (/^br[oO])$/)
port . . . . . . . . Transport-layer p ort (22/tcp,53/udp)
string . . . . . . . . . . . . . . . . . . . . . . String of bytes ("foo")
subnet . . . . . . . . . . . CIDR subnet mask (10.0.0.0/8)
time . . . . . . . . . . . . Absolute epoch time (1320977325)
Enumerables
Declaration . . . . . . . . . . . . . . . . . . . . enum { FOO, BAR }
Assignment .........................scope x = FOO
Records
Declaration . . . . . . . . record { a: T, b: U, ... }
Constructor . . . . . . . . . . . . .record($a=x, $b=y, ...)
Assignment . . . . . . . . scope r = [$a=x, $b=y, ...]
Access .....................................z = r$a
Field assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . r$b = y
Deletion ................................delete r$a
Sets
Declaration .................................set[T]
Constructor ...........................set(x, ...)
Assignment . . . . . . . . . . . . . . . . .scope s={x,...}
Access ....................................z = s[x]
Insertion ..................................add s[x]
Deletion ..............................delete s[x]
Tables
Declaration ........................table[T] of U
Constructor . . . . . . . . . . . . . . . . . table([x] = y, ...)
Assignment . . . . . . . . . scope t = { [x] = y, ... }
Access ....................................z = t[x]
Insertion ..................................t[x] = y
Deletion ..............................delete t[x]
Vectors
Declaration ...........................vector of T
Constructor .......................vector(x, ...)
Assignment . . . . . . . . . . . . . . . . .scope v={x,...}
Access ....................................z = v[0]
Insertion ................................ v[42] = x
pf3
pf4
pf5

Partial preview of the text

Download Bro Cheat Sheet for Linux Users and more Cheat Sheet Linux skills in PDF only on Docsity!

Bro

Cheat Sheet

Version: December 13, 2012 Website: http://www.bro-ids.org Email: [email protected] Download: https://github.com/broids/cheat-sheet License: Attribution-NonCommercial-ShareAlike 3.0 Unported

Startup

bro [options] [file ...] file.................... .Bro policy script or stdin -e code............ Augment policies by given code -h................... Display command line options -i iface................ Read from given interface -p pfx........ Add given prefix to policy resolution -r file................ Read from given PCAP file -w file........ Write to given file in PCAP format -x file................. Print contents of state file -C.......................... Ignore invalid checksum

Language

Lowercase letters represent instance variables and up- percase letters represent types. In general, x is an instance of type T and y an instance of type U. Argu- ment names and record fields begin with a, b,... , and z represents a default instance variable which takes on the type of the right-hand side expression. For nota- tional convenience, x can often be replaced with an expression of type T.

Variables

Constant qualifier............................ const Constant redefinition............ redef x op expr Scope qualifier....................... local, global Declaration............................ scope x: T Declaration & Definition.......... scope z = expr

Declarations

Type................................ type name : T Function.............. function f(a: T, ...): R Event......................... event e(a: T, ...)

Modules

Script import.......................... @load path Set current namespace to ns............ module ns Export global symbols............. export { ... } Access module or enum namespace............. T::a

Statements

Basic statement.................... stmt ; or expr ; Code block........................ { stmt ; ... } Assignment.............................. z = expr Function assignment. z = function(...): R {..} Event queuing....................... event e(...) Event scheduling... schedule 10 secs { e(...) } Print expression to stdout............. print expr

Branching if (expr ) { ... } else if (expr ) { ... } else { ... }

Iteration for (i in x) { ... } Asynchronous when (expr ) { ... } when (local x = expr ) { ... }

Control break continue next return

Expressions

Operators !.......................................... Negation $, ?$............. Dereference, record field existence +, - , *, /, %............................. .Arithmetic ++, --.............. Post-increment, post-decrement +=, -=, *=, /=........... Arithmetic and assignment ==, !=.......................... Equality, inequality <, <=, >=, >............ Less/greater than (or equal) &&, ||..................... Conjunction, disjunction in, !in........... .Membership or pattern matching [x].................... Index strings and containers |x| Cardinality/size for addresses, strings, containers f(...)................................ Function call expr? expr : expr............ Ternary if-then-else

Types

Basic addr IP address (v4: 127.0.0.1, v6: [fe80::db15])

bool............................ .Boolean flag (T, F) count.................. 64-bit unsigned integer ( 42 ) double....... Double-precision floating point (99.9) int....................... 64-bit signed integer (-7) interval.... Time interval (8 sec/min/hr/day[s]) pattern......... Regular expression (/^br[oO])$/) port........ Transport-layer port (22/tcp, 53/udp) string...................... String of bytes ("foo") subnet........... CIDR subnet mask (10.0.0.0/8) time............ Absolute epoch time ( 1320977325 )

Enumerables Declaration.................... enum { FOO, BAR } Assignment......................... scope x = FOO

Records Declaration........ record { a: T, b: U, ... } Constructor............ .record($a=x, $b=y, ...) Assignment........ scope r = [$a=x, $b=y, ...] Access..................................... z = r$a Field assignment........................... r$b = y Deletion................................ delete r$a

Sets Declaration................................. set[T ] Constructor........................... set(x, ...) Assignment................ .scope s = { x, ... } Access.................................... z = s[x] Insertion................................. .add s[x] Deletion.............................. delete s[x]

Tables Declaration........................ table[T ] of U Constructor................. table([x] = y, ...) Assignment......... scope t = { [x] = y, ... } Access.................................... z = t[x] Insertion................................. .t[x] = y Deletion.............................. delete t[x]

Vectors Declaration........................... vector of T Constructor....................... vector(x, ...) Assignment................ .scope v = { x, ... } Access.................................... z = v[0] Insertion................................ v[42] = x

Attributes

Attributes occur at the end of type/event declarations and change their behavior. The syntax is &key or &key=val, e.g., type T: set[count] &read_expire=5min or event foo() &priority=-3.

&optional..................................... Allow record field to be missing &default=x......... Use default value x for record fields and container elements &redef............................. Allow for redefinition of initial object value &expire_func=f.................. .Call f right before container element expires &read_expire=x................ Remove element after not reading it for time x &write_expire=x............... Remove element after not writing it for time x &create_expire=x................. Remove element after time x from insertion &persistent..................... Write state to disk (per default on shutdown) &synchronized.............................. .Synchronize variable across nodes &raw_output........ Do not escape non-ASCII characters when writing to a file &mergeable............... Prefer set union to assignment for synchronized state &priority=x.. Execution priority of event handler, higher values first, default 0 &group="x"...... Events in the same group can be jointly activated/deactivated &log................................................... Write record field to log

Built-In Functions (BIFs)

Core

  • syslog(s: string) Send the string s to syslog.
  • system(s: string): int Invokes a command via the system function. Returns the return value from the system() call. The command is run in the background, stdout redirects to stderr. Here is a usage example: system(fmt("rm "%s"", str_shell_escape(sniffed_data)));
  • piped_exec(program: string, to_write: string): bool Opens the application program with popen and writes the string to_write to stdin of the opened program.
  • srand(seed: count) Sets the seed for subsequent rand calls.
  • rand(max: count): count Returns a random value from the interval [0, max).
  • md5_hash(...): string Computes the MD5 hash value of the provided list of arguments.
  • md5_hash_init(): opaque of md Retrieves an opaque handle for incremental MD5 hash computation. Add data with via md5_hash_update. - md5_hash_update(handle: opaque of md5, data: string): bool Feeds an incremental MD5 computation with data. Call md5_hash_finish(handle) to retrieve the final hash digest. - md5_hash_finish(handle: opaque of md5): string Returns the final MD5 digest of handle. This invalidates handle, i.e., the func- tion can only be called once. - md5_hmac(...): string Computes an HMAC-MD5 hash value of the provided list of arguments. The HMAC secret key is generated from available entropy when Bro starts up, or it can be specified for repeatability using the -K flag. - sha1_hash(...): string Computes the SHA1 hash value of the provided list of arguments. Analogous to md5_hash. - sha1_hash(...): string Computes the SHA1 hash value of the provided list of arguments. - sha1_hash_init(): opaque of sha Retrieves an opaque handle for incremental SHA1 hash computation. Add data with via sha1_hash_update. - sha1_hash_update(handle: opaque of sha1, data: string): bool Feeds an incremental SHA1 computation with data. Call sha1_hash_finish(handle) to retrieve the final hash digest. - sha1_hash_finish(handle: opaque of sha1): string Returns the final SHA1 digest of handle. This invalidates handle, i.e., the function can only be called once. - sha256_hash(...): string Computes the SHA256 hash value of the provided list of arguments. Analogous to md5_hash. - sha256_hash(...): string Computes the SHA256 hash value of the provided list of arguments. - sha256_hash_init(): opaque of sha Retrieves an opaque handle for incremental SHA256 hash computation. Add data with via sha256_hash_update. - sha256_hash_update(handle: opaque of sha256, data: string): bool Feeds an incremental SHA256 computation with data. Call sha256_hash_finish(handle) to retrieve the final hash digest. - sha256_hash_finish(handle: opaque of sha256): string Returns the final SHA256 digest of handle. This invalidates handle, i.e., the function can only be called once. - strftime(fmt: string, d: time): string Formats the time value d according to the format string fmt. See man strftime for the format of fmt. - lookup_addr(host: addr): string
  • all_set(v: any): bool Tests whether all elements of the boolean vector (vector of bool) are true, i.e., checks whether ∀x ∈ v : x = T. Missing elements count as false.
  • sort(v: any, ...): any Sorts the vector v in place and returns the original vector. The sec- ond argument is a comparison function that takes two arguments: if the type of v is vector of T, then the comparison function must be function(a: T, b: T): bool, which returns a < b for some type-specific no- tion of the less-than operator.
  • order(v: any, ...): vector of count Returns the order of the elements in the vector v according to some comparison function. See sort.

Math

  • floor(x: double): double Chops off any decimal digits of x, i.e., computes bxc.
  • sqrt(x: double): double Returns the square root of x, i.e., computes

x.

  • exp(x: double): double Raises e to the power of x, i.e., computes ex.
  • ln(x: double): double Returns the natural logarithm of x, i.e., computes ln x.
  • log10(x: double): double Returns the common logarithm of x, i.e., computes log 10 x.

String Processing

  • byte_len(s: string): count Returns the number of characters (i.e., bytes) in s. This includes any embedded NULs, and also a trailing NUL, if any (which is why the function isn’t called strlen; to remind the user that Bro strings can include NULs).
  • sub_bytes(s: string, start: count, n: int): string Extracts a substring of s, starting at position start and having length n.
  • split(s: string, re: pattern): table[count] of string Splits s into an array using re to separate the elements. The returned table starts at index 1. Note that conceptually the return value is meant to be a vector and this might change in the future.
  • split1(s: string, re: pattern): table[count] of string Same as split, but s is only split once (if possible) at the earliest position and an array of two strings is returned. An array of one string is returned when s cannot be split.
  • split_all(s: string, re: pattern): table[count] of string Same as split, but also include the matching separators, e.g.,

split_all("a-b--cd", /(-)+/) returns {"a", "-", "b", "--", "cd"}. Odd-indexed elements do not match the pattern and even-indexed ones do.

  • split_n(s: string, re: pattern, incl_sep: bool, max_num_sep: count): table[count] of string Similar to split1 and split_all, but incl_sep indicates whether to include matching separators and max_num_sep the number of times to split s.
  • str_split(s: string, idx: vector of count): vector of string Splits s into substrings, taking all the indices in idx as cutting points; idx does not need to be sorted and out-of-bounds indices are ignored.
  • string_cat(...): string Concatenes a variable number of string arguments into a single string.
  • cat_string_array(a: table[count] of string): string Same as string_cat, except that it takes an array of strings as argument and concatenates its values into a single string.
  • cat_string_array_n(a: table[count] of string, start: count, end: count): string Same as cat_string_array, but only concatenates the strings from index start to end.
  • join_string_array(sep: string, a: table[count] of string): string Concatenates all elements in a into a single string, with sep placed between each element.
  • join_string_vec(v: vector of string, sep: string): string Concatenates all elements in v into a single string, with sep placed between each element.
  • sort_string_array(a: table[count] of string): string Sorts the string array a and returns a sorted copy.
  • sub(s: string, re: pattern, repl: string): string Substitutes repl for the first occurrence of re in s.
  • gsub(s: string, re: pattern, repl: string): string Same as sub except that all occurrences of re are replaced.
  • strcmp(s1: string, s2: string): int Lexicographically compares s1 and s2. Returns an integer greater than, equal to, or less than 0 according as s1 is greater than, equal to, or less than s2.
  • strstr(big: string, little: string): count Locates the first occurrence of little in big. Returns 0 if little is not found in big.
  • subst_string(s: string, from: string, to: string): string Substitutes each (non-overlapping) appearance of from in s to to, and return the resulting string.
  • to_lower(s: string): string Returns a copy of the given string with the uppercase letters (as indicated by isascii and isupper) folded to lowercase (via tolower).
  • to_upper(s: string): string Returns a copy of s with the lowercase letters (as indicated by isascii and islower) folded to lowercase (via toupper).
  • is_ascii(s: string): bool Returns false if any byte value of s is greater than 127, and true otherwise.
  • edit(s: string, edit_char: string): string Returns a version of s assuming that edit_char is the “backspace char- acter” (usually \x08 for backspace or \x7f for DEL). For example, edit("hello there", "e") returns "llo t". The argument edit_char must be a string of exactly one character, or Bro generates a run-time error and uses the first character in the string.
  • clean(s: string): string Replaces non-printable characters in s with escaped sequences, with the map- pings NUL → \0, DEL → ^?, values ≤ 26 → ^[A-Z], and values not in [32, 126] → %XX. If the string does not yet have a trailing NUL, one is added.
  • to_string_literal(s: string): string Same as clean, but with different mappings: values not in [32, 126] → %XX, \ → \, ' → ', " → ".
  • escape_string(s: string): string Returns a printable version of s. Same as clean except that non-printable characters are removed.
  • string_to_ascii_hex(s: string): string Returns an ASCII hexadecimal representation of a string.
  • strip(s: string): string Strips whitespace at both ends of s.
  • string_fill(len: int, source: string): string Generates a string of size len and fills it with repetitions of source.
  • str_shell_escape(source: string): string Takes a string and escapes characters that would allow execution of commands at the shell level. Must be used before including strings in system or similar calls.
  • find_all(s: string, re: pattern): set of string Returns all occurrences of re in s (or an empty empty set if none).
  • find_last(s: string, re: pattern): string Returns the last occurrence of re in s. If not found, returns an empty string. Note that this function returns the match that starts at the largest index in the string, which is not necessarily the longest match. For example, a pattern of /.*/ will return the final character in the string.
  • hexdump(data: string): string Returns a hex dump for data. The hex dump renders 16 bytes per line, with hex on the left and ASCII (where printable) on the right. Based on Netdude’s hex editor code. - find_entropy(data: string): entropy_test_result Performs an entropy test on data. - entropy_test_init(): opaque of entropy Retrieves a data structures for incremental entropy calculation. Returns true on success. See entropy_test_add and entropy_test_finish. - entropy_test_add(handle: opaque of entropy, data: string): bool Adds data to the incremental entropy calculation identified by handle. Returns true on success. - entropy_test_finish(handle: opaque of entropy): entropy_test_result Finalizes the incremental entropy calculation identified by handle.

Network Type Processing

  • is_v4_addr(a: addr): bool Checks whether an address is IPv4. Returns true for IPv4 and false for IPv addresses.
  • is_v6_addr(a: addr): bool Checks whether an address is IPv6. Returns the opposite of is_v4_addr.
  • mask_addr(a: addr, top_bits_to_keep: count): subnet Returns the address a masked down to the number of upper bits indicated by top_bits_to_keep, which must be greater than 0 and less than 33. For example, mask_addr(1.2.3.4, 18) returns 1.2.0.0, and mask_addr(1.2.255.4, 18) returns 1.2.192.0.
  • remask_addr(a1: addr, a2: addr, top_bits_from_a1: count): count Takes some top bits (e.g., subnet address) from a1 and the other bits (intra- subnet part) from a2 and merges them to get a new address. This is useful for anonymizing at subnet level while preserving serial scans.
  • is_tcp_port(p: port): bool Checks whether p is a TCP port.
  • is_udp_port(p: port): bool Checks whether p is a UDP port.
  • is_icmp_port(p: port): bool Checks whether p is an ICMP port.
  • connection_exists(id: conn_id): bool Checks whether the connection identified by id is (still) active.
  • lookup_connection(id: conn_id): connection Returns the connection record for id. If id does not point to an existing connection, the function generates a run-time error and returns a dummy value.
  • unescape_URI(URI: string): string Unescapes all characters in URI, i.e., decodes every %xx group.
  • lookup_location(a: addr) : geo_location Performs a geo-lookup of the IP address a. Returns country, region, city, lati- tude, and longitude. Needs Bro to built with libgeoip.