pastebin - collaborative debugging tool
kpaste.net RSS


x ray penis
Posted by Anonymous on Sun 11th Dec 2011 10:43
raw | new post

  1. HowTo: UFW ) Block Outgoing Ports Except Those Needed + More in Ubuntu 11.10
  2.  
  3. Contents -
  4.  
  5. Part 1: (. Novice .) - Block Outgoing Ports Except Those Needed
  6.                        allow: 20-21, 53, 80, 123, 443 outgoing only
  7. Part 2: (. Moderate .) - Sysctl: configure kernel parameters at runtime
  8. Part 3: (. Moderate .) - Configuring before*.rules
  9. Part 4: (. Advanced .) - Blocking Private Networks
  10. ###################################################
  11.  
  12. Part 1: (. Novice .) - Block Outgoing Ports Except Those Needed
  13.                        allow: 20-21, 53, 80, 123, 443 outgoing
  14.  
  15. I have tested this at the command line and it works. Here
  16. are the instructions on how to block outgoing ports except
  17. those specified using ufw at the command line. This guide
  18. assumes you have previously modified /etc/ufw/ufw.conf
  19. to enable auto-launching on system startup and ufw is
  20. running.
  21.  
  22. This configuration will allow the following outbound ports:
  23. 20-21, 53, 80, 123, 443 which is all that is required for
  24. many users. The outbound port mapping may be customized
  25. by you for your setup if it's your desire for allowing
  26. other applications using different ports. This guide
  27. does not cover configuration of apps which would reside
  28. in /etc/ufw/applications.d
  29.  
  30. 1. Open a Terminal window
  31. 2. With ufw started and configured for system startup
  32. with the default inbound deny, begin:
  33.  
  34. sudo ufw deny out 1:19/tcp
  35. sudo ufw deny out 1:19/udp
  36. sudo ufw deny out 22:52/tcp
  37. sudo ufw deny out 22:52/udp
  38. sudo ufw deny out 53:79/tcp
  39. sudo ufw deny out 53:79/udp
  40. sudo ufw deny out 81:122/tcp
  41. sudo ufw deny out 81:122/udp
  42. sudo ufw deny out 124:442/tcp
  43. sudo ufw deny out 124:442/udp
  44. sudo ufw deny out 444:65535/tcp
  45. sudo ufw deny out 444:65535/udp
  46.  
  47. 3. Check your work in one or two ways:
  48.  
  49. sudo ufw status verbose
  50. sudo ufw status numbered
  51.  
  52. Configuration is complete. To test this configuration
  53. you may start applications requiring the use of another
  54. port, such as a torrent application and when it fails
  55. to function, your leak test is a success. If you prefer
  56. retaining the above configuration, you may customize
  57. applications which allow it to use ports 80 or 443 to
  58. function. Or, you may prefer to redo the above differently
  59. with your own port range to allow for ports you need
  60. open.
  61.  
  62. I wrote this post because I couldn't find the information
  63. on-line on blocking outbound, or the information found was
  64. in error for the current version of Ubuntu 11.10. Or, there
  65. were posts where users *wanted* this functionality but
  66. people would post back unhelpful information in different
  67. ways, including but not limited to, "You don't need to
  68. do this." Yes, some would like this functionality, otherwise
  69. they wouldn't have asked for the information!
  70.  
  71. When you've finished using the sudo command in your
  72. Terminal, close it out with:
  73.  
  74. sudo -K
  75.  
  76. followed by:
  77.  
  78. exit
  79.  
  80. If you're continuing to use sudo for other operations
  81. at the command line, don't type sudo -K until you've
  82. finished.
  83.  
  84. One example of an application which may be customized for
  85. this setup is Vidalia/Tor:
  86.  
  87. - Open Vidalia's Control Panel and click on Settings.
  88. - Now click on the Network Icon.
  89. - Next, click the box which says, "My firewall only lets
  90. me connect to certain ports - Firewall Settings", from
  91. here it should say 80,443 by default, you're done here,
  92. click OK.
  93.  
  94. When you reload Vidalia/Tor, it will have
  95. written those port settings to the Tor configuration
  96. file and it will launch using the above two ports only.
  97.  
  98. This is very useful when running Tor if you want an
  99. outbound blocking policy in ufw, as Tor by default
  100. connects to several different ports and it would be
  101. impossible to configure them all, as they change
  102. per Tor node(s).
  103.  
  104. ###################################################
  105.  
  106. Part 2: (. Moderate .) - Sysctl: configure kernel parameters at runtime
  107.  
  108. This is interesting in Ubuntu 11.10, as Sysctl is
  109. found/referenced in three different locations:
  110.  
  111. /etc/sysctl.conf
  112. /etc/sysctl.d/ (contains a few files)
  113. /etc/ufw/sysctl.conf
  114.  
  115. Within /etc/ufw/sysctl.conf it reads:
  116.  
  117. "Please note these settings override /etc/sysctl.conf and
  118. /etc/sysctl.d. If you prefer to use /etc/sysctl.conf,
  119. please adjust IPT_SYSCTL in /etc/default/ufw."
  120.  
  121. Let's start by modifying /etc/default/ufw, use one
  122. of the two options, nano if you're comfortable
  123. with using nano, or gedit if you'd rather use a graphical
  124. editor:
  125.  
  126. For nano copy/paste: sudo nano /etc/default/ufw
  127. For gedit copy/paste: gksudo gedit /etc/default/ufw
  128.  
  129. Modify the following section to match this value:
  130.  
  131. # IPT backend
  132. # only enable if using iptables backend
  133. IPT_SYSCTL=/etc/sysctl.conf
  134.  
  135. Save document and exit.
  136.  
  137. We've changed the default setting to specify
  138. the use of /etc/sysctl.conf here.
  139.  
  140. Now we modify the /etc/sysctl.conf file. Start
  141. the editor you wish to use, nano or gedit:
  142.  
  143. sudo nano /etc/sysctl.conf
  144. gksudo gedit /etc/sysctl.conf
  145.  
  146. * Uncomment (remove the '#' before each line) the
  147. following sections: (these are my recommended settings)
  148. If you prefer, you could simply copy/paste these
  149. lines into /etc/sysctl.conf rather than hunting down
  150. each section for uncommenting, it's faster:
  151.  
  152. kernel.printk = 3 4 1 3
  153. net.ipv4.conf.default.rp_filter=1
  154. net.ipv4.conf.all.rp_filter=1
  155. net.ipv4.tcp_syncookies=1
  156. net.ipv4.conf.all.accept_redirects = 0
  157. net.ipv6.conf.all.accept_redirects = 0
  158. net.ipv4.conf.all.send_redirects = 0
  159. net.ipv4.conf.all.accept_source_route = 0
  160. net.ipv6.conf.all.accept_source_route = 0
  161. net.ipv4.conf.all.log_martians = 1
  162.  
  163. * Copy/paste the following into /etc/sysctl.conf:
  164.  
  165. #from /etc/sysctl.d
  166. kernel.kptr_restrict = 1
  167. kernel.yama.ptrace_scope = 1
  168. vm.mmap_min_addr = 65536
  169.  
  170. * Copy/paste the following into /etc/sysctl.conf:
  171.  
  172. #from /etc/ufw/ directory
  173. net.ipv4.icmp_echo_ignore_broadcasts=1
  174. net.ipv4.icmp_ignore_bogus_error_responses=1
  175. net.ipv4.icmp_echo_ignore_all=0
  176.  
  177. Save the document and exit, now copy/paste at
  178. the command line:
  179.  
  180. sudo sysctl -p
  181.  
  182. and you're done with the sysctl configuration!
  183. If you know what you're doing, you may alter
  184. the configuration above, but know what you're
  185. doing before you add anything further, or
  186. subtract from my recommended settings.
  187.  
  188. References:
  189.  
  190. - https://en.wikipedia.org/wiki/Sysctl
  191.  
  192. After completion:
  193.  
  194. sudo ufw disable
  195. sudo ufw enable
  196.  
  197. When you've finished using the sudo command in your
  198. Terminal, close it out with:
  199.  
  200. sudo -K
  201.  
  202. followed by:
  203.  
  204. exit
  205.  
  206. If you're continuing to use sudo for other operations
  207. at the command line, don't type sudo -K until you've
  208. finished.
  209.  
  210. ###################################################
  211.  
  212. Part 3: (. Moderate .) - Configuring before*.rules
  213.  
  214. I won't elaborate on the purpose of this section,
  215. it should become obvious should you read the files.
  216. The following are my recommendations:
  217.  
  218. sudo nano /etc/ufw/before.rules
  219. or: gksudo gedit /etc/ufw/before.rules
  220.  
  221. under #ok icmp codes
  222. comment all entries in this section by adding
  223. a # mark at the beginning of each line. There's
  224. no reason for my computer to allow icmp. I don't
  225. care what someone else says or why, this is my
  226. preference, ignore me here if your preferences
  227. are different.
  228.  
  229. under #allow dhcp to work
  230. comment out the line if your system is setup
  231. for static ip use, if your system needs dhcp
  232. for networking, do not comment this section,
  233. leave it as-is.
  234.  
  235. under #allow MULTICAST mDNS for service discovery
  236. comment out the line
  237.  
  238. under #allow MULTICAST UPnP for service discovery
  239. comment out the line
  240.  
  241. Save file and exit
  242.  
  243. Repeat the above configuration modifications to
  244. the file before6.rules, loading it with nano
  245. or gedit, save and exit.
  246.  
  247. After completion:
  248.  
  249. sudo ufw disable
  250. sudo ufw enable
  251.  
  252. When you've finished using the sudo command in your
  253. Terminal, close it out with:
  254.  
  255. sudo -K
  256.  
  257. followed by:
  258.  
  259. exit
  260.  
  261. If you're continuing to use sudo for other operations
  262. at the command line, don't type sudo -K until you've
  263. finished.
  264.  
  265. ###################################################
  266.  
  267. Part 4: (. Advanced .) - Blocking Private Networks
  268.  
  269. I assume you know what you're doing in this portion
  270. of the guide. If you do not, please skip this section.
  271.  
  272. To block private networks (including the pesky multicast
  273. if you don't need it) this works, but look out for the
  274. 192.168.0.0/16 which may be your local private network
  275. and shouldn't be blocked.
  276.  
  277. Subsection 2(1): Blocking Private Networks: [1]
  278.  
  279. sudo ufw deny out to 10.0.0.0/8
  280. sudo ufw deny out to 172.16.0.0/12
  281. sudo ufw deny out to 192.168.0.0/16
  282.  
  283. 2(1),[1] References:
  284. - https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
  285. - https://tools.ietf.org/html/rfc1918
  286.  
  287. Subsection 2(2): Blocking MULTICAST: [2] [2/I]
  288.  
  289. sudo ufw deny out to 239.0.0.0/10 (or 239.0.0.0/8)
  290. sudo ufw deny out to 224.0.0.0/4
  291.  
  292. 2(2),[2] References:
  293. - https://en.wikipedia.org/wiki/Multicast_address
  294. - https://www.ietf.org/rfc/rfc2365.txt
  295.  
  296. 2(2),[2/I] Information: Google about MULTICAST and
  297. many users on the web experiencing flooding from
  298. their routers with messages in their log from
  299. these addresses frustrating them. You may ignore
  300. these messages if you see them in your logs by
  301. backtracking to Part 2 in this guide above,
  302. and modifying the line within /etc/sysctl.conf
  303. to: net.ipv4.conf.all.log_martians = 0
  304. instead of the value = 1. Personally, I like
  305. seeing martians logged, you may not, for
  306. reasons of sanity when combing log files
  307. and disk space.
  308.  
  309. When you've finished using the sudo command in your
  310. Terminal, close it out with:
  311.  
  312. sudo -K
  313.  
  314. followed by:
  315.  
  316. exit
  317.  
  318. If you're continuing to use sudo for other operations
  319. at the command line, don't type sudo -K until you've
  320. finished.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at