How to Use AppArmor to Secure Applications in Ubuntu

Install AppArmor

AppArmor is a Linux kernel security module that allows you to restrict programs' capabilities with per-program profiles. To install AppArmor on Ubuntu, open a terminal window and type:

sudo apt-get install apparmor apparmor-utils
This will install the AppArmor packages and all of their dependencies. Once the installation is complete, you can enable AppArmor by typing:
sudo aa-enforce /etc/apparmor.d/*
This will enable AppArmor and load all of the profiles in the /etc/apparmor.d/ directory. You can also use the Ubuntu AppArmor documentation for more information on how to use AppArmor.

Enable AppArmor

Once AppArmor is installed, it needs to be enabled. To do this, open the terminal and type sudo aa-enforce /etc/apparmor.d/*. This will enable AppArmor and apply the profiles to all applications. To reload AppArmor, type sudo service apparmor reload. This will reload the profiles and apply any changes that have been made. To test the profile, type sudo aa-status. This will show the status of AppArmor and any profiles that have been loaded. If the profile is working correctly, it should show that AppArmor is enabled and the profile is active. For more information on AppArmor, visit the Ubuntu Wiki.

Create a Profile

Creating a profile for AppArmor is the next step in securing applications in Ubuntu. To create a profile, you need to use the aa-genprof command. This command will generate a profile for the application you specify. To create a profile, open a terminal and type the following command:

aa-genprof <application>
Replace <application> with the name of the application you want to secure. For example, to create a profile for the Apache web server, type the following command:
aa-genprof apache2
This command will start the profile generation process. You will be asked to answer a few questions about the application. Answer the questions as accurately as possible. Once the profile is generated, you can enable it and test it.

Reload AppArmor

Once you have created a profile, you need to reload AppArmor to apply the changes. To do this, open a terminal window and type the following command: sudo /etc/init.d/apparmor reload. This will reload AppArmor and apply the changes you have made. You can also use the aa-status command to check the status of AppArmor and make sure that the profile is loaded correctly. If you encounter any errors, you can use the aa-logprof command to troubleshoot the issue. For more information on AppArmor, you can refer to the official Ubuntu AppArmor Wiki.

Test the Profile

Once you have created a profile, you can test it to make sure it is working correctly. To do this, you can use the aa-status command. This command will show you the status of all the profiles that are currently loaded. If your profile is loaded correctly, it should show up in the list. You can also use the aa-logprof command to view the log of any changes that have been made to the profile. This can be useful for troubleshooting any issues that may arise.

You can also use the aa-enforce command to test the profile. This command will run the profile in enforce mode, which means that any violations of the profile will be logged. This can be useful for testing the profile and making sure that it is working correctly. Once you have tested the profile, you can use the aa-disable command to disable it.

Finally, you can use the aa-audit command to audit the profile. This command will generate a report of any violations that have occurred. This can be useful for identifying any potential security issues that may exist in the profile. For more information on using AppArmor to secure applications in Ubuntu, you can refer to the official Ubuntu documentation.

Useful Links