Rust Port Sniffer v2 - Update

Tensor Programming · Intermediate ·📰 AI News & Updates ·3y ago

Key Takeaways

The video demonstrates an updated version of the Rust Port Sniffer project, utilizing Tokyo runtime for green threads, BPF for command line tools, and async TCP stream connections. The project showcases improvements in command line interface, argument parsing, and task-based concurrency.

Full Transcript

hey guys my name is tenser today we're going to be doing something that I've never really done on this channel before and that is to go back to an old project revisit it and update it project in question is the old rust port sniffer that I wrote about three or four years ago the other day I was going across GitHub and I was looking at some things and I noticed that there were quite a few repositories that had copied this code and it kind of bothered me because this code is just not optimal if you go ahead and you run this even with the max amount of threads it will take tens of minutes to resolve and we can do better and while we're making this application faster let's go ahead and also clean up the command line by importing a command line Library so that's what we're going to be doing today one more thing before we get started I'm just going to point out that the old video of course is still valuable it's still valuable to go back and look at it and I will make sure to link it over to this video as well I will be keeping the old code in the repository for prosperity's sake we'll have a version 1 and a version two with version one being this whole code and version two being the new code that we end up writing today first thing we want to do is go into our cargo about Tamil for one let's update the version just because we also want to update the rust Edition so because this was a project that was written a long time ago it was using the old version of rust setting the addition to 2021 will allow us to use libraries and dependencies that obviously also use this Edition it will also give us new features that we would not have access to without it we're going to pull in two dependencies Tokyo and BPF Tokyo is an asynchronous runtime and this will allow us to use green threads instead of os threads now green threads are basically threads that are scheduled by the Tokyo runtime itself whereas OS threads are the hardware threads that you get with the CPU so for example my CPU is a threadripper with 128 threads so I would have 128 OS threads available to me green threads are a lighter weight and because they're scheduled by the Tokyo runtime itself we can have many many more of them than we can have OS threads the main trade-off here though is that we have to embed a runtime into our program that is in this case the Tokyo runtime whereas if we were just using native rust and Os threads then we wouldn't actually have a runtime except for of course the runtime that we create with our program now bpath on the other hand is a command line tool and we're going to be using the derive feature and then the bright color feature these features will let us quickly write a COI derive will allow us to basically create a struct and then we can use the macros that are imported with derive to essentially flesh out the struct and turn it into our CRI and then bright color will just allow us to have colors in the command line all right so the first thing I'm going to come and do is just clean up the CRI and we're going to get rid of the threads argument the flag and then we're also going to just get rid of this entire implementation block so what we're going to do here is we're going to make it so that rather than having the user specify the amount of threads that they want to use and the address we'll have it so that the user specifies the address they want to sniff on and we'll also allow the user to specify a start port and an end port so that they can specify a specific range of ports and then because we're not specifying threads we'll make it so that the system will use exactly one Tokyo task to attempt to connect to a specific Port rather than what we're doing right here where we can have a thread try to connect to multiple different ports and of course I'll explain this a bit more as we get into it alright so first I'm going to go ahead and just clean up our Imports here so we're going to be using these things still but we're also going to be pulling in the TCP stream from Tokyo the Tokyo task structure and then the bpap bpap macro this will allow us to create our command line another thing we want to go ahead and do is just create a fallback address so because the user can specify an address and that address could potentially fail we want to have a fallback that the sniffer will go ahead and fall back to first we want to specify a fallback now the fallback here will just be our Local Host which is 127.0.0.1 if the user doesn't specify the address argument in our CRI it will go ahead and just fall back to this value we'll go ahead and derive bpap on our argument struct this will basically specify to the bpap macro that we want the argument struck to be our CLI parser in other words when a user passes in an address it will be in the ipu address format and if they pass in something that isn't in that format it will throw an error now when we run this CLI we also want to have an options screen so we'll go ahead and we'll add this little annotation here so it'll be path options now we can use the The bpath annotation on top of the address field to specify how we want this field to act in the command line so for instance we want to have long and short versions of the command these will just default to these will give us a long form version of the address argument which will just of course be the name address and then a short form which will just be the first letter of the value which will just be a and then we can also add our fallback in this case we'll point it towards the constant that we created up here so IP fallback if we want to add a description to the help we can go ahead and add a comment here with three forward slashes like this and this just defines what this field is so the address that you want to sniff must be a valid ipv4 address and then States it falls back to our fallback now we can create another field here we'll call this start port and the start Port will be the first Port that we want the sniffer to start on let's have this fall back to one because obviously you can't have ports that are negative or zero that also means that we also want to have a guard so that the user can't put in a negative port or some kind of invalid port in general bpap allows us to create these guards by creating a function it takes in a reference to the type that we're trying to add this on in this case u16 and outputs a Boolean start Port guard will just be is the value greater than zero and we can attach it here by writing guard like this and then putting in the function name and then we can add a little description here and we can just say must be greater than zero now let's go ahead and add an end port argument and as you can see it just kind of followed what we did with start here so we have our long and short and then we also have a guard here where we're making sure that this is less than or equal to our max value and of course we have the fallback of the max constant value and that's because you can't really have a port greater than this number so of course we don't want a user to enter in like 1 million for instance and as a result this will allow us to just catch those kinds of errors and then output this little display if the user inputs the error and actually both of these fields have a description so the start Port is just the start port for the sniffer and then it says must be greater than zero and then the end port is the end port for the sniffer and then it says must be less than or equal to six five five three five now let's go ahead and clean up our scan function we imported TCP stream from Tokyo and TCP stream from Tokyo automatically is a synchronous meaning it uses a future so we want to turn this function into an async function that way we can go ahead and call a weight on the TCP stream connect call so by calling away we're basically saying wait for this call to finish before you move on to any other line of code and what that does is it also unwraps the future so now the errors that we were seeing before on the match statement here go away we're also going to just in general clean up this function by getting rid of the loop and getting rid of this logic here before what would happen is we'd have the iteration that happens in the main function down here where it iterates from zero to the number of threads and then that would specify where the thread would start going through the ports in the address so we'd put in say like 10 and then there'd be another one that'd have 11 so 10 would start at 10 and then it would iterate upwards and then we'd have weapon which would start at 11 and then iterate upwards the problem with this though is that we're starting at say 10 iterating upwards which means that we're going to cover 10 11 12 13 Etc and then we're also starting at 11 iterating upwards and then we're covering 11 12 13 40. so we're covering multiple ports at the same time through multiple threads and that's just inefficient let's just get rid of all this so we're going to get rid of the start Port here the loop and we're going to get rid of this if break statement and then the iteration let's also get rid of the number of threads argument because we're not going to need that anymore instead of just putting in a tuple with the address in the port we're going to use format we're just going to use a string instead and we'll change the argument up here from start port to just Port because each scan will scan exactly one port and then end and exit so here's what our new function looks like what we're doing is we're connecting to the address and of course the address is just the string where we have the address part and then the colon and then the port that we want to listen to then if we connect to a port we'll print out a period as we did before then we'll flush the i o stream so that we can then print out another period next to it and so on and so forth and we'll send the port back through the channel to the main function so that we can then print out that we have an open port if it doesn't connect properly we do nothing because we don't really care about the ones that are unsuccessful now down in Maine we have all these errors first of all we can get rid of all of this we can get rid of most of this logic as well so any of the arguments logic we could just get rid of we also want to take main make it async and we want to annotate it with Tokyo main like this and this will tell the system that this is the main entry point for the Tokyo system the macro that we implemented on the argument struct creates a function of the same name in this case it's just called arguments and so what we can do is just called arguments.run this will collect all of the command line values from the parser and then put them into the struct and give us the struct we have the value Ops which is of type arguments and this will collect all of the user inputs now we can go ahead and modify the for Loop here so instead of iterating from zero to the amount of threads that we wanted to use we can iterate from the start port to the end port then let's get rid of this thread spawn car because we're not spawning threads anymore instead we're going to be using tasks so tasks are essentially green threads in the Tokyo system so we can just say task Bond and this is going to want an asynchronous block so instead of passing in a closure we'll pass in our async block and then we'll put our scan call in here we also want to call a weight and then we also want to of course call away because we want this to block inside of the thread and to resolve then naturally we want to make sure that our scan is working properly so we'll get rid of the number of threads argument and then we'll get address from our Ops value looking over the rest of the logic it can stay the same and we are done with our program so now this is a much more efficient version of the port sniffer than the one that I wrote five years ago apparently based on this little message here all right so let's go ahead and go into our command line and see what our CLI looks like so I'm going to go ahead and just call Cargo run with double dashes and dash H so that we can see the help it was generated from the macro that we're using so you can see here we have the available options we have a little usage thing and because we implemented the bright colors feature for bpap it allows us to have this green as well which is kind of nice tells us all the arguments um and then it shows all the values that we put into the comments so now when I go into invoke this call I can go ahead and specify the address and I can just use Dash a instead of dash dash address and if I want to I can specify a start and an end so let's see how fast this runs if I just call it on 192.168.11 here's the result of running the port sniffer and it took us roughly two three minutes to resolve and get all these open ports we can also go ahead and just call Cargo run and this will use our default values which will be 127.0.0.1 and then start Port of one and the end port of the maximum Port value and you can see this went through and it got all of the open ports importantly it also only took about three or four minutes at most so there's one more thing I just want to let you guys know while Port sniffing itself is not inherently malicious it can be misconstrued as such if you use this code on some kind of public domain the owners of said domain May flag you or Blacklist you or think that you're trying to DDOS them I would highly recommend that you just use this code on things that you own so your own servers or your router or your public computers and stuff like that all right guys so that's it for this tutorial feel free to comment in the box below let me know what you think of the new format if you like it if you hate it what I should improve upon any of this stuff it's been a while since I've made any videos in general and I'm hoping that at least doing something like this will allow me to create some simple basic videos in the future I am really going to try to put aside some time so that I can make videos on the regular but I'm hoping that in the interim we can do a few of these videos just to kind of get the ball away all right guys well have a good night

