Running kali linux on any linux distro using docker

Installing docker

You can follow this link to install docker on your machine

Install kali in docker

Run this command to pull kali images

1
docker pull kalilinux/kali-linux-docker

Run this command

1
docker run -Pti --privileged -e DISPLAY=:1.0 --device /dev/snd -v /tmp/.X11-unix:/tmp/.X11-unix -v /home/vuquangtam/Docker/Share:/share --net=host --name kali kalilinux/kali-linux-docker

to create a new container. It will return a shell to interact with. By default, there are no tools added to this image, so we must install additional toolpack, you can follow this link to choose your suitable one.

After create container. you can check containter id by run this command

1
docker ps -a

To start a container, run

1
docker start [container's id]

Adding GUI

You can install any desktop environment that kali support. In my case, i’ll install XFCE because it’s lightweight. Run this command to install XFCE in your docker container

1
apt-get install kali-defaults kali-root-login desktop-base xfce4 xfce4-places-plugin xfce4-goodies

Run Docker GUI in new xsession

If you want to run in new window in current xsession. you need to install Xephyr

1
apt-get install xserver-xephyr

I wrote a small script to run. You just need to copy it to /usr/bin for convenient. The script need a root priviledge to stop process.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
container="kali"
init_desktop () {
if [[ $1 = "window" ]]; then
nohup Xephyr -ac -br -noreset -screen 1280x600 -resizeable :1 &> /dev/null &
sleep 1
else
if [ ! -f /tmp/.X1-lock ]; then
nohup xinit -- :1 vt9 &> /dev/null &
sleep 4
fi
fi
}
stop_desktop () {
nohup kill $(pgrep xinit) &> /dev/null
nohup kill $(pgrep Xephyr) &> /dev/null
}
case "$1" in
"start")
echo "Starting..."
init_desktop $2
nohup docker start $container &> /dev/null
docker exec -it -d --privileged kali x-session-manager
;;
"run")
echo "Running $2..."
xhost + &> /dev/null
docker run -ti --rm --privileged --device /dev/snd -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v /home/vuquangtam/Docker/Share:/share -p 8080:8080 --net=host vuquangtam/kali $2
;;
"shell")
nohup docker start $container &> /dev/null
docker exec -it --privileged kali bash
;;
"stop")
echo "Stopping..."
nohup docker stop $container &> /dev/null
stop_desktop
;;
"restart")
echo "Restarting..."
nohup docker restart $container &> /dev/null
docker exec -it -d --privileged kali x-session-manager
;;
"pause")
echo "Pausing..."
nohup docker pause $container &> /dev/null
stop_desktop
;;
"resume" | "unpause")
echo "Resuming..."
init_desktop $2
nohup docker unpause $container &> /dev/null
;;
*)
;;
esac

Some examples:

1
2
3
4
5
sudo nameOfScript start window # to run a new window in current xsession
sudo nameOfScript start # to start new session in vt9 (press Ctrl+Alt+F9 to switch in Ubuntu/Linux Mint)
sudo nameOfScript stop
sudo nameOfScript restart
sudo nameOfScript run wireshark

SSH in Emacs with TRAMP

I use emacs a lot, sometimes I work on another machine though ssh to edit file. In emacs, you can use TRAMP to do EVERYTHING in remote machine.

Syntax

1
C-x C-f /protocol:user@remote_address#port:/path/to/file
  • protocol : ssh, scp, ftp and even docker
  • user : username to remote in remote machine
  • remote_address : it can be ip address or domain name of remote address
  • port : port of protocol

Example

1
C-x C-f /ssh:root@localhost:/home

When the connection is established, emacs will open a dired - emacs’s file manager for you to interact with remote machine. You can edit file and save, you can open terminal, you can copy file from local machine to remote machine and vice versa.

TRAMP dired mode

If you haven’t use this before, don’t worry, just remember some following keys to use:

  • enter or f : enter directory or open file
  • D : delete
  • C : copy
  • R : move
  • d : change state to delete
  • m : mark
  • u : unmark
  • U : unmark all
  • x : execute
  • ^ : back to parent directory

To open terminal, just call “shell” command

1
M-x shell