Skip to content

Commit a0dc7c5

Browse files
committed
add toc and cleanup excess spaces
1 parent 7fe078f commit a0dc7c5

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

Environment variables and how to get them.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33
Author: Pantelis Roditis <proditis[at]echothrust.com>
44
Date: 04/06/2021
55
```
6+
- [Environment variables and how to get them](#environment-variables-and-how-to-get-them)
7+
- [Introduction](#introduction)
8+
- [Why do we care](#why-do-we-care)
9+
- [How they work](#how-they-work)
10+
- [docker variables](#docker-variables)
11+
- [How to get them](#how-to-get-them)
12+
- [`printenv` - print all or part of environment](#printenv---print-all-or-part-of-environment)
13+
- [`env` - print the environment or run a program in a modified environment](#env---print-the-environment-or-run-a-program-in-a-modified-environment)
14+
- [`set` BUILTIN - show or set shell variables and functions](#set-builtin---show-or-set-shell-variables-and-functions)
15+
- [`declare` / `typeset` BUILTINS - declare variables and/or give them attributes](#declare--typeset-builtins---declare-variables-andor-give-them-attributes)
16+
- [ps](#ps)
17+
- [/proc](#proc)
18+
- [What next](#what-next)
19+
- [References](#references)
620

721
## Introduction
8-
> An **environment variable** is a named value that can be accessed and affect the way running processes will behave on a computer.
22+
> An **environment variable** is a named value that can be accessed and affect the way running processes will behave on a computer.
923
> - wikipedia
1024
1125
Think of variables as symbolic names we give to values in order to avoid remembering them. Say you use the number `3.1415926535897932384626433` quite often, and unless you are a robot it is going to be hard to remember the entire number by heart. So you give a name this number `pi=3.1415926535897932384626433` and now whenever you want to use it you simply use the symbolic name `pi`, instead of the huge number sequence.
1226

1327
In this document we will outline the use of environment variables, how they can be accessed, manipulated and taking advantage of them for both offensive and defensive security engineers.
1428

1529
## Why do we care
16-
So why are environment variables important from a security perspective?
30+
So why are environment variables important from a security perspective?
1731

18-
It used to be (in the old days lol) that environment variables where visible by everyone and as a rule most developers didn't use them to hold sensitive information.
32+
It used to be (in the old days lol) that environment variables where visible by everyone and as a rule most developers didn't use them to hold sensitive information.
1933
However, as the adoption of environment variables grew, so was the need to start holding a bit more sensitive information. So the ability to hide the environment variables from other users and processes was added to some, if not all, UNIX and UNIX-like systems (such as Linux) and thus limiting their attack surface.
2034

2135
Now, we've moved into the container era, environment variables got a new meaning. Have a quick look at docker hub and you'll see millions of images that use environment variables that hold sensitive information. From username, passwords, encryption keys, authentication tokens, system keys, there is an image with an environment variable use to match any imagination...
@@ -48,8 +62,8 @@ $ echo $mystring
4862
$
4963
```
5064

51-
The same will happen if you open another terminal and try to access the variable. So why is that?
52-
The variables have effect only on the current session. In order for a variable to be available on subsequent commands and sessions that are spawned from the existing one, we have to `export` them.
65+
The same will happen if you open another terminal and try to access the variable. So why is that?
66+
The variables have effect only on the current session. In order for a variable to be available on subsequent commands and sessions that are spawned from the existing one, we have to `export` them.
5367
```bash
5468
$ export mystring
5569
$ bash
@@ -64,17 +78,17 @@ Understanding the concept of variable visibility is particularly important in si
6478

6579
_We will not go deep into docker specifics just as far as we need for understanding the variables._
6680

67-
Lets see an example with docker variables.
81+
Lets see an example with docker variables.
6882
```bash
69-
$ docker run -it -e "myvar=myvalue" bash
83+
$ docker run -it -e "myvar=myvalue" bash
7084
root@envlab: / # echo $myvar
7185
myvalue
72-
root@envlab: / #
86+
root@envlab: / #
7387
```
7488

7589
One thing we notice is that this variable is exported
7690
```bash
77-
root@envlab: / # bash
91+
root@envlab: / # bash
7892
root@envlab: / # echo $myvar
7993
myvalue
8094
```
@@ -84,7 +98,7 @@ Even if we start a login bash the variable is still there
8498
root@envlab: / # bash -l
8599
4df007c4e761:/# echo $myvar
86100
myvalue
87-
4df007c4e761:/#
101+
4df007c4e761:/#
88102
```
89103

90104
Even if we change users and shell, the variable is still there
@@ -115,7 +129,7 @@ TERM=xterm
115129
SHLVL=1
116130
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
117131
_=/bin/printenv
118-
root@envlab: / #
132+
root@envlab: / #
119133
```
120134

121135
### `env` - print the environment or run a program in a modified environment
@@ -139,8 +153,8 @@ root@envlab: / #
139153

140154
2. Reset the environment and run `/usr/bin/env` to print the environment and see that its empty
141155
```bash
142-
root@envlab: / # env -i /usr/bin/env
143-
root@envlab: / #
156+
root@envlab: / # env -i /usr/bin/env
157+
root@envlab: / #
144158
```
145159

146160
3. Reset the environment and run `/usr/local/bin/bash`. What we get is the variables that bash defines by default
@@ -150,7 +164,7 @@ root@envlab: / # env
150164
PWD=/
151165
SHLVL=1
152166
_=/usr/bin/env
153-
root@envlab: / #
167+
root@envlab: / #
154168
```
155169