Original Description

#Rust #PortSniffer #TensorProgramming In this video, we create an updated version of an old rust project, the port sniffer project. Source Code (v2): https://github.com/tensor-programming/Rust_Port_Sniffer Source Code (v1): https://github.com/tensor-programming/Rust_Port_Sniffer/tree/v1 Support the Channel and Join Patreon: Patreon: https://www.patreon.com/tensor_programming Dontate: ETH: 0x03247265dd5242605bD2FA3c40fb3b70d9e3D685 Cardano: addr1q9auccwrr9ws8qdyv45f4qwsx76pfmld4zapks89sakq94ay0xmle73y0r8ruwd0zslls4eglf98lghru7ywv56cedysk7ftjt Check out our Twitter: https://twitter.com/TensorProgram Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/ Check out our Steemit: https://steemit.com/@tensor
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tensor Programming · Tensor Programming · 0 of 60

← Previous Next →
1 NodeJs, Text editors and IDEs
NodeJs, Text editors and IDEs
Tensor Programming
2 Vanilla JS todo App
Vanilla JS todo App
Tensor Programming
3 Elm Tutorial part 1
Elm Tutorial part 1
Tensor Programming
4 Elm Lang Tutorial, Part 2
Elm Lang Tutorial, Part 2
Tensor Programming
5 Elm Tutorial Part 3
Elm Tutorial Part 3
Tensor Programming
6 Elm Tutorial Part 4 -- Analog Clock App
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
7 Elm Tutorial part 5 -- Snake Game
Elm Tutorial part 5 -- Snake Game
Tensor Programming
8 Elm Tutorial part 6 -- Calculator
Elm Tutorial part 6 -- Calculator
Tensor Programming
9 Go Tutorial part 1 -- Hello World and Static File Server
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
10 Go Tutorial part 2 -- Web Crawler
Go Tutorial part 2 -- Web Crawler
Tensor Programming
11 Go Tutorial Part 3 (Web App part 1)
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
12 Go tutorial Part 4 (Web tutorial part 2) - Using templates
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
13 Go tutorial part 5 (web app part 3)
Go tutorial part 5 (web app part 3)
Tensor Programming
14 Go tutorial part 6 (webapp part 4)
Go tutorial part 6 (webapp part 4)
Tensor Programming
15 Go tutorial part 7 (web app part 5)
Go tutorial part 7 (web app part 5)
Tensor Programming
16 Go tutorial part 8 (Web app part 6)
Go tutorial part 8 (Web app part 6)
Tensor Programming
17 Go tutorial Part 9 (web tutorial part 7)
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
18 Go tutorial Part 10 (web app part 8)
Go tutorial Part 10 (web app part 8)
Tensor Programming
19 Go tutorial Part 11 (Web app Part 9)
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
20 Go Tutorial Part 12 (Web app Part 10)
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
21 Go Tutorial Part 13 (Web app Part 11)
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
22 Looking at Elm 0.18
Looking at Elm 0.18
Tensor Programming
23 Go tutorial Part 14 (Web tutorial part 12)
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
24 Go tutorial Part 15 (Web tutorial part 13)
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
25 Go tutorial part 16 (web app part 14)
Go tutorial part 16 (web app part 14)
Tensor Programming
26 Elm Tutorial Part 7 (SPA part 1)
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
27 Elm Tutorial Part 8 (SPA Part 2)
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
28 Electron Elm Tutorial
Electron Elm Tutorial
Tensor Programming
29 Go tutorial part 17 (web app part 15)
Go tutorial part 17 (web app part 15)
Tensor Programming
30 Up and Coming Programming Languages and Technologies for 2017
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
31 elixir tutorial part 1
elixir tutorial part 1
Tensor Programming
32 elixir tutorial part 2
elixir tutorial part 2
Tensor Programming
33 Elixir tutorial Part 3 (GenServer and Supervisor)
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
34 Elixir Tutorial Part 4 (GenStage)
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
35 Elixir Tutorial Part 5 (Plug and Cowboy)
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
36 Phoenix Framework Tutorial Part 1 (elixir part 6)
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
37 Phoenix Framework Tutorial Part 2  (elixir part 7)
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
38 Phoenix Framework Tutorial Part 3 (elixir part 8)
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
39 A Intro to Clojure and Clojure Syntax
A Intro to Clojure and Clojure Syntax
Tensor Programming
40 An Update about the channel
An Update about the channel
Tensor Programming
41 Intro to Rustlang (Setup and Primitives)
Intro to Rustlang (Setup and Primitives)
Tensor Programming
42 Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
43 Intro to Rustlang (Ownership and Borrowing)
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
44 Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
45 Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
46 Intro to RustLang (Enums and Options)
Intro to RustLang (Enums and Options)
Tensor Programming
47 Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
48 Rustlang Project: Snake Game
Rustlang Project: Snake Game
Tensor Programming
49 Intro to Rustlang (Traits and Generic Types)
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
50 Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
51 Intro to Rust-lang (Modules and Lifetimes)
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
52 Intro to Rust-lang (Macros and Metaprogramming)
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
53 Intro to Rust-lang (Error Handling)
Intro to Rust-lang (Error Handling)
Tensor Programming
54 Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
55 Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
56 Rustlang Project: Port Sniffer CLI
Rustlang Project: Port Sniffer CLI
Tensor Programming
57 Rustlang Project: Chat Application
Rustlang Project: Chat Application
Tensor Programming
58 Rustlang Project: CLI Toy Blockchain
Rustlang Project: CLI Toy Blockchain
Tensor Programming
59 Intro to Rust-lang (Setting up a Development Environment)
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
60 Intro to Rust-lang (Building a Web API with Iron)
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming

