# sepolgen -n nmap --application /usr/bin/nmap -u user_u -u staff_u Loaded plugins: fastestmirror Created the following files: /root/nmap_module/nmap.te # Type Enforcement file /root/nmap_module/nmap.if # Interface file /root/nmap_module/nmap.fc # File Contexts file /root/nmap_module/nmap_selinux.spec # Spec file /root/nmap_module/nmap.sh # Setup Script
# rm nmap.fc rm: remove regular file 'nmap.fc'? y # ./nmap.sh Building and Loading Policy + make -f /usr/share/selinux/devel/Makefile nmap.pp Compiling minimum nmap module /usr/bin/checkmodule: loading policy configuration from tmp/nmap.tmp /usr/bin/checkmodule: policy configuration loaded /usr/bin/checkmodule: writing binary representation (version 17) to tmp/nmap.mod Creating minimum nmap.pp policy package rm tmp/nmap.mod tmp/nmap.mod.fc + /usr/sbin/semodule -i nmap.pp ... # chcon -t nmap_exec_t /usr/bin/nmap
# chmod u+s /usr/bin/nmap # semodule -DB $ id user_u:user_r:user_t:s0 $ nmap -sS -A -PI jnode.in .... $ nmap -A -sU -PI jnode.in .... $ nmap jnode.in -o out.log ....
# audit2allow -bl -R -t nmap_t allow nmap_t self:capability {net_raw dac_read_search dac_override}; allow nmap_t self:unix_dgram_socket { create ioctl }; allow nmap_t self:packet_socket { bind create getopt ioctl read setopt write }; allow nmap_t self:rawip_socket { create setopt write }; corenet_tcp_connect_http_port(nmap_t) corenet_tcp_connect_smtp_port(nmap_t) corenet_tcp_connect_ssh_port(nmap_t) kernel_read_network_state(nmap_t) kernel_read_system_state(nmap_t) userdom_use_inherited_user_ptys(nmap_t)
policy_module(nmap, 1.0.0) ######################################## # # Declarations # attribute_role nmap_roles; roleattribute system_r nmap_roles; type nmap_t; type nmap_exec_t; application_domain(nmap_t, nmap_exec_t) role nmap_roles types nmap_t; # log files type type nmap_result_log_t; files_type(nmap_result_log_t) # home transition userdom_user_home_dir_filetrans(nmap_t, nmap_result_log_t, { dir file }) # permissive nmap_t; ######################################## # # nmap local policy # allow nmap_t self:process { setrlimit }; allow nmap_t self:fifo_file manage_fifo_file_perms; allow nmap_t self:unix_stream_socket create_stream_socket_perms; domain_use_interactive_fds(nmap_t) files_read_etc_files(nmap_t) auth_use_nsswitch(nmap_t) miscfiles_read_localization(nmap_t) sysnet_dns_name_resolve(nmap_t) optional_policy(` gen_require(` type user_t; role user_r; ') nmap_run(user_t, user_r) # nmap log files access manage_files_pattern(user_t, nmap_result_log_t, nmap_result_log_t) ') optional_policy(` gen_require(` type staff_t; role staff_r; ') nmap_run(staff_t, staff_r) # autogenerated macro from nmap.if # nmap log files access manage_files_pattern(staff_t, nmap_result_log_t, nmap_result_log_t) ') # from audit2allow allow nmap_t self:capability { dac_override net_raw dac_read_search }; allow nmap_t self:packet_socket { bind create getopt ioctl read setopt write }; allow nmap_t self:rawip_socket { create setopt write }; kernel_read_network_state(nmap_t) kernel_read_system_state(nmap_t) userdom_use_inherited_user_ptys(nmap_t) corenet_tcp_connect_all_ports(nmap_t) #nmap log files access manage_files_pattern(nmap_t, nmap_result_log_t, nmap_result_log_t)
# ./nmap.sh ....
$ nmap -A -PI -sX -p 53 jnode.in -o out.log Starting Nmap 6.40 ( http://nmap.org ) at 2018-05-13 13:29 CEST WARNING: Running Nmap setuid, as you are doing, is a major security risk. Nmap scan report for jnode.in (79.137.74.224) Host is up (0.012s latency). PORT STATE SERVICE VERSION 53/tcp open domain ISC BIND hostmaster ... $ ls -laZ out.log -rw-rw-r--. root user user_u:object_r:nmap_result_log_t:s0 out.log $ rm out.log $
# sepolicy interface -v -l|grep filetrans|grep user_home_dir userdom_user_home_dir_filetrans(domain, private_type, object_class, name) Create objects in a user home directory with an automatic type transition to a specified private type. # sepolicy interface -v -l|grep corenet_tcp_connect|grep all corenet_tcp_connect_all_ports(domain) Connect TCP sockets to all ports. # sepolicy interface -v -l|grep files_pattern|grep manage manage_files_pattern
# sepolicy transition -s user_t -t nmap_t user_t @ nmap_exec_t --> nmap_t
# sepolicy communicate -s user_t -t nmap_t xserver_tmpfs_t user_tmp_t user_fonts_t nmap_result_log_t
Source: https://habr.com/ru/post/354598/
All Articles