Monday, 25 November 2024

Check that when my python code is producing data, which network interface it is using

import psutil

from scapy.all import sniff, IP, TCP, raw

import logging

 

# Logging setup

logging.basicConfig(

    filename="network_interface_usage.log",  # Log file

    level=logging.INFO,

    format="%(asctime)s - %(message)s",

    datefmt="%Y-%m-%d %H:%M:%S"

)

 

def log_and_print(message):

    """Logs the message to a file and prints it to the console."""

    print(message)

    logging.info(message)

 

def get_network_interface():

    """Returns a list of network interfaces and their IP addresses."""

    interfaces = psutil.net_if_addrs()

    interface_info = {}

    for interface, addrs in interfaces.items():

        for addr in addrs:

            if addr.family == psutil.AF_INET:  # Filter for IPv4 addresses

                interface_info[interface] = addr.address

    return interface_info

 

def packet_callback(packet):

    """Callback function to process captured packets."""

    if IP in packet and TCP in packet:

        ip_src = packet[IP].src

        ip_dst = packet[IP].dst

        tcp_sport = packet[TCP].sport

        tcp_dport = packet[TCP].dport

 

        # Log packet details

        log_and_print(f"Packet from {ip_src}:{tcp_sport} -> {ip_dst}:{tcp_dport}")

        log_and_print(f"  Raw Packet Data: {raw(packet).hex()}")

        log_and_print("-" * 50)

 

def capture_packets(interface):

    """Start sniffing packets on a specific interface."""

    log_and_print(f"Starting packet capture on {interface}...")

    sniff(iface=interface, prn=packet_callback, store=False)

 

def main():

    # Get all network interfaces and IPs

    interfaces = get_network_interface()

    log_and_print("Detected Network Interfaces and IPs:")

    for interface, ip in interfaces.items():

        log_and_print(f"Interface: {interface} - IP: {ip}")

 

    # Capture packets on each interface (you can choose one based on your setup)

    for interface in interfaces.keys():

        capture_packets(interface)

 

if __name__ == "__main__":

    main()


Sunday, 3 November 2024

ClodFormation, terraform and CDK example

AWSTemplateFormatVersion: "2010-09-09"

Description: "CloudFormation Template for VPC Endpoints and Route 53 with VPC and Subnets as parameters."

 

Parameters:

  SelectedRegion:

    Description: "Select the region for deployment (us-east-1 or ap-east-1)."

    Type: String

    AllowedValues:

      - us-east-1

      - ap-east-1

    Default: us-east-1

 

  VPCID:

    Description: "The VPC ID where the resources will be deployed."

    Type: String

 

  Subnet1ID:

    Description: "The ID of the first subnet."

    Type: String

 

  Subnet2ID:

    Description: "The ID of the second subnet."

    Type: String

 

  Subnet3ID:

    Description: "The ID of the third subnet."

    Type: String

 

Resources:

  # VPC Endpoints

  VPCEndpointS3:

    Type: AWS::EC2::VPCEndpoint

    Properties:

      VpcId: !Ref VPCID

      ServiceName: !Sub "com.amazonaws.${SelectedRegion}.s3"

      VpcEndpointType: Gateway

 

  VPCEndpointEC2:

    Type: AWS::EC2::VPCEndpoint

    Properties:

      VpcId: !Ref VPCID

      ServiceName: !Sub "com.amazonaws.${SelectedRegion}.ec2"

      VpcEndpointType: Interface

      SubnetIds:

        - !Ref Subnet1ID

        - !Ref Subnet2ID

        - !Ref Subnet3ID

 

  VPCEndpointSSM:

    Type: AWS::EC2::VPCEndpoint

    Properties:

      VpcId: !Ref VPCID

      ServiceName: !Sub "com.amazonaws.${SelectedRegion}.ssm"

      VpcEndpointType: Interface

      SubnetIds:

        - !Ref Subnet1ID

        - !Ref Subnet2ID

        - !Ref Subnet3ID

 

  VPCEndpointSecretsManager:

    Type: AWS::EC2::VPCEndpoint

    Properties:

      VpcId: !Ref VPCID

      ServiceName: !Sub "com.amazonaws.${SelectedRegion}.secretsmanager"

      VpcEndpointType: Interface

      SubnetIds:

        - !Ref Subnet1ID

        - !Ref Subnet2ID

        - !Ref Subnet3ID

 

  VPCEndpointCloudWatchLogs:

    Type: AWS::EC2::VPCEndpoint

    Properties:

      VpcId: !Ref VPCID

      ServiceName: !Sub "com.amazonaws.${SelectedRegion}.logs"

      VpcEndpointType: Interface

      SubnetIds:

        - !Ref Subnet1ID

        - !Ref Subnet2ID

        - !Ref Subnet3ID

 

  # Route 53 Hosted Zone

  Route53HostedZone:

    Type: AWS::Route53::HostedZone

    Properties:

      Name: !Sub "example-${SelectedRegion}.com"

 

  # Route 53 Record Set

  Route53RecordSet:

    Type: AWS::Route53::RecordSet

    Properties:

      HostedZoneId: !Ref Route53HostedZone

      Name: "app.example.com."

      Type: A

      AliasTarget:

        DNSName: !Sub "vpce.${SelectedRegion}.amazonaws.com"

        HostedZoneId: !GetAtt Route53HostedZone.Id

 

Outputs:

  SelectedRegionOutput:

    Description: "The selected region."

    Value: !Ref SelectedRegion

 

  VPCIDOutput:

    Description: "The VPC ID used for this deployment."

    Value: !Ref VPCID

 

  Subnet1IDOutput:

    Description: "The Subnet ID for Subnet 1."

    Value: !Ref Subnet1ID

 

  Subnet2IDOutput:

    Description: "The Subnet ID for Subnet 2."

    Value: !Ref Subnet2ID

 

  Subnet3IDOutput:

    Description: "The Subnet ID for Subnet 3."

    Value: !Ref Subnet3ID

 

  HostedZoneIDOutput:

    Description: "The Route 53 Hosted Zone ID."

    Value: !Ref Route53HostedZone