Sunday, 5 April 2020

Ping an IP range from the command line interface (CLI)

If you ever wanted to do host discovery while no specific network scanning tools were available (e.g. nmap), or you wanted to avoid creating a script file (e.g. due to having no write permissions)? Here are a couple of useful commands depending on the OS of your choice:

Windows OS:
for /L %i IN (1,1,254) DO ping -n 1 -w 1 192.168.1.%i| FIND /i "Reply" >> IPs.txt

Note: command prompt has a limitation when asked to return the exact string using regex and returns the whole line. Here is an example of replacing find with findstr in order to use a regular expression (regex). 
findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"

The regular expression can still be useful in cases such as:
ipconfig | findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"


Linux OS:
for i in {1..254}; do (ping -c 1 -i 1 192.168.1.$i >/dev/null && echo "192.168.1.$i" &); done

Note: The above command will only list the discovered IP address, without any additional text.

Tip/Trick: Did you know you can use apr to achieve the same results but much faster. This seems to be reliable under Linux. The following command will list the discovered host on your network, including any additional information per IP (including the IPv6 address).
for ip in $(seq 1 254); do arp -n 192.168.1.$ip | grep on; done

The following command however, will list only the IP addresses without any additional text (also avoiding to specify the IP range twice)
for ip in $(seq 1 254); do arp -n 192.168.1.$ip | grep on | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' ; done





HOST DISCOVERY WITHOUT NMAP OR SCRIPT FILES
===========================================

Useful when no scanning tools are installed, or you can't write a script
file (e.g. no write permissions). Set the network prefix once at the start
of each command (n= / net=) instead of editing the IP in several places.

Key idea: a ping sweep finds hosts that answer ICMP. Many hosts block ICMP
(Windows Firewall does by default), but every host on your local subnet
must still answer ARP. So run the sweep first, then read the ARP cache:
that catches the hosts ping alone misses. ARP results only cover your own
local subnet; hosts beyond a router only show up via ping.


-------------------------------------------------------------------------
WINDOWS (cmd)
-------------------------------------------------------------------------

1) Ping sweep, IP addresses only, saved to IPs.txt:

for /L %i in (1,1,254) do @for /f "tokens=3 delims=: " %a in ('ping -n 1 -w 300 192.168.1.%i ^| find "TTL="') do @echo %a>>IPs.txt

Notes:
- -w is in milliseconds (-w 1 = 1 ms, far too short). 200-500 ms suits a LAN.
- Filtering on "TTL=" instead of "Reply" avoids false positives such as
  "Reply from 192.168.1.1: Destination host unreachable", and also works
  on non-English Windows.
- In a .bat file, write %%i and %%a instead of %i and %a.
- The loop is sequential, so a full /24 can take about a minute.

2) Then add hosts that blocked ping but answered ARP:

arp -a | findstr /i "dynamic"

3) findstr and regular expressions:
findstr has no "print only the match" option (no equivalent of grep -o);
it always returns the whole line. It also doesn't support {n} quantifiers.
It is still useful for matching lines, e.g.:

ipconfig | findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"

To extract only the IP, use "for /f" tokenizing (as in #1) or PowerShell.


-------------------------------------------------------------------------
WINDOWS (PowerShell 5.1+, parallel, finishes in seconds)
-------------------------------------------------------------------------

1) Ping sweep, IP addresses only:

$n='192.168.1'; $t=1..254|%{(New-Object Net.NetworkInformation.Ping).SendPingAsync("$n.$_",500)}; [Threading.Tasks.Task]::WaitAll($t); $t.Result|?{$_.Status -eq 'Success'}|%{$_.Address.IPAddressToString}

2) ARP cache afterwards (IP + MAC):

Get-NetNeighbor -AddressFamily IPv4 -State Reachable,Stale,Delay,Probe | ? IPAddress -like "$n.*" | select IPAddress,LinkLayerAddress


-------------------------------------------------------------------------
LINUX (bash)
-------------------------------------------------------------------------

1) Parallel ping sweep, IP addresses only, sorted:

net=192.168.1; for i in {1..254}; do (ping -c1 -W1 $net.$i &>/dev/null && echo $net.$i) & done | sort -t. -k4n; wait

Notes:
- -W is the reply timeout in seconds. (-i is the interval between pings,
  which does nothing with -c 1.)
- "wait" stops results printing after the prompt returns, and stops
  redirected/piped output from being cut off.

2) Sweep + ARP cache (recommended; also catches ICMP-blocking hosts):

net=192.168.1; for i in {1..254}; do ping -c1 -W1 $net.$i &>/dev/null & done; wait; ip -4 neigh show to $net.0/24 | awk '!/FAILED|INCOMPLETE/{print $1}' | sort -t. -k4n

To include MAC addresses, replace {print $1} with {print $1, $5}.

