PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/00-concepts.rst

https://gitlab.com/diffcoloredpixel2/NetworkProgramming
ReStructuredText | 616 lines | 345 code | 271 blank | 0 comment | 0 complexity | 4eca804610c287dc147d3918d8bbcea5 MD5 | raw file
  1. :title: Introduction to Network Programming
  2. :data-transition-duration: 1500
  3. :css: networking.css
  4. ====================================
  5. ?QT Basic Developer Training
  6. ====================================
  7. Network Programming
  8. ------------------------------------
  9. ::
  10. Authors:
  11. Eric Ortega eric.ortega@us.af.mil
  12. Chris Plummer christopher.plummer.4@us.af.mil
  13. Version:
  14. v4 14 JUN 2017
  15. #######################
  16. ====================================
  17. Objectives
  18. ====================================
  19. By the end of the class you should be able to:
  20. - Perform socket programming for TCP and UDP, in Python, without supervision
  21. - Understand and explain fundamental concepts of networking, at the both the high level and byte level
  22. - View network traffic and explain what is occuring at a high level and at the byte level
  23. - Be able to independently gather missing information from proper sources, and apply it to a (socket programming) problem**
  24. - Be able to analyze and/or debug networking issues with Wireshark, Netcat, and Python in the absence of information (black box)**
  25. #######################
  26. ====================================
  27. Scary slide first**
  28. ====================================
  29. After this class, you will be expected to perform on a team. You will be tasked to do things that are unfamiliar, with the added disadvantage of nowhere to look up the entire problem
  30. Independently solving problems using technical resources is a requirement in this line of work.
  31. The slides will NOT cover everything in detail. I will cover the topics at a high level, provide you references for the implementation, and assist you in learning tools to debug your code.
  32. I expect you to use those resources and make an effort to figure it out yourself.
  33. #######################
  34. ====================================
  35. Class Structure
  36. ====================================
  37. "Try it" - See what happens. Rebooting is cheap. Just don't interfere with your classmates learning
  38. Make mistakes! Ask questions! This is how we all learn.
  39. Lectures will be brief. This course is primarily labs.
  40. Protip: It might be very beneficial to have working lab code by the end of the course
  41. #######################
  42. ====================================
  43. Environment
  44. ====================================
  45. The majority of labs will be IPv6 and using raw sockets.
  46. We will be using Linux for this class because Windows does not properly support raw sockets.
  47. If you need assistance with Linux, let us know during the lab portions.
  48. #######################
  49. ====================================
  50. Concepts Overview
  51. ====================================
  52. - Numbering Systems
  53. - Endianness
  54. - RFCs
  55. - Wireshark
  56. - Netcat
  57. - OSI Model
  58. - Broadcast vs Collision Domains
  59. #######################
  60. ====================================
  61. Numbering Systems
  62. ====================================
  63. bit = 1 or 0
  64. nibble = 4 bits, half a byte
  65. octet = 8 bits
  66. A byte on most computers is the same as an octet, but other architectures exist with differing sizes
  67. #######################
  68. ====================================
  69. Numbering Systems
  70. ====================================
  71. Hexadecimal - base 16
  72. - Counting: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12, ....
  73. - A single hex digit = 4 bits/half a byte
  74. Octal - base 8
  75. - Counting: 0,1,2,3,4,5,6,7,10,11,12, ...
  76. #######################
  77. ====================================
  78. Numbering Systems
  79. ====================================
  80. Place value is the key.
  81. In decimal our BASE is 10:
  82. - BASE ^ 0 = 10^0 = 1, "ones"
  83. - BASE ^ 1 = 10^1 = 10, "tens"
  84. - BASE ^ 2 = 10^2 = 100, "hundreds"
  85. A number like 25 is broken down:
  86. - (2*10) + (5*1) = 20+5 = 25
  87. #######################
  88. ====================================
  89. Numbering Systems
  90. ====================================
  91. In hexadecimal the "tens" place is actually "sixteens", and the "ones" stays the same but now consists of 15 numbers, 0 to F.
  92. BASE is 16:
  93. - BASE ^ 0 = 16^0 = 1, "ones"
  94. - BASE ^ 1 = 16^1 = 16, "sixteens"
  95. - BASE ^ 2 = 16^2 = 256, "256ths"
  96. Convert 2A into decimal
  97. - (2*16) + (A*1) = (2*16) + (10*1) = 32+10 = 42
  98. #######################
  99. ====================================
  100. Endianness
  101. ====================================
  102. A number is represented by a sequence of bytes. A 32 bit integer is 4 bytes.
  103. - Least Significant Byte (LSB)- The byte representing the smallest part of a number (e.g. the "ones")
  104. - Most Significant Byte (MSB) The place representing the largest part of a number
  105. Endianness tells us how to read a grouping of bytes.
  106. - Big Endian - MSB is first
  107. - Little Endian - LSB is first
  108. #######################
  109. ====================================
  110. Endianness
  111. ====================================
  112. 0xDEADBEEF (4 bytes)
  113. - Little Endian - 0xEF, 0xBE, 0xAD, 0xDE
  114. - Big Endian - 0xDE, 0xAD, 0xBE, 0xEF
  115. #######################
  116. ====================================
  117. Endianness
  118. ====================================
  119. Network byte order is Big Endian
  120. x86/x86_64 are Little Endian
  121. Know what endian your data is in or you will have problems with binary data
  122. #######################
  123. ====================================
  124. RFC - Request for Comments
  125. ====================================
  126. Originally (in the ARPANET days) a semi-formal document of ideas shared to get comments from peers. Now they are issued by the Internet Engineering Task Force (IETF) to formally define an accepted specification
  127. RFCs describe and define the history, implementation, formats, and use of protocols. They are the authoritative source of information regarding protocols.
  128. #######################
  129. ====================================
  130. RFC - Request for Comments
  131. ====================================
  132. You should not 100% rely on vendor implementations or internet posts if you have questions about a protocol.
  133. Microsoft intentionally implemented parts of the HTML/web protocols incorrectly around the era of Internet Explorer 5 and 6.
  134. Posters on forums, even Stack Overflow, can say something that sounds correct/interpreted as correct, even if the RFC specifies otherwise.
  135. Stack Overflow is still a highly useful site, just use it to supplement/validate your understanding of the RFC.
  136. #######################
  137. ====================================
  138. Pydocs
  139. ====================================
  140. Python documentation can be accessed online
  141. Each module has it's own page describing the functions, function parameters, constants, and example usage. These will be linked in the references slide in each slide deck.
  142. The search function on Pydocs will let you search for a specific function to see it's parameters and return values. It does require you to know the module.
  143. e.g. accept() can be found by searching socket.accept()
  144. #######################
  145. ====================================
  146. Man pages
  147. ====================================
  148. The BSD socket API is POSIX compliant and is the standard on Linux machines. There are man pages that describe each system call.
  149. The man pages can be accessed via a terminal in Linux, or via Google.
  150. Man pages have different numbers for different sections. I typically link to man 7 pages which in turn link to the man 2 pages for specific calls.
  151. 7 describes the higher level operations (e.g. man 7 socket)
  152. 2 describes the system calls for C, which are reused in Python. (e.g man 2 accept)
  153. #######################
  154. ====================================
  155. Wireshark
  156. ====================================
  157. Wireshark is a GUI based protocol analyzer. It works on live traffic and PCAP files.
  158. There are three windows in Wireshark:
  159. 1. The traffic window which shows the packets, in order of receipt
  160. 2. The packet window which shows the protocol breakdowns of the selected packet from the traffic window
  161. 3. The hexdump window which shows the raw bytes of the highlighted section highlighted in the packet window
  162. #######################
  163. ====================================
  164. Wireshark
  165. ====================================
  166. .. image:: img/wireshark-example2.png
  167. #######################
  168. ====================================
  169. Wireshark
  170. ====================================
  171. Wireshark requires you to select an interface to sniff traffic
  172. The 'lo' interface is the loopback. Any traffic that is both to and from a single host (A VM and Host OS are different hosts) will show up on this interface. Typically this traffic will never touch the wire
  173. The 'ens33' interface should be the one you have bridged in VMWare. If you have traffic from different hosts, it will show up here.
  174. The 'any' interface should show you all traffic on all interfaces and is the noisiest. We have had issues with this interface in the past. Do not use for these labs.
  175. #######################
  176. =============================================
  177. Wireshark - Quick and Dirty Intro to Filters
  178. =============================================
  179. Click the expression button to the right of the filter bar OR type it in yourself:
  180. ::
  181. eth.addr == aa:bb:cc:dd:ee:ff (source OR dest)
  182. ip.src == 127.0.0.2
  183. ipv6.dst == ::1
  184. tcp.port == 1337 (source OR dest)
  185. tcp.srcport == 80
  186. udp.dstport == 53
  187. arp
  188. icmpv6
  189. #######################
  190. ====================================
  191. Netcat
  192. ====================================
  193. Netcat is the "swiss army knife" of networking. It is a simple networking program that can be used as a client or server.
  194. Any data that is recieved is printed to the terminal (or file if using file redirection)
  195. Netcat supports both IPv4/IPv6 and TCP/UDP
  196. You can use netcat to recieve traffic you are sending, or to simulate a client accessing a server.
  197. #######################
  198. ====================================
  199. Netcat - Linux
  200. ====================================
  201. Most linux distros have netcat built in. It can be executed with 'nc' command.
  202. Useful netcat switches
  203. -6: IPv6
  204. -l: listen
  205. -p: Source port to listen on
  206. -u: UDP mode
  207. #######################
  208. ====================================
  209. Netcat - Windows
  210. ====================================
  211. Some people have compiled the netcat source for Windows and posted the binaries online. There is one binary for IPv4 and one for IPv6, however they have the exact same functionality.
  212. nc.exe is for IPv4
  213. nc6.exe is for IPv6
  214. Useful netcat switches
  215. -l: listen
  216. -L: listen harder (Windows only, automatically listens again after connection terminates)
  217. -p: Source port to listen on
  218. -u: UDP mode
  219. #######################
  220. ====================================
  221. Using netcat
  222. ====================================
  223. Netcat listener ("server")
  224. - nc -lp 1337 (-p is only for listeners)
  225. Netcat connector ("client")
  226. - nc 192.168.1.1 1337
  227. File redirection
  228. - netcat supports file redirection. Files piped into netcat will send be sent over the wire. Output from netcat may be piped to a file
  229. Loops can be used as a ghetto server
  230. - nc -Lp 80 < index.html
  231. - while true; do sudo nc -lp 80 < index.html; done (Equivalent to -L in Windows)
  232. #######################
  233. ====================================
  234. OSI Model
  235. ====================================
  236. .. image:: img/osimodel.png
  237. From: https://infosys.beckhoff.com/content/1033/tf6310_tc3_tcpip/Images/png/84433547__Web.png
  238. #######################
  239. ====================================
  240. TCP/IP Model
  241. ====================================
  242. .. image:: img/tcpmodel.jpeg
  243. From: http://lemoncisco.blogspot.com/2014/06/internetworking-with-tcpip-notes_18.html
  244. #######################
  245. ====================================
  246. Models
  247. ====================================
  248. The OSI model is primarily a theory model
  249. The TCP/IP Model is more practical oriented.
  250. Both are useful at different times. We are primarily concerned with OSI layers 2,3,4,7 in this class
  251. #######################
  252. ====================================
  253. Broadcast vs Collision Domains
  254. ====================================
  255. Collision Domain A grouping of networked devices on a shared medium (Coax ethernet, wifi, etc) that can cause a collision when two device transmit simultaneously
  256. - Extended by: repeaters, hubs
  257. - Divided by: switches
  258. Broadcast Domain A grouping of networked device that can all be reached by a layer 2 broadcast
  259. - Extended by: repeaters, hubs, switches
  260. - Divided by: routers
  261. #######################
  262. ====================================
  263. x-casts
  264. ====================================
  265. Unicast - Single message to a specific host
  266. Multicast - Single message to to multiple hosts
  267. Broadcast - Single message to every host on the LAN
  268. Anycast - IPv6 black magic. We will see this in section 03
  269. #######################
  270. ====================================
  271. LAB 0
  272. ====================================
  273. The following slides contain several labs for you to do.
  274. I expect you to be able to use Wireshark for debugging. Make sure you understand how to find Ethernet addresses, IP addresses,
  275. #######################
  276. ====================================
  277. LAB 0A
  278. ====================================
  279. I have assigned each of you a number starting with 20.
  280. Almost all class labs are to be done in IPv6. Give yourself a global IPv6 address starting with a:c:7:9::X, where X is the number I assigned you.
  281. If you have issues, let us know.
  282. 0) Execute 'ifconfig' and find your interface name (mine is ens33)
  283. 1) Become root
  284. 2) As root, edit /etc/network/interfaces. Any editor will work, as long as you run execute it as root from the terminal
  285. Edit the file to look similar to what is below. Do not alter the 'lo' interface
  286. ::
  287. # interfaces(5) file used by ifup(8) and ifdown(8)
  288. auto lo
  289. iface lo inet loopback
  290. auto ens33
  291. iface ens33 inet dhcp
  292. iface ens33 inet6 static
  293. address a:c:7:9::QQ/64 #Replace QQ with your assigned number
  294. 3) Reboot your VM.
  295. 4) Execute 'ifconfig' and validate your address is there
  296. #######################
  297. ====================================
  298. LAB 0B
  299. ====================================
  300. Setup Windows too...
  301. #######################
  302. ====================================
  303. LAB 0B
  304. ====================================
  305. Run nc and nc6 listeners and connectors. Ensure you can connect to yourself and send data via the following IP addresses:
  306. - 127.0.0.1
  307. - ::1
  308. - Your IPv4 address
  309. - Your IPv6 a:c:7:9::X address
  310. - Your IPv6 address beginning with "fe80" (hint ping -I ens33)
  311. #######################
  312. ====================================
  313. LAB 0C
  314. ====================================
  315. Open wireshark.
  316. In preferences, uncheck the box for "Relative Sequence Numbers" in TCP options
  317. Practice filtering traffic
  318. - By IP/IPv6
  319. - BY MAC Address
  320. - by port
  321. - By protocol
  322. - A combination of the above
  323. Use 'ping' and 'ping6' to test connectivity to your classmates
  324. Use netcat to send traffic back and forth using IPv4 and IPv6
  325. ====================================
  326. Questions and Debugging
  327. ====================================
  328. Which IPs are visible on the loopback/lo interface?
  329. Which IPs are visible on the normal enterface ('ens33', 'eth0' or something similar)?
  330. How do you see into Ethernet, IP, TCP/UDP, and data information of a packet?
  331. What are some of the fields in those layers?
  332. How do you see the specific bytes of a field? How can you highlight them?
  333. #######################
  334. ====================================
  335. Concepts Summary
  336. ====================================
  337. Numbering Systems
  338. - Converting to hex will be crucial for raw sockets
  339. Endianness
  340. - Know your endianess when dealing with binary
  341. RFCs are the authority for protocols
  342. Wireshark is your network debugger
  343. Netcat can be used for many networking tasks.
  344. OSI Model is the theory model. In practice, it becomes the TCP/IP model
  345. Broadcast vs Collision Domains