반응형

작성일: 2023년 9월 20일

 

 

 

Client 장비에 network port가 여러개 있는 경우, 특정 network port를 지정하여 IP 패킷을 전송하고 싶을 때가 있다.

이럴 때, source IP address를 binding하면 특정 network port를 통해 IP 패킷이 전송된다.

참고:
  일반적으로 Target IP address로 가기 위한 routing path 및 network port는 
  OS에 있는 Routing table을 통해서 자동으로 결정된다.
  그러나 Target IP address로 가기 위한 routing path가 1개가 아닌 2개 이상인 경우에는
  어느 network port를 통해서 IP 패킷이 나갈지 예측하기 어렵다.  
package main

import (
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "time"
)


func main() {
##
## NOTE:  14.33.80.179를 Source IP address로 지정한다. (즉, Source IP address binding)
##
    localAddr, err := net.ResolveIPAddr("ip", "14.33.80.179")
    if err != nil {
        panic(err)
    }

    localTCPAddr := net.TCPAddr{
        IP: localAddr.IP,
    }

    d := net.Dialer{
        LocalAddr: &localTCPAddr,
        Timeout:   30 * time.Second,
        KeepAlive: 30 * time.Second,
    }

    tr := &http.Transport{
        Proxy:               http.ProxyFromEnvironment,
        Dial:                d.Dial,
        TLSHandshakeTimeout: 10 * time.Second,
    }

    webclient := &http.Client{Transport: tr}

    // Use NewRequest so we can change the UserAgent string in the header
    req, err := http.NewRequest("GET", "https://www.naver.com", nil)
    if err != nil {
        panic(err)
    }

    res, err := webclient.Do(req)
    if err != nil {
        panic(err)
    }

    fmt.Println("DEBUG", res)
    defer res.Body.Close()

    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s", string(content))
}

 

 

 

 


 

반응형
작성일: 2023년 9월

 

IP address 범위를 지정하여 ping 패킷(즉, ICMP Echo Request)를 보내고 싶다면, 아래 예제 스크립트처럼 작성하여 실행하면 된다.

 

#!/usr/bin/bash

for ip in $(seq 1 7);
  do ping -c 1 10.1.4.$ip;
done

 

반응형

 


 

명령어(CLI)로 조회하기

## 방법 A-1
$ curl ipinfo.io
{
  "ip": "211.33.55.12",
  "city": "Yongsan-dong",
  "region": "Seoul",
  "country": "KR",
  "loc": "37.5032,126.9989",
  "org": "AS3786 LG DACOM Corporation",
  "postal": "06547",
  "timezone": "Asia/Seoul",
  "readme": "https://ipinfo.io/missingauth"
}


## 방법 A-2
$ curl ipinfo.io/ip
211.33.55.12


## ----------------------------------------------------------------

## 방법 B
$  dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com
"211.33.55.12"

 

 

웹 브라우저로 조회하기

https://www.whatismyip.com/

 

What Is My IP? Shows Your Public IP Address - IPv4 - IPv6

See the IP address assigned to your device. Show my IP city, state, and country. What Is An IP Address? IPv4, IPv6, public IP explained.

www.whatismyip.com

 


 

 

반응형

Web Server를 운영하다보니, 해외에서 내 Web Server를 공격하는 시도가 많이 보인다.

그래서 이런 Web Server를 공격하는 Access를 몽땅 iptables 명령으로 drop(block)하는 스크립트를 만들었다.

아래와 같이 스크립트를 작성하고, 실행하면 이 시간 이후로 공격이 모두 차단된다.

 

##
## Block Some IP addresses
##

#!/bin/bash

LOG_FILE_NAME="/var/log/apache2/access.log"

BLACK_LIST=$(awk '{ printf "%s\n", $1 }' $LOG_FILE_NAME  | grep -v "192.168.0." | sort | uniq)

for BLOCKING_IP in $BLACK_LIST
do
  BLOCKING_CIDR="$BLOCKING_IP/16"
  echo "Blocking CIDR: $BLOCKING_CIDR"
  GEO_ADDRESS=$(whois $BLOCKING_IP | grep -i "address:")
  echo "$GEO_ADDRESS"
  echo ""
  iptables -A INPUT -s $BLOCKING_CIDR  -p tcp  --dport 80  -j DROP
done


iptables -L -n --line-numbers | grep "DROP" | awk '{ printf "%s\n", $5 }' | sort
# iptables -D INPUT 1
# iptables -D INPUT 2
# ...


##
## If you would like to save the iptables rules, then run the following command.
##
# service iptables save

 

만약, 위와 같이 수행했다가 다시 Rule을 지우고 싶다면, 아래와 같이 스크립트를 작성해서 실행한다.

 

##
## Remove rules of IP tables
##

#!/bin/bash

REMOVE_IP_LIST=$(iptables -L -n | grep "DROP" | awk '{ printf "%s\n", $4 }' | sort)

for REMOVE_IP in $REMOVE_IP_LIST
do
  echo "IP Address to be removed: $REMOVE_IP"
  iptables -D INPUT -s $REMOVE_IP -j DROP
done

+ Recent posts