Notes on ARP:
- arp / ip neigh send nothing; they only read the local cache of hosts
  your machine has recently talked to. That's why they are run after a
  sweep, not instead of one.
- The extra column is the MAC (hardware) address, not an IPv6 address.
- "arp" (net-tools) is deprecated on modern Linux; use "ip neigh".
- Dump the table once instead of calling arp 254 times.

3) Bonus: IPv6 discovery in one packet (replace eth0 with your interface):

ping -6 -c2 ff02::1%eth0 >/dev/null; ip -6 neigh show dev eth0

This pings the link-local all-nodes multicast address, then lists the
IPv6 neighbors that answered.

Portability:
- {1..254} and &> are bash-specific. Under plain sh, use $(seq 1 254)
  and >/dev/null 2>&1.


-------------------------------------------------------------------------
SCANNING MORE THAN ONE /24 (RANGES THAT CROSS OCTETS)
-------------------------------------------------------------------------

Two cases to keep separate:

  A) Whole subnets, e.g. 192.168.2.0 through 192.168.5.255
     -> just nest a loop over the third octet.

  B) A precise range, e.g. 192.168.2.3 through 192.168.5.67
     -> convert start/end to integers, loop over the integers, convert
        each back to a dotted IP. This is the only clean way to start and
        stop partway through a subnet.

A full multi-subnet sweep launches hundreds or thousands of pings. Throttle
the parallelism (xargs -P on Linux, the async batch on Windows), and expect
it to take longer and put noticeable traffic on the wire.


--- Linux (bash) ---

A) Whole subnets 192.168.2-5.x, sweep + ARP cache:

net=192.168; for o3 in {2..5}; do for o4 in {1..254}; do ping -c1 -W1 $net.$o3.$o4 &>/dev/null & done; done; wait; ip -4 neigh show | awk '!/FAILED|INCOMPLETE/{print $1}' | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n

B) Precise range 192.168.2.3 - 192.168.5.67, throttled with xargs:

s=$(( (192<<24)+(168<<16)+(2<<8)+3 )); e=$(( (192<<24)+(168<<16)+(5<<8)+67 )); for ((i=s;i<=e;i++)); do printf '%d.%d.%d.%d\n' $((i>>24&255)) $((i>>16&255)) $((i>>8&255)) $((i&255)); done | xargs -P256 -I{} sh -c 'ping -c1 -W1 {} >/dev/null 2>&1 && echo {}' | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n

Change the four numbers in s=(...) and e=(...) to set the start and end IP.
After it finishes, read the ARP cache to add ICMP-blocking hosts:

ip -4 neigh show | awk '!/FAILED|INCOMPLETE/{print $1}' | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n


--- Windows (PowerShell 5.1+) ---

Paste these two helpers into the session once:

function ip2int($ip){ $b=$ip -split '\.'; [uint32]$b[0]*16777216 + [uint32]$b[1]*65536 + [uint32]$b[2]*256 + [uint32]$b[3] }
function int2ip([uint32]$i){ '{0}.{1}.{2}.{3}' -f (($i -shr 24) -band 255),(($i -shr 16) -band 255),(($i -shr 8) -band 255),($i -band 255) }

Precise range 192.168.2.3 - 192.168.5.67, parallel:

$s=ip2int '192.168.2.3'; $e=ip2int '192.168.5.67'; $ips=for($i=$s;$i -le $e;$i++){int2ip $i}; $t=$ips|%{(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,500)}; [Threading.Tasks.Task]::WaitAll($t); 0..($t.Count-1)|?{$t[$_].Result.Status -eq 'Success'}|%{$ips[$_]}

For whole subnets just pass '192.168.2.0' and '192.168.5.255' as start/end.
A for-loop counter is used instead of the .. range operator on purpose:
these IPs exceed int32, which .. can't handle in PowerShell 5.1.


--- Windows (cmd) ---

Whole subnets 192.168.2-5.x, IPs to IPs.txt (nested for /L):

for /L %j in (2,1,5) do @for /L %i in (1,1,254) do @for /f "tokens=3 delims=: " %a in ('ping -n 1 -w 300 192.168.%j.%i ^| find "TTL="') do @echo %a>>IPs.txt

In a .bat file use %%j, %%i and %%a. For a precise start/end that isn't a
whole subnet (starting at .3, ending at .67), use the PowerShell version
above - integer math is far cleaner there than in cmd.


-------------------------------------------------------------------------
macOS / BSD
-------------------------------------------------------------------------

- ping -W is in milliseconds on macOS, so use -W 1000.
- ARP cache, IP addresses only:

arp -an | grep -v incomplete | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'

(The original "grep on" filter matches this BSD/macOS output format,
e.g. "? (192.168.1.1) at aa:bb:cc:dd:ee:ff on en0", but not Linux output.)


Friday, 3 April 2020

Awareness around COVID-19 SMS Phishing (Smishing)

