network - GradedJestRisk/linux-training GitHub Wiki

Network

Overview

List:

  • get list of opened ports sudo lsof -i -P -n | grep LISTEN
  • kill process listening on specific port lsof -ti tcp:``<PORT>{=html} | xargs kill
  • get active internet interface
  • monitor traffic sudo itop or sudo itop -i ``<INTERFACE>{=html}
  • export port through ngrok: ./ngrok http 80
  • get domain details whois ``<DOMAIN>{=html}
  • get DNS details host -v ``<DOMAIN>{=html}

Get general info

MAC address

Get network adapters

sudo lshw -C network

The MAC address is in serial property

*-network                 
   description: Wireless interface
   logical name: wlp58s0
    serial: 60:f2:62:8d:aa:13

IP

In shell

curl ifconfig.me

Or visit https://ifconfig.me/

Network adapter

ip -color -brief addr

You'll get status and IP per adapter

lo               UNKNOWN        127.0.0.1/8 ::1/128 
enp0s31f6        DOWN           
wlp58s0          UP             172.19.211.206/22 fe80::1d02:812c:d002:4279/64

To get amount of data transferred

ip -color -stat -human addr show

make HTTP requests

cat

Useful in bare-metal docker images

exec 3<>/dev/tcp/127.0.0.1/9310;
echo -e \"GET /auth/ HTTP/1.1\nhost:localhost:9310\n\" >&3;
timeout --preserve-status 1 cat <&3

wget

Download file, ignoring networks problems

wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 --tries=0  $URL

curl

Make many request

Send request each second and show result (you can switch to 100 ms interval using 0.1

watch -n 1 curl http://httpbin.org/get

Save output

curl --request GET http://httpbin.org/get > response.json

response.json

{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.68.0", 
    "X-Amzn-Trace-Id": "Root=1-5fabfe89-5d7010b37c48e02f34fe140d"
  }, 
  "origin": "81.254.35.63", 
  "url": "http://httpbin.org/get"
}

Send data in payload

Literal

Example in JSON

curl --header "Content-Type: application/json" \
  --request PUT \
  --data '{"username":"xyz","password":"xyz"}' \
  http://httpbin.org/put
From file

Use --data-binary, using @ metacharacter to specify file location

curl --header "Content-Type: text/xml" \
  --request PUT \
  --data-binary "@path/to/file" \
  http://httpbin.org/put
Summary

To send:

  • with URL encoding: --data-urlencode
  • a form application/x-www-form-urlencoded:
    • file allowed : --data or --data-ascii
    • file not allowed: --data-raw
  • with no processing (file allowed): --data-binary

Store return value in variable

An example to provide token in request

Get token

TOKEN=$(curl  \
  --request POST  \
  --data "grant_type=password&username=sco.admin%40example.net&password=pix123&scope=pix-orga" \
  http://localhost:3000/api/token |  jq --raw-output .access_token) 

Make request using token (not working, why ?)

curl --header "Authorization: Bearer $(TOKEN)" http://localhost:3000/api/users/me

Read headers in file

An example to store token

Get and store token

 curl  \                                                                          
  --request POST  \
  --data "grant_type=password&username=sco.admin%40example.net&password=pix123&scope=pix-orga" \
  http://localhost:3000/api/token |  jq --raw-output .access_token > token.txt

Build headers file

echo -e "Authorization: Bearer $(cat token.txt)" > headers.txt

Make request using token

curl --header "@./headers.txt" http://localhost:3000/api/users/me | jq .

Send form data

List response: - HTTP code -w "%{http_code}" - headers -D -

Send form data from file and get token from response header

curl \
'http://localhost:3000/api/saml/assert' \
-H 'Content-Type: application/x-www-form-urlencoded'\ 
--data "@./form-data.txt" \
-w "%{http_code}" \ 
-D -

httpie

https://httpie.io/cli

Send JSON with headers

http PUT pie.dev/put X-API-Token:123 name=John

Download

http pie.dev/image/png > image.png

siege

Make performance test to API.

Configuration file is

$HOME/.siege/siege.conf

call a single URL

Make:

  • calls to URL http://localhost:9880
  • using header Cookie
  • 10 clients at once --concurrent
  • during a minute --time
siege --concurrent=10  \
      --header="Cookie: JSESSIONID=BCFFBC2A27426D2D6CF9B5CE9DB7CCE2" \
      --time=60s \ 
      'http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=950590100194&quantiemeAnnee=&numeroDepartement=#selectTraceParPage'

If you want each client to perform the query several time, use --reps (repetitions)

siege --concurrent=10 \
    --header="Cookie: JSESSIONID=BCFFBC2A27426D2D6CF9B5CE9DB7CCE2" \ 
    --reps=10 \
    'http://localhost:9880/ptm-visualisation/resultats-recherche-avancee?dateDebut=2021-07-06&dateFin=2021-07-06&_afficherLesTracesDeTypeEcriture=on&afficherLesTracesDeTypeEcriture=true&_afficherLesTracesDeTypeLecture=on&identifiantRessource=950590100194&quantiemeAnnee=&numeroDepartement=&champRio=&champIdentiteAuteur=&action=modification-etatcivil&organisation=&codeAutorite=&nbTracesParPage=1'

call several URL

Put the scenario to be called in a file

http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=1&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=2&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=3&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=4&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=5&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=6&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=7&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=8&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=9&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=10&quantiemeAnnee=&numeroDepartement=#selectTraceParPage
http://localhost:9880/ptm-visualisation/resultats-recherche-simple?identifiantRessource=11&quantiemeAnnee=&numeroDepartement=#selectTraceParPage

Then pass the file using --file

siege \ 
 --verbose \ 
 --concurrent=1  \
 --header="Cookie: JSESSIONID=BCFFBC2A27426D2D6CF9B5CE9DB7CCE2" \ 
 --reps=1 \ 
 --file=url.txt

report

Response time The average time it took to respond to each simulated user's requests.

Response time The average time it took to respond to each simulated user's requests.

Transaction rate The average number of transactions the server was able to handle per second, in a nutshell: it is the count of all transactions divided by elapsed time.

ab

Apache bench

ab $OPTIONS $URL

$OPTIONS

-n: The number of requests to send
-t: A duration in seconds after which ab will stop sending requests
-c: The number of concurrent requests to make

sniff local calls to outside

Install httpry

sudo apt-get install -y httpry

First, get interface name route

In another terminal, get this ready

    curl --header "Content-Type: application/json" \                                          
      --request PUT \
      --data '{}' \
      http://httpbin.org/put

Then:

  • overview : sudo httpry -i INTERFACE_NAME

You will get

Starting capture on enp0s31f6 interface
2020-05-14 21:31:36 192.168.1.14    34.230.193.231  >   PUT httpbin.org /put    HTTP/1.1    
2020-05-14 21:31:36 34.230.193.231  192.168.1.14    <   -   -   -   HTTP/1.1    100 
  • details: sudo tcpflow -p -c -i INTERFACE_NAME port 80

You wil get

192.168.001.014.45326-034.230.193.231.00080: PUT /put HTTP/1.1
Host: httpbin.org
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json
Content-Length: 35
{"username":"xyz","password":"xyz"}
(..)

Share Internet connection to external device through bluetooth

https://unix.stackexchange.com/questions/320428/how-can-i-share-my-wifi-connection-through-bluetooth

⚠️ **GitHub.com Fallback** ⚠️