156170
4. Run bash with modified `$HOME` variable
@@ -168,7 +182,7 @@ _=/usr/bin/env
168182
### `set` BUILTIN - show or set shell variables and functions
169183
Without options, the name and value of each shell variable are displayed in a format that can be reused as input for setting or resetting the currently-set variables. Read-only variables cannot be reset.
170184

171-
This is a builtin command that is used to define and/or print shell specific variables and functions.
185+
This is a builtin command that is used to define and/or print shell specific variables and functions.
172186

173187
Running just `set`, returns far more variables than before.
174188
```bash
@@ -224,7 +238,7 @@ root@envlab: / # /bin/ash
224238
HISTFILE='/root/.ash_history'
225239
HOME='/root'
226240
HOSTNAME='envlab'
227-
IFS='
241+
IFS='
228242
'
229243
LINENO=''
230244
OPTIND='1'
@@ -240,7 +254,7 @@ _='/bin/sh'
240254
_BASH_BASELINE='5.1'
241255
_BASH_LATEST_PATCH='8'
242256
_BASH_VERSION='5.1.8'
243-
/ #
257+
/ #
244258
```
245259

246260
An added bonus of using `set` is that it also displays functions that may have been defined
@@ -253,15 +267,15 @@ _=set
253267
_BASH_BASELINE=5.1
254268
_BASH_LATEST_PATCH=8
255269
_BASH_VERSION=5.1.8
256-
mytest ()
257-
{
270+
mytest ()
271+
{
258272
ls
259273
}
260274
root@envlab: / # mytest
261275
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
262276
```
263277
### `declare` / `typeset` BUILTINS - declare variables and/or give them attributes
264-
Declare variables and/or give them attributes. If no names are given then display the values of variables. This command has the ability to display and manipulate variables as well as their attributes (eg. int, array, exported, etc).
278+
Declare variables and/or give them attributes. If no names are given then display the values of variables. This command has the ability to display and manipulate variables as well as their attributes (eg. int, array, exported, etc).
265279

266280
Without arguments it operates just like `set`, displaying the shell and environment variables and functions
267281
```bash
@@ -301,9 +315,9 @@ This is what the declare attributes mean
301315
* `-u` When the variable is assigned a value, all lower-case characters are converted to upper-case
302316
* `-x` Mark names for export to subsequent commands via the environment
303317

304-
For more details about the `declare` command check the `bash(1)` manual pages under the **SHELL BUILTIN COMMANDS** section.
318+
For more details about the `declare` command check the `bash(1)` manual pages under the **SHELL BUILTIN COMMANDS** section.
305319

306-
The `typeset` command behaves very similar to `declare` and is mostly there for compatibility with other shells. However, `typeset` without options, returns all variables and builtin shell functions.
320+
The `typeset` command behaves very similar to `declare` and is mostly there for compatibility with other shells. However, `typeset` without options, returns all variables and builtin shell functions.
307321

308322
### ps
309323
The previous commands we saw all work on the current shell session, however many times we would like to see what environment variables were defined for an already running application, that may or may not have started by us.
@@ -346,13 +360,13 @@ As we can see, the first three processes belonging to the `root` and `sampleuser
346360
### /proc
347361
There is another way to get the environment from a running process and this is through `/proc`, the process information pseudo-filesystem.
348362

349-
The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at `/proc`, automatically by the system.
363+
The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at `/proc`, automatically by the system.
350364

351365
The filesystem provides and easy query to query and manipulate kernel structures as if they were simple files. For every process on the system, there is a corresponding directory under `/proc/<pid>/` with the exported kernel information. The files located under that folder correspond to different types of kernel information, but the ones that is of interest to us is `environ` & `cmdline`
352366

353-
`/proc/[pid]/environ`: This file contains the **initial** environment that was set when the currently executing program was started via `execve(2).` The entries are separated by null bytes (`\0`), and there may be a null byte at the end. Thus, to print out the environment of process 1, you would do
367+
`/proc/[pid]/environ`: This file contains the **initial** environment that was set when the currently executing program was started via `execve(2).` The entries are separated by null bytes (`\0`), and there may be a null byte at the end. Thus, to print out the environment of process 1, you would do
354368
```bash
355-
root@63f7264d374e:/# strings /proc/1/environ
369+
root@63f7264d374e:/# strings /proc/1/environ
356370
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
357371
HOSTNAME=envlab
358372
TERM=xterm
@@ -362,7 +376,7 @@ HOME=/root
362376

363377
`/proc/[pid]/cmdline`: This read-only file holds the complete command line for the process, unless the process is a zombie. In the latter case, there is nothing in this file: that is, a read on this file will return 0 characters. The command-line arguments appear in this file as a set of strings separated by null bytes (`\0`), with a further null byte after the last string.
364378
```bash
365-
root@63f7264d374e:/# strings /proc/1/cmdline
379+
root@63f7264d374e:/# strings /proc/1/cmdline
366380
/bin/bash
367381
/entrypoint.sh
368382
bash

0 commit comments

Comments
 (0)