This video teaches how to update a Rust port sniffer project to use Tokyo runtime and BPF, improving the command line interface and task-based concurrency. The project demonstrates how to use async TCP stream connections and derive features of BPF.

Key Takeaways
  1. Update the version of the project in Cargo.toml
  2. Update the Rust edition to 2021
  3. Pull in the Tokyo and BPF dependencies
  4. Use the derive feature of BPF to create a CLI
  5. Use the bright color feature of BPF to add colors to the CLI
  6. Derive bpap on argument struct
  7. Add fallback address
  8. Create options screen
  9. Add description to help
  10. Create guards for start and end port arguments
💡 Using Tokyo runtime and BPF can improve the performance and usability of a Rust port sniffer project.

Related Reads

📰
Crypto Market Navigates Institutional Inflows and Innovation Amidst Rising AI Scam Threats and Valuation Deb Concerns
Learn how AI scam threats and valuation concerns impact crypto market navigation amidst institutional inflows and innovation
Dev.to AI
📰
IQE bets on AI data-centre demand to drive 20% sales growth in 2026
IQE expects 20% sales growth in 2026 driven by AI data-centre demand, learn how AI is transforming the semiconductor industry
The Next Web AI
📰
Judge signs off on Anthropic’s $1.5bn book piracy settlement, the biggest in US copyright history
Anthropic settles for $1.5bn in a copyright infringement case, the largest in US history, for training its Claude models on pirated books
The Next Web AI
📰
Oracle Just Fired 30,000 People to Pay One Customer’s AI Bill
Oracle's massive layoffs may be linked to a single customer's large AI bill, highlighting the economic risks of AI adoption
Medium · AI
Up next
Meta Changed Your Ads Without Consent & More Ecom Marketing News
Daryl Mander
Watch →