The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. Poor Man's IDS
  2. ==============
  3. ### (Yes Women can use it too)
  4. The goal of this project is to keep an eye on the requests going in
  5. and out of my network onto the Internet (iNet). This is made
  6. necessary for two reasons:
  7. 1. By looking for unusual activity I can get a heads up about
  8. unwanted software or even "spy hardware" on my systems, ie.
  9. "Detect Intrusions".
  10. 2. Almost all software now days, especially those created by gigabuck
  11. giants, makes requests out onto the iNet that I did not ask for
  12. and don't want happening. But even Mozilla makes network traffic I
  13. didn't ask for and don't want.
  14. So this tool is my way of "watching the watchers". This is not a
  15. plug-n-play tool that _magically_ grants the user a "suit of
  16. invulnerability". But it is a tool for those looking for more insight
  17. into their iNet traffic either due to security concerns or curiosity.
  18. This software is in the very early stages. Right now it just combines
  19. data from logs from a couple of different software packages. I've
  20. already setup many blocks for traffic I don't want happening, like
  21. g00gle Analytics, some ad servers, ...
  22. For the curious I'll post my current block lists in this repository
  23. from time to time. But be **WARNED** that its likely to break your
  24. iNet experience if you use them. I'm a cyber-rebel at heart and tend
  25. to take an "if its doing something I don't want, I have no use for
  26. it" approach. Meaning I'd rather not use a site / program if it
  27. violates my concerns, rather than just "go with the flow". And I will
  28. likely discover I'm breaking stuff I actually want, like: I realized
  29. I've broken my ability to post comments on
  30. [HaD](https://hackaday.com/), but I was curious about what "Server X"
  31. did.
  32. So! If you're not "faint of heart" come join me on my adventure in
  33. iNet security exploration.
  34. Phase 1 - General Setup & Operation
  35. -----------------------------------
  36. In general I feel its necessary to have a **real-world** idea of what
  37. you're dealing with before diving in and writing software to deal
  38. with what you _think_ your dealing with. So the basic plan is simple:
  39. 1. Setup my network routing devices, which are running Linux, to log
  40. DNS queries and network connections. Specifically, anything that is
  41. a _new_ unrelated packet gets logged.
  42. 2. Collect the logs on a machine, with backups on alternate machines.
  43. 3. Run the logs through a filter to combine the DNS query data with
  44. the packet data.
  45. 4. Analyze results to determine phase 2 needs.
  46. In this phase I'm jumping the gun a bit, but cyber-thugs are actively
  47. beating on all of our doors, even as I'm getting my stuff together
  48. and there are certain kinds of traffic I know I want to put an end
  49. to. In that light I've already spotted interesting servers I'm
  50. blocking. And I've beefed up my firewall to my mail server with more
  51. permanent blocks from obvious MTA bashers.
  52. In this phase I'm using the most excellent
  53. "[dnsmasq](https://thekelleys.org.uk/dnsmasq/doc.html)" to log the
  54. queries **and** block host names I don't want being accessed. I do
  55. this by assigning the bogus address "127.0.0.255" to the names in my
  56. server's "hosts" file, which is used by "dnsmasq" to answer DNS
  57. queries. That address is a **valid** "localhost" address, so will
  58. **immediately** fail requests unless you put a http(s) server on it.
  59. And I'm sure you can imagine those possibilities.
  60. The other source of information and block capabilities is "iptables"
  61. / "ip6tables". I added rules to log "new" packets. The parsing
  62. software is what this repository is about right now.
  63. Note on routers
  64. ---------------
  65. I'm using Netgear appliances for WiFi and the first tier firewall
  66. connected to my iNet connection. Since a lot of modern ISP connection
  67. devices provide their own idea of security I have to turn off their
  68. firewall stuff and configure them in a transparent bridging
  69. configuration.
  70. I use Netgear because they actively provide tool chains for select
  71. models of their equipment and encourage people to load their own
  72. software mods on them. Have a look at
  73. [My Open Router](https://www.myopenrouter.com/). On my devices I
  74. have settled on [Shibby's Tomato](https://tomato.groov.pl/). Its very
  75. compact, extremely flexible and seems to have everything I've needed
  76. when I've needed it. As an example my dnsmasq & iptables setup for my
  77. gateway router required no changes to the firmware. I just put some
  78. of the lesser used config pages to use.
  79. The future
  80. ----------
  81. The goal is an active alert system. This system should provide
  82. immediate feedback on unknown connections allowing the user to either
  83. grant or deny access and maintain the appropriate block lists.
  84. But as "reality" exposes itself things are likely to change.
  85. The CODE
  86. --------
  87. I'm using C++ to write this. I'm targeting C++98 at this time.
  88. Although I think C++11 defines the minimum viable version of C++
  89. there are places that its still not available, which is most likely
  90. to happen with oddball tool chains like those provided by Netgear.
  91. But even my RaspberryPI 2B doesn't support it. Well... maybe with an
  92. OS upgrade... In the end I'm hoping to support the broadest range of
  93. Linux powered platforms.
  94. I'm doing all of my development with Linux. It may be possible to do
  95. something similar, maybe with very little modification, on BSD based
  96. platforms. I don't have them and therefor won't personally be working
  97. on it. But, unless it makes things really ugly, I'm not opposed to
  98. contributions on that front.
  99. Installation & use
  100. ------------------
  101. This is still **VERY** crude and incomplete.
  102. 1. Type "make" to compile. Hopefully it compiles for you.
  103. 2. Create a configuration file. See "Config File" section below.
  104. 3. Run `iptraffic ...`. See "Command Line" section below.
  105. 4. Review the output.
  106. Config File
  107. -----------
  108. The config file is a sloppy INI style file. Its broken into sections
  109. that start with a header, which is a line containing a name
  110. surrounded by square brackets ( [...] ). The content of the section
  111. stretches to the next header and will either contain name / value
  112. pairs or specifically formatted text data.
  113. Remarks are lines that start with a pound sign (#). I am not using
  114. the MS style semicolon (;). Blank lines are ignored.
  115. Currently there are two sections allowed in the file:
  116. - **us:** which lists the ip address range(s) that are used inside
  117. your network (us). Every address not matching is considered
  118. outside your network (them). This is used to determine direction
  119. of connection (in/out).
  120. Each line is a single entry. The entries can be in IPv4 or 6
  121. notation and should be in a minimalistic format. Basically this
  122. means no leading zeros. For IPv6 make sure to use :: as
  123. appropriate. Address prefixes can be used to define networks and
  124. they should end with either "." (IPv4) or ":" (IPv6).
  125. Example: "192.168.1." matches the 192.168.1.0/24 network.
  126. - **ignores:** is a set of TSV records that define what traffic you
  127. are OK with happening, which are ignored and not reported in the
  128. output.
  129. TSV records contain the following fields separated by tabs:
  130. 1. Our / internal address. Follows same wild card specification as
  131. described in "us" above.
  132. 2. Our port number. 0 matches all.
  133. 3. Outside / their IP address. Follows same wild card
  134. specification as described in "us" above.
  135. 4. Their port number. Zero matches all.
  136. 5. Domain name associated with the destination. "*" matches all.
  137. 6. IP protocol used (must be all caps):
  138. - **ICMP:** pings, trace route, ... Port #s match the ICMP
  139. message "type"
  140. - **TCP:** the most common network connection (HTTP(s), FTP, ...)
  141. - **UDP:** Connectionless IP protocol, used for DNS, NTP, ...
  142. 7. Incoming (1) / Outgoing (0) connection.
  143. A network connection that matches the filter defined by the
  144. fields will be ignored and not reported to the output. In this
  145. phase this is basically how we define what's "OK" traffic. Its
  146. assumed blocking is handled in the firewall and therfore not
  147. showing in the logs.
  148. example:
  149. ```
  150. * 0 * 443 hackaday.com TCP 0
  151. ```
  152. Ignores a connection from any internal address to a remote
  153. address for "hackaday.com" using the remote port of 443 in the
  154. TCP protocol. Basically this ignores (marks OK):
  155. `https://hackaday.com/`. **BUT** other connections generated by
  156. the content from that connection will still show in the output.
  157. So this means things like g00gle analytics, CDNs, external
  158. JavaScript and CSS servers, ... will still be reported.
  159. Obviously in normal use the list of OK (ignored) content will get
  160. quite long. Future tools will handle this better.
  161. iptraffic Command Line
  162. ----------------------
  163. The iptraffic command uses the following syntax:
  164. ```
  165. iptraffic -c {config file} \[-o {output log}\] \[{input log} \[ ... \]\]
  166. ```
  167. - **{config file}:** must be specified and for best results must
  168. contain at least the \[us\] section with your internal addresses.
  169. See "Config File" above. Otherwise the "direction" indication is
  170. meaningless and it gets harder to write ignores.
  171. - **{output log}:** if supplied is the file name to write the
  172. agregated and filtered content to. If the "-o" option is left off
  173. output is written to stdout, in UNIX tradition.
  174. - **{input log}:** One or more input log files can be specified on
  175. the command line. They will be processed in the order specified and
  176. resulting data written to the specified output or stdout.
  177. **NOTE:** Its assumed that the content of an input log file is in
  178. chronological order (pretty typical) and that the files are specified
  179. on the command line in chronological order. Data collected from the
  180. earlier logs will be used to satisfy needs in the latter logs. This
  181. affects how DNS names (queries) are mapped to connection addresses.
  182. Its assumed that the DNS query is answered before the resulting
  183. connection is made. This is the natural order of things.
  184. This program is mainly intended as a test _jig_. To test both the
  185. concept and DNS to connection mapping algorithms, which still need
  186. work.
  187. iptraffic Output
  188. ----------------
  189. Here's a sample of an unexpected connection I found being made:
  190. ```
  191. Jun 15 02:06:28 192.0.2.75 -> 74.125.195.188 TCP[5228] mobile-gtalk.l.google.com
  192. ```
  193. This shows my tablet calling home to g00gle for something. The
  194. internal address was changed to match the "demo" IPv4 network, as
  195. specified in RFCs. This line consists of the following information in
  196. order:
  197. 1. Date / time stamp of the iptables log entry
  198. 2. The _us_ (internal) address, assuming your config file is correct.
  199. 3. Direction of the connection. "192.0.2.75 called _them_".
  200. 4. The _them_ (external) address, assuming your config file is
  201. correct.
  202. 5. IP protocol & port of the destination
  203. 6. The DNS name if a matching DNS query was logged prior to the
  204. connection getting logged.