Help with Json and Python

Asked

Viewed 95 times

-2

So I’m doing a script on python to the Python-Nmap and I have a file Json where I store information such as default ip and default port and I created in the options menu and an option to change the default ip but I don’t know how to make him change the file Json.

JSON:

{
  "DataInfo":[
    {
      "defaultportrange": "22-445",
      "defaultipaddress": "127.0.0.1"
    }
  ]
}

MENU CODE

When the user chooses option 5 in the menu he asks this:

OS()
newip = input("Please enter your new default ip address: ")

Now I don’t know how to change the user input in the Json file.

COMPLETE CODE

import nmap
import os
import sys
import json

nm = nmap.PortScanner()

def OS():
    os.system('cls' if os.name == 'nt' else 'clear')

def FullNetworkScan():

    nm.scan(ipaddr, portrange, '-v')

    OS()
    print("Colected data:")
    print("--------------")
    print("Ip Status: ", nm[ipaddr].state())
    print("Port Range: ", portrange)
    print("HostName: ", nm[ipaddr].hostname())
    print("Scanned Hosts: ", nm.all_hosts())
    print("Open TCP Ports: ", nm[ipaddr].all_tcp())
    print("Open UDP Ports: ", nm[ipaddr].all_udp())
    print("Open SCTP Ports: ", nm[ipaddr].all_sctp())
    print("Open IP Ports: ", nm[ipaddr].all_ip())
    sys.exit()

def TCPScan():

    nm.scan(ipaddr, portrange, '-v')

    OS()
    print("Colected data:")
    print("--------------")
    print("Ip Status: ", nm[ipaddr].state())
    print("Port Range: ", portrange)
    print("HostName: ", nm[ipaddr].hostname())
    print("Scanned Hosts: ", nm.all_hosts())
    print("Open TCP Ports: ", nm[ipaddr].all_tcp())
    sys.exit()

def UDPScan():

    nm.scan(ipaddr, portrange, '-v')

    OS()
    print("Colected data:")
    print("--------------")
    print("Ip Status: ", nm[ipaddr].state())
    print("Port Range: ", portrange)
    print("HostName: ", nm[ipaddr].hostname())
    print("Scanned Hosts: ", nm.all_hosts())
    print("Open UDP Ports: ", nm[ipaddr].all_udp())
    sys.exit()

def SpecificPort():

    nm.scan(ipaddr, portrange, '-v')

    OS()
    print("Colected data:")
    print("--------------")
    print("Ip Status: ", nm[ipaddr].state())
    print("Selected Port: ", port)
    print("HostName: ", nm[ipaddr].hostname())
    print("Scanned Hosts: ", nm.all_hosts())
    print("UDP OPEN: ", nm[ipaddr].has_udp(int(port)))
    print("TCP OPEN: ", nm[ipaddr].has_tcp(int(port)))
    print("SCTP OPEN: ", nm[ipaddr].has_sctp(int(port)))
    sys.exit()

def menu():

    global menuoption
    global ipaddr
    global port
    global state
    global portrange
    global defaultipaddr
    global optionip
    global optionport
    global newip
    global newport

    with open('Data/DATA.json') as f:
        data = json.load(f)

    for DataInfo in data['DataInfo']:
        portrange = DataInfo['defaultportrange']
        defaultipaddr = DataInfo['defaultipaddress']

    OS()

    print("Welcome to Python-Nmap Scanner made by DeAdSeC")
    print("----------------------------------------------")
    print("What type of scan would you like to run?")
    print("1) Full Network Scan")
    print("2) TPC Scan")
    print("3) UDP Scan")
    print("4) Scan for a specified port")
    print("5) Change default ip address")
    print("6) Change default port range")

    menuoption = int(input())

    if menuoption == 1:
        OS()
        print("Would you like to use the default ip? [", defaultipaddr, "]")
        optionip = input()

        if optionip == "Y":

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return FullNetworkScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            if optionport == "y":

                return FullNetworkScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "N":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return FullNetworkScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            if optionport == "y":

                return  FullNetworkScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "y":

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                nm.scan(ipaddr, portrange, '-v')

                return FullNetworkScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            if optionport == "y":

                return FullNetworkScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "n":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return FullNetworkScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            if optionport == "y":

                return FullNetworkScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return FullNetworkScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        else:
            print("Use only Y/N or y/n")
            return menu()

    if menuoption == 2:

        OS()
        print("Would you like to use the default ip? [", defaultipaddr, "]")
        optionip = input()

        if optionip == "Y":

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return TCPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            if optionport == "y":

                return TCPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "N":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return TCPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            if optionport == "y":

                return  TCPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "y":

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                nm.scan(ipaddr, portrange, '-v')

                return FTCPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            if optionport == "y":

                return TCPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "n":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return TCPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            if optionport == "y":

                return TCPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return TCPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        else:
            print("Use only Y/N or y/n")
            return menu()

    if menuoption == 3:

        OS()
        print("Would you like to use the default ip? [", defaultipaddr, "]")
        optionip = input()

        if optionip == "Y":

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return UDPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            if optionport == "y":

                return UDPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "N":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return UDPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            if optionport == "y":

                return  UDPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "y":

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                nm.scan(ipaddr, portrange, '-v')

                return UDPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            if optionport == "y":

                return UDPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        if optionip == "n":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            OS()
            print("Would you like to use the default port range? [", portrange, "]")
            optionport = input()

            if optionport == "Y":

                return UDPScan()

            if optionport == "N":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            if optionport == "y":

                return UDPScan()

            if optionport == "n":

                OS()
                print("Please use this format for the port range: MinPort-MaxPort")
                portrange = input("What port range would you want me to use: ")

                return UDPScan()

            else:
                print("Please use only Y/N or y/n")
                return menu()

        else:
            print("Use only Y/N or y/n")
            return menu()

    if menuoption == 4:
        OS()
        print("Would you like to use the default ip? [", defaultipaddr, "]")
        optionip = input()

        OS()
        port = input("Please enter the port you wanna scan: ")

        if optionip == "Y":

            return  SpecificPort()

        if optionip == "N":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            return SpecificPort()

        if optionip == "y":

            return SpecificPort()

        if optionip == "n":

            OS()
            defaultipaddr = input("Please enter your ip address: ")

            return SpecificPort()

    if menuoption == 5:

        OS()
        newip = input("Please enter your new default ip address: ")

    if menuoption == 6:
        pass

    else:
        OS()
        print("You fucked up a simple shit!")
        return menu()
menu()

1 answer

1


To change the JSON file, the best way is to change in memory and then rewrite the changed file:

OS()
newip = input("Please enter your new default ip address: ")
data['DataInfo'][0]['defaultipaddress'] = defaultipaddr = newip 
with open('Data/DATA.json', 'w') as f:
    json.dump(data, f) 
  • I added this code in my script but it is giving me this error: Traceback (most recent call last):&#xA; File "scanner.py", line 637, in <module>&#xA; menu()&#xA; File "scanner.py", line 627, in menu&#xA; data['DataInfo']['defaultportrange'] = portrange = newport&#xA;TypeError: list indices must be integers or slices, not str

  • @Deadsec Oops, I didn’t see you had a list there - I edited the answer

  • I could give +1 in the publication ! Apparently I was ban the stack until I hit better points in the publications!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.