Skip to content

Instantly share code, notes, and snippets.

@SoledaD208
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save SoledaD208/aa12cb296c7ab0bb1d34 to your computer and use it in GitHub Desktop.

Select an option

Save SoledaD208/aa12cb296c7ab0bb1d34 to your computer and use it in GitHub Desktop.

Revisions

  1. SoledaD208 revised this gist Sep 19, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions getIP.py
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,8 @@
    tempRules = tempF.readlines()
    tempF.close()
    for i in re.findall(''' (.+)<br />''', r._content, re.I):
    tempRules.insert(6,'-A INPUT -s ' + i + ' -j VIETNAM-INPUT\n')
    tempRules.insert(6,'-A FORWARD -s ' + i + ' -j VIETNAM-INPUT\n')
    tempRules.insert(8,'-A INPUT -s ' + i + ' -j VIETNAM-INPUT\n')
    tempRules.insert(8,'-A FORWARD -s ' + i + ' -j VIETNAM-INPUT\n')
    ipt = file('/etc/sysconfig/iptables', 'wt')
    rules = "".join(tempRules)
    rules = rules.replace('--dport 22', '--dport ' + sshInput)
  2. SoledaD208 revised this gist Sep 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion getIP.py
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@
    for i in re.findall(''' (.+)<br />''', r._content, re.I):
    tempRules.insert(6,'-A INPUT -s ' + i + ' -j VIETNAM-INPUT\n')
    tempRules.insert(6,'-A FORWARD -s ' + i + ' -j VIETNAM-INPUT\n')
    ipt = file('iptables', 'wt')
    ipt = file('/etc/sysconfig/iptables', 'wt')
    rules = "".join(tempRules)
    rules = rules.replace('--dport 22', '--dport ' + sshInput)
    ipt.write(rules)
  3. SoledaD208 created this gist Sep 15, 2014.
    45 changes: 45 additions & 0 deletions getIP.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # sciprt's written by SoledaD208, email: [email protected]
    # script get national IP from http://www.ipaddresslocation.org, permit all these IP with minimum policy (enable ssh only)
    # block all the foreign traffic
    # script create 2 new chains in Iptables: VIETNAM-INPUT and NOT-VIETNAM-INPUT:
    # accept just ssh protocol in VIETNAM-INPUT chain
    # all these foreign traffic jump to NOT-VIETNAM-INPUT chain and block by default
    # if have internal networks, you should create more chain for these networks, or add smt like this to iptables config file:
    # -A INPUT -i internallIf -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

    import re
    import shutil
    import requests

    # Create payload to get IP
    payload = {'country': 'VN', 'prefix': '', 'output': 'cidr'}

    # Send request to http://www.ipaddresslocation.org
    r = requests.post('http://www.ipaddresslocation.org/ip_ranges/get_ranges.php', data=payload)
    confIpt = raw_input("config iptbles? ")
    if confIpt == 'y' or confIpt == 'Y' or confIpt == 'Yes' or confIpt == 'YES':
    # backup config file
    shutil.copyfile('/etc/sysconfig/iptables', '/etc/sysconfig/iptables.bak')
    print 'current iptables config file is backuped to iptables.bak'
    while True:
    sshInput = raw_input("Which's ssh port? ")
    try:
    ssh = int(sshInput)
    except ValueError:
    print("That's not an int!")
    continue
    else:
    break
    tempF = open('iptablesv5','r')
    tempRules = tempF.readlines()
    tempF.close()
    for i in re.findall(''' (.+)<br />''', r._content, re.I):
    tempRules.insert(6,'-A INPUT -s ' + i + ' -j VIETNAM-INPUT\n')
    tempRules.insert(6,'-A FORWARD -s ' + i + ' -j VIETNAM-INPUT\n')
    ipt = file('iptables', 'wt')
    rules = "".join(tempRules)
    rules = rules.replace('--dport 22', '--dport ' + sshInput)
    ipt.write(rules)
    ipt.close()
    else:
    exit()
    15 changes: 15 additions & 0 deletions iptablesv5
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :VIETNAM-INPUT - [0:0]
    :NOT-VIETNAM-INPUT - [0:0]
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    -A VIETNAM-INPUT -p icmp --icmp-type any -j ACCEPT
    -A VIETNAM-INPUT -p tcp --dport 22 -j ACCEPT
    -A VIETNAM-INPUT -j REJECT --reject-with icmp-host-prohibited
    -A INPUT -j NOT-VIETNAM-INPUT
    -A FORWARD -j NOT-VIETNAM-INPUT
    -A NOT-VIETNAM-INPUT -j DROP
    COMMIT