ostree

Kali Linux LXC/LXD Images

Apr 16th, 2022 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. Content:
  2.  
  3. Overview
  4. Command line Kali LXD container on Ubuntu host
  5. Gui Kali LXD container on Ubuntu host
  6. Privileged Kali LXC container on Kali host
  7. Unprivileged Kali LXC container on Kali host
  8. References
  9.  
  10. Overview
  11.  
  12. Kali Linux containers are the ideal solution to
  13.  
  14. run Kali Linux within other Linux distributions
  15. provide isolated environments for development or testing activities
  16.  
  17. without the overhead of virtual machines. Docker is the preferred solution for applications whilst LXC/LXD are preferred for entire systems.
  18.  
  19. Linux containers provide features like snapshots and freezing which comes in very handy when developing or testing software.
  20.  
  21. Kali images are available on the image server for LXC and LXD and can easily be launched either in LXD using the “images:” image server or in LXC using the “lxc-download” template.
  22.  
  23. LXC is a userspace interface for the Linux kernel containment features. Through a powerful API and simple tools, it lets Linux users easily create and manage system or application containers.
  24.  
  25. LXD is a next generation system container manager. It offers a user experience similar to virtual machines but using Linux containers instead. It’s image based with pre-made images available for a wide number of Linux distributions and is built around a very powerful, yet pretty simple, REST API.
  26.  
  27. LXD vs LXC:
  28.  
  29. LXD is the more convenient of the two but is only available in Ubuntu or other distributions (such as Kali) as snap package.
  30.  
  31. LXC is available in more distributions and preferred in Kali as it is supported natively and does not required snapd to be running.
  32. Command line Kali LXD container on Ubuntu host
  33.  
  34. Installing a Kali Linux container in Ubuntu only requires a few steps:
  35.  
  36. Install LXD
  37. Launch a Kali container
  38. Install additional packages inside the container
  39. Create non-root user
  40. Login
  41.  
  42. 1 - Install lxd via snap and perform initial setup:
  43.  
  44. kali@kali:~$ sudo snap install lxd
  45. kali@kali:~$ lxd init
  46.  
  47. 2 - Launch your first Kali Linux container with
  48.  
  49. kali@kali:~$ lxc launch images:kali/current/amd64 my-kali
  50.  
  51. 3 - Install additional packages inside the container via
  52.  
  53. kali@kali:~$ xc exec my-kali -- apt update
  54. kali@kali:~$ xc exec my-kali -- apt install -y kali-linux-default kali-desktop-xfce
  55.  
  56. 4 - Create non-root user - “kali” in this example:
  57.  
  58. kali@kali:~$ lxc exec my-kali -- adduser kali
  59. kali@kali:~$ lxc exec my-kali -- usermod -aG sudo kali
  60. kali@kali:~$ lxc exec my-kali -- sed -i '1 i\TERM=xterm-256color' /home/kali/.bashrc
  61. kali@kali:~$ lxc exec my-kali -- sh -c "echo 'Set disable_coredump false' > /etc/sudo.conf"
  62.  
  63. 5 - Login to the new container as user “kali” via
  64.  
  65. kali@kali:~$ lxc console my-kali
  66.  
  67. Voila!
  68. Container management:
  69.  
  70. Start: lxc start my-kali
  71. Stop: lxc stop my-kali
  72. Remove: lxc destroy my-kali
  73.  
  74. GUI Kali LXD container on Ubuntu host
  75.  
  76. Installing a Kali container to run GUI applications is similar to the previous example with a few additional steps:
  77.  
  78. Install LXD
  79. Create GUI profile and launch a Kali GUI container
  80. Install additional packages inside the container
  81. Create non-root user
  82. Start Kali Xfce panel
  83. Customise Kali Xfce panel
  84.  
  85. 1 - Install lxd via snap and perform initial setup (if not already done):
  86.  
  87. kali@kali:~$ sudo snap install lxd
  88. kali@kali:~$ lxd init
  89.  
  90. 2 - Launch your first Kali Linux container with
  91.  
  92. kali@kali:~$ wget https://blog.simos.info/wp-content/uploads/2018/06/lxdguiprofile.txt
  93. kali@kali:~$ lxc profile create gui
  94. kali@kali:~$ cat lxdguiprofile.txt | lxc profile edit gui
  95. kali@kali:~$ lxc profile list
  96. kali@kali:~$ lxc launch --profile default --profile gui images:kali/current/amd64 gui-kali
  97. kali@kali:~$ lxc launch images:kali/current/amd64 my-kali
  98.  
  99. 3 - Install additional packages inside the container via
  100.  
  101. kali@kali:~$ lxc exec my-kali -- apt update
  102. kali@kali:~$ lxc exec my-kali -- apt install -y kali-linux-default
  103.  
  104. 4 - Create non-root user - “kali” in this example:
  105.  
  106. kali@kali:~$ lxc exec my-kali -- adduser kali
  107. kali@kali:~$ lxc exec my-kali -- usermod -aG sudo kali
  108. kali@kali:~$ lxc exec my-kali -- sed -i '1 i\TERM=xterm-256color' /home/kali/.bashrc
  109. kali@kali:~$ lxc exec my-kali -- echo "export DISPLAY=:0" >> /home/kali/.bashrc
  110. kali@kali:~$ lxc exec my-kali -- sh -c "echo 'Set disable_coredump false' > /etc/sudo.conf"
  111.  
  112. 5 - Start Kali Xfce panel via
  113.  
  114. kali@kali:~$ lxc exec gui-kali -- sudo -u kali xfce4-panel
  115.  
  116. Customise the panel as desired.
  117. Container management:
  118.  
  119. Start: lxc start my-kali
  120. Stop: lxc stop my-kali
  121. Remove: lxc destroy my-kali
  122.  
  123. Privileged Kali LXC container on Kali host
  124.  
  125. Privileged containers are containers created by root and running as root. They are quicker to setup than unprivileged containers but are inherently unsafe. Installing a privileged Kali Linux container on a Kali host only requires to:
  126.  
  127. Install and setup lxc
  128. Download the kali image from the image server
  129. Start the container
  130. Attach to the container
  131.  
  132. 1 - Install lxc and setup the network:
  133.  
  134. kali@kali:~$ sudo apt install -y lxc libvirt0 libpam-cgfs bridge-utils libvirt-clients libvirt-daemon-system iptables ebtables dnsmasq-base
  135. kali@kali:~$
  136. kali@kali:~$ sudo cat <<EOF > /etc/lxc/default.conf
  137. lxc.net.0.type = veth
  138. lxc.net.0.link = virbr0
  139. lxc.net.0.flags = up
  140. lxc.apparmor.profile = generated
  141. lxc.apparmor.allow_nesting = 1
  142. EOF
  143. kali@kali:~$
  144. kali@kali:~$ sudo virsh net-start default
  145. kali@kali:~$ sudo virsh net-autostart default
  146.  
  147. 2 - Download the Kali Linux image from the image server via
  148.  
  149. kali@kali:~$ lxc-create -t download -n my-kali
  150.  
  151. This will list all available images.
  152.  
  153. When prompted, enter:
  154.  
  155. Distribution: kali
  156. Release: current
  157. Architecture: amd64 (or other as applicable)
  158.  
  159. 3 - Start the container with
  160.  
  161. kali@kali:~$ sudo lxc-start -n my-kali -d
  162.  
  163. 4 - Attach to the container via
  164.  
  165. kali@kali:~$ sudo lxc-attach -n my-kali
  166.  
  167. There you have it. Next you should set a root password and install the “kali-linux-default” metapackage.
  168. Container management:
  169.  
  170. Start: sudo lxc-start -n my-kali -d
  171. Stop: sudo lxc-stop -n my-kali
  172. List: sudo lxc-ls -f
  173. Info: sudo lxc-info -n my-kali
  174. Remove: sudo lxc-destroy -n my-kali
  175.  
  176. Unprivileged Kali LXC container on Kali host
  177.  
  178. Unprivileged containers run in a user context and are considered safer and are preferred over using privileged container. The setup it slightly more involved:
  179.  
  180. Install and setup lxc
  181. Setup LXC for unprivileged containers
  182. Download the kali image from the image server
  183. Start the container
  184. Install some additional packages
  185. Create non-root user
  186. Login
  187.  
  188. 1 - Install lxc (if required):
  189.  
  190. kali@kali:~$ sudo apt install -y lxc libvirt0 libpam-cgfs bridge-utils libvirt-clients libvirt-daemon-system iptables ebtables dnsmasq-base
  191.  
  192. 2 - Setup LXC for unprivileged containers
  193.  
  194. kali@kali:~$ echo "$USER veth virbr0 10" | sudo tee -i /etc/lxc/lxc-usernet
  195. kali@kali:~$ sudo sh -c 'echo "kernel.unprivileged_userns_clone=1" > /etc/sysctl.d/80-lxc-userns.conf'
  196. kali@kali:~$ sudo sysctl kernel.unprivileged_userns_clone=1
  197. kali@kali:~$ sudo chmod u+s /usr/libexec/lxc/lxc-user-nic
  198. kali@kali:~$
  199. kali@kali:~$ mkdir -p ~/.config/lxc
  200. kali@kali:~$ cp /etc/lxc/default.conf ~/.config/lxc/default.conf
  201. kali@kali:~$ sed -i 's/lxc.apparmor.profile = generated/lxc.apparmor.profile = unconfined/g' ~/.config/lxc/default.conf
  202.  
  203. Next we have to add two lines into ~/.config/lxc/default.conf whose subuid & subguid match those listed in /etc/subuid and /etc/subgid. First let’s get the id’s via cat /etc/s*i d grep $USER The result should look like this:
  204.  
  205. kali:100000:65536
  206. kali:100000:65536
  207.  
  208. Substitute the ID’s in the following commands with the ones in the previous output:
  209.  
  210. kali@kali:~$ echo lxc.idmap = u 0 100000 65536 >> ~/.config/lxc/default.conf
  211. kali@kali:~$ echo lxc.idmap = g 0 100000 65536 >> ~/.config/lxc/default.conf
  212.  
  213. 3 - Download the Kali Linux image from the image server via
  214.  
  215. kali@kali:~$ lxc-create -t download -n my-kali
  216.  
  217. This will list all available images.
  218.  
  219. When prompted, enter:
  220.  
  221. Distribution: kali
  222. Release: current
  223. Architecture: amd64 (or other as applicable)
  224.  
  225. 4 - Start the container with
  226.  
  227. kali@kali:~$ lxc-start -n my-kali -d
  228.  
  229. But before we login, we perform some post-installation setup tasks
  230.  
  231. 5 - Install default packages:
  232.  
  233. kali@kali:~$ lxc-attach -n my-kali apt update
  234. kali@kali:~$ lxc-attach -n my-kali apt install -y kali-linux-default
  235.  
  236. 6 - Create a non-root user:
  237.  
  238. kali@kali:~$ lxc-attach -n my-kali --clear-env adduser <username>
  239. kali@kali:~$ lxc-attach -n my-kali --clear-env adduser <username> sudo
  240.  
  241. 7 - Login as non-root user via
  242.  
  243. kali@kali:~$ lxc-console
  244.  
  245. And perform the following on initial login to get some colors in the console:
  246.  
  247. kali@kali:~$ sed -i '1 i\TERM=xterm-256color' ~/.bashrc
  248. kali@kali:~$ . ~/.bashrc
  249.  
  250. Container management:
  251.  
  252. Start: sudo lxc-start -n my-kali -d
  253. Stop: sudo lxc-stop -n my-kali
  254. List: sudo lxc-ls -f
  255. Info: sudo lxc-info -n my-kali
  256. Remove: sudo lxc-destroy -n my-kali
Advertisement
Add Comment
Please, Sign In to add comment