This blog-post discusses an issue known for almost 20years, which is related to the online SMS platforms. Given the recent pandemic and the use of the GSM network for sending SMS notifications to the public, it in an opportunity to raise awareness regarding Smishing (SMS Phishing) attempts, targeting the public which is affected by COVID-19.


In Greece, the number 13033 is being used to send SMS confirmations to people who use this particular service, which is used for registering in advance their daily movements when it comes to get essential goods (such as, going to the supermarket, the pharmacy, etc.) before they exit their homes. Due to the COVID-19 pandemic, this process attempts to limit people’s unnecessary movement(s), in an attempt to minimise the risk of getting affected, or contaminating others in case the person is a carrier of the virus.

Given the importance of the pandemic and the necessity of this service, it is mandatory to mention that it is possible to spoof the SEND ID in order to send SMS updates to recipients pretending to be from the original 13033 service number. This action, have the potential to trick the recipients (general public) in clicking on malicious links, or by using Social Engineering (and potentially scare tactics) to ask recipients to pay a fine that has been imposed. 

"Given the current situation, it would be beneficial to everyone if the Ministry in Greece responsible for operating the 13033 service (and other Ministries around the globe that use similar services), could promote a campaign educating all recipients regarding the potential threat of Smishing"

More specifically, the public should be informed that they should under no circumstances visits any URLs received by the 13033 service and that the 13033 service will not send any messages requesting to pay any fines. The public need to be aware that in case they receive such messages, these should be ignored and deleted.  

The proof of concept for the alluded was tested and proven by @DimisMeu and we decided to publish this blog post in order to be able to raise the necessary awareness. 

Saturday, 28 March 2020

Command line (CLI) check and monitor COVID-19 statistics

Use the following command to quickly check for COVID-19 (corona virus) statistics from the console (CLI), for the countries you want to monitor.


Command line:
clear && curl -s https://corona-stats.online/?minimal=true | grep "Rank\|(DE)\|(IT)\|(ES)\|(GR)\|(US)\|(AE)\|(GB)\|(CH)\|(AT)\|(NL)"


Modify the command as needed with any valid ISO 3166-1 Alpha-2 Country Codes. 

Thanks goes to @ekrysis for collecting the data sources and providing the means to query the information. See more at GitHub: https://github.com/sagarkarira/coronavirus-tracker-cli 

Some examples:
# Get data from worldometers api i.e source 2
curl https://corona-stats.online

Friday, 5 July 2019

Mac OS - Show the full path in the Finder title bar

A very useful feature that you might want to enable on Mac OS is to always have the full path of the working directory displayed in the Finder title bar. 




















> Using the command line, run the following command to activate it:
defaults write com.apple.finder _FXShowPosixPathInTitle -bool true; killall Finder

> To deactivate the feature run the command:
defaults write com.apple.finder _FXShowPosixPathInTitle -bool false; killall Finder

NOTE: The above commands will terminate all of your open Finder windows. If you do not wish to have your Finder windows closed, you can run the commands without the
"; killall Finder" part. In that case, only new windows will have the full path until you close and reopen the ones that do not have it. 


How to "cheat" from reopening all of your Finder windows:
  • Option A) On your Finder icon in your Dock, hold the ⌥ (alt/option) key and Right-Click, then Click Relaunch.
  • Option B) Bring in-focus a Finder window. Hold the key Shift on the keyboard and click the  menu. The option Force Quit Finder will be available.  
Tip: You can Show/Hide the Path Bar at the bottom of the Finder windows at anytime from the menu. Bring in-focus a Finder window and to to View > Show Path Bar. This is useful if you need to instantly navigate up several folders. 

Bonus: Show/Hide the hidden files in Finder. 
  1. Open the folder where you want to display the hidden files in Finder.
  2. Hold down the keys Command, Shift and Period: cmd + shift + .
  3. The hidden files and folders on your Mac will then appear partially transparent.

Saturday, 22 June 2019

Security BSides Athens 2019

Security BSides Athens 2019 was the 4th Ethical Hacking / Information Security / Cybersecurity conference that took place in Athens, Greece. Once again we created a conference for the information security community, by the information security community, with a special thanks to all our volunteers. 

Due to the feedback we received we kept the same location again this year allowing more people to explore Athens Impact Hub. We try our best to make the event a unique experience each year and a day to look forward to. We have dedicated ourselves in having more quality in the event, rather that focus on quantity. 


Security BSides Athens 2019 (www.bsidesath.gr) took place at Impact Hub Athens (link). (All the information for our previous events is archived and can be found here: https://www.bsidesath.gr/index.php#Pevents)
This year we managed to reach 230 attendees throughout the day exceeding our expectation. 

We are the conference that introduced the "gamification" of the CTF, and we are very proud that we did that again this year! We tried to have a realistic CTF scenario with the help of our CTF partner Hack The Box, while demonstrating the ethical side of hacking. 

We look forward to seeing you at
Security BSides Athens 2020!