Rust for Hackers: Project 10 – Building a High-Performance Port Scanner

Listen to this Post

In this project, we explore the development of a high-performance port scanner using Rust, focusing on multithreading, async programming, and concurrency control. The goal is to build a resource-efficient and crash-resistant port scanner by leveraging Rust’s powerful features like Tokio and semaphore-based concurrency control.

You Should Know:

1. Threading in Rust:

  • Rust allows you to create OS threads using the `std::thread` module. Here’s a basic example of creating a thread:
    use std::thread;</li>
    </ul>
    
    fn main() {
    let handle = thread::spawn(|| {
    println!("Hello from a thread!");
    });
    
    handle.join().unwrap();
    }
    

    2. Managing Thread Stack Size:

    • You can control the stack size of threads using thread::Builder:
      use std::thread;</li>
      </ul>
      
      fn main() {
      let child = thread::Builder::new()
      .stack_size(2 * 1024 * 1024) // 2 MB stack size
      .spawn(|| {
      println!("Hello from a custom stack size thread!");
      })
      .unwrap();
      
      child.join().unwrap();
      }
      

      3. Async Programming with Tokio:

      • Tokio is a runtime for writing reliable, asynchronous applications in Rust. Here’s a simple example of using Tokio to perform asynchronous tasks:
        use tokio::time::{sleep, Duration};</li>
        </ul>
        
        #[tokio::main]
        async fn main() {
        println!("Starting async task...");
        sleep(Duration::from_secs(1)).await;
        println!("Async task completed!");
        }
        

        4. Semaphore for Concurrency Control:

        • Tokio provides a semaphore to limit the number of concurrent tasks. Here’s how you can use it:
          use tokio::sync::Semaphore;
          use std::sync::Arc;</li>
          </ul>
          
          #[tokio::main]
          async fn main() {
          let semaphore = Arc::new(Semaphore::new(3)); // Allow 3 concurrent tasks
          let mut handles = vec![];
          
          for i in 0..10 {
          let permit = semaphore.clone().acquire_owned().await.unwrap();
          handles.push(tokio::spawn(async move {
          println!("Task {} started", i);
          tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
          println!("Task {} finished", i);
          drop(permit);
          }));
          }
          
          for handle in handles {
          handle.await.unwrap();
          }
          }
          

          5. Building the Port Scanner:

          • Combine the above concepts to build a port scanner. Here’s a simplified version:
            use std::net::{TcpStream, SocketAddr};
            use std::time::Duration;
            use tokio::sync::Semaphore;
            use std::sync::Arc;</li>
            </ul>
            
            async fn scan_port(addr: SocketAddr, semaphore: Arc<Semaphore>) {
            let <em>permit = semaphore.acquire().await.unwrap();
            if let Ok(</em>) = TcpStream::connect_timeout(&addr, Duration::from_secs(1)) {
            println!("Port {} is open", addr.port());
            }
            }
            
            #[tokio::main]
            async fn main() {
            let semaphore = Arc::new(Semaphore::new(100)); // Limit to 100 concurrent scans
            let mut handles = vec![];
            
            for port in 1..1024 {
            let addr = SocketAddr::from(([127, 0, 0, 1], port));
            let semaphore = semaphore.clone();
            handles.push(tokio::spawn(async move {
            scan_port(addr, semaphore).await;
            }));
            }
            
            for handle in handles {
            handle.await.unwrap();
            }
            }
            

            What Undercode Say:

            Building a high-performance port scanner in Rust requires a deep understanding of multithreading, async programming, and concurrency control. By leveraging Rust’s powerful features like Tokio and semaphores, you can create a resource-efficient and crash-resistant tool. This project not only enhances your Rust skills but also provides valuable insights into cybersecurity tool development. For more advanced techniques, consider exploring the full video and code walkthrough at Rust for Hackers: Project 10.

            References:

            Reported By: Diljiths369 Rust – Hackers Feeds
            Extra Hub: Undercode MoN
            Basic Verification: Pass ✅

            Join Our Cyber World:

            💬 Whatsapp | 💬 TelegramFeatured Image