Skip to content

Commit

Permalink
update codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on committed Sep 8, 2024
1 parent fb3ce27 commit 6463860
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

env:
Expand All @@ -12,7 +14,6 @@ jobs:
build:
name: Build project
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[ci skip]')"

strategy:
matrix:
Expand All @@ -21,7 +22,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup ${{ matrix.toolchain }} toolchain
uses: dtolnay/rust-toolchain@master
Expand All @@ -33,13 +34,13 @@ jobs:
uses: Swatinem/rust-cache@v2

- name: Cargo build
run: cargo build --verbose --release
run: cargo build --release

- name: Cargo test
run: cargo test --verbose --release
run: cargo test --release

- name: Cargo clippy
run: cargo clippy --verbose --release --all-targets -- -D warnings
run: cargo clippy --release --all-targets -- -D warnings

- name: Cargo fmt
run: cargo fmt -- --check
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ip-spoofing"
version = "0.1.0"
version = "0.1.1"
authors = ["StackOverflowExcept1on"]
edition = "2021"
description = "Library to send fake IPv4 headers & UDP/TCP-SYN packets to perform L3/L4 attacks"
Expand All @@ -11,6 +11,6 @@ categories = ["network-programming", "api-bindings"]

[dependencies]
thiserror = "1.0"
rand = "0.8.5"
nix = "0.26.1"
etherparse = "0.13.0"
rand = "0.8"
nix = { version = "0.29", features = ["net"] }
etherparse = "0.15"
6 changes: 2 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub enum Error {
IO(#[from] std::io::Error),
#[error("nix crate error: {0}")]
Nix(#[from] nix::Error),
#[error("etherparse crate value error: {0}")]
EtherparseValue(#[from] etherparse::ValueError),
#[error("etherparse crate write error: {0}")]
EtherparseWrite(#[from] etherparse::WriteError),
#[error("etherparse crate error: {0}")]
Etherparse(#[from] etherparse::err::packet::BuildWriteError),
}
28 changes: 16 additions & 12 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ impl ReusablePacketWriter {
#[inline]
pub fn build_ipv4_header(
time_to_live: u8,
protocol: u8,
protocol: IpNumber,
source: [u8; 4],
destination: [u8; 4],
) -> IpHeader {
let mut ipv4_header = Ipv4Header::default();
ipv4_header.identification = rand::thread_rng().gen();
ipv4_header.time_to_live = time_to_live;
ipv4_header.protocol = protocol;
ipv4_header.source = source;
ipv4_header.destination = destination;

IpHeader::Version4(ipv4_header, Default::default())
) -> IpHeaders {
let identification = rand::thread_rng().gen();
IpHeaders::Ipv4(
Ipv4Header {
identification,
time_to_live,
protocol,
source,
destination,
..Default::default()
},
Default::default(),
)
}

/// Writes UDP packet into buffer with given parameters
Expand All @@ -55,7 +59,7 @@ impl ReusablePacketWriter {
) -> Result<()> {
self.inner.clear();

let ip_header = Self::build_ipv4_header(time_to_live, ip_number::UDP, source, destination);
let ip_header = Self::build_ipv4_header(time_to_live, IpNumber::UDP, source, destination);

PacketBuilder::ip(ip_header)
.udp(source_port, destination_port)
Expand All @@ -79,7 +83,7 @@ impl ReusablePacketWriter {
) -> Result<()> {
self.inner.clear();

let ip_header = Self::build_ipv4_header(time_to_live, ip_number::TCP, source, destination);
let ip_header = Self::build_ipv4_header(time_to_live, IpNumber::TCP, source, destination);

PacketBuilder::ip(ip_header)
.tcp(source_port, destination_port, sequence_number, window_size)
Expand Down
16 changes: 4 additions & 12 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use super::{Result, ReusablePacketWriter};
use nix::sys::socket::{
sendto, socket, AddressFamily, MsgFlags, SockFlag, SockProtocol, SockType, SockaddrIn,
};
use nix::unistd::close;
use std::os::fd::{AsRawFd, RawFd};
use std::os::fd::{AsRawFd, OwnedFd, RawFd};

/// Wrapper around low-level `socket(AF_INET, SOCK_RAW, IPPROTO_RAW)`
#[derive(Debug)]
pub struct RawSocket {
fd: RawFd,
fd: OwnedFd,
}

impl RawSocket {
Expand All @@ -30,7 +29,7 @@ impl RawSocket {
pub fn sendto(&self, buf: &[u8], addr: [u8; 4]) -> Result<usize> {
let [a, b, c, d] = addr;
let addr = SockaddrIn::new(a, b, c, d, 0);
let len = sendto(self.fd, buf, &addr, MsgFlags::empty())?;
let len = sendto(self.fd.as_raw_fd(), buf, &addr, MsgFlags::empty())?;
Ok(len)
}

Expand Down Expand Up @@ -88,13 +87,6 @@ impl RawSocket {
/// Implementation that converts `RawSocket` into `fd` (file descriptor)
impl AsRawFd for RawSocket {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}

/// Destructor implementation for `RawSocket`
impl Drop for RawSocket {
fn drop(&mut self) {
let _ = close(self.fd);
self.fd.as_raw_fd()
}
}

0 comments on commit 6463860

Please sign in to comment.