1

Hello everyone I am reading from this text file that has all the fields I want

{"Tables":
[
    {
        "Name": "Event",
        "EventType": "Summary",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
        {
        "Name": "Event",
        "EventType": "Account",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
      {
        "Name": "Event",
        "EventType": "Agent",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
      {
        "Name": "Event",
        "EventType": "Server",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
    {
        "Name": "ADUser",
        "Fields": "*",
        "SortBy": "",
        "SortType": "None"
    }
]

}

I am using logic that goes through all the fields and input the response into the get request json parameter

with open('tables.txt') as json_file:
    data = json.load(json_file)
    for p in data['Tables']:
        tableName = p['Name']
        sortBy = p['SortBy']
        lastGot = p['Last']
        fields = p['Fields']
        sortType = p['SortType']
        eventType = p['EventType']
        fileName = ""
        sql = ""

        if (tableName == 'Event'):
            sql = "SELECT " + fields + " FROM " + tableName + " WHERE Event.EventType IN ('" + eventType + "') AND " + tableName + "." + sortBy + " > DateFunc('" + lastGot + "') ORDER BY " + tableName + "." + sortBy + ";"

            fileName = "Event-" + eventType
        else:
            if (sortType == 'None'):
                sql = "SELECT " + fields + " FROM " + tableName + ";";
            else:
                sql = "SELECT " + fields + " FROM " + tableName + " WHERE " + tableName + "." + sortBy + " > DateFunc('" + lastGot + "') ORDER BY " + tableName + "." + sortBy + ";"

            fileName = tableName

event_data = sql
#print(event_data)




r = requests.get(url, headers=headers, json={"script": event_data},
                 verify=verify).json()


reponseobject = r
jsonlist = json.dumps(reponseobject)


print(jsonlist)

But it throws an error which I do not understand how to rectify it when all the fields i want are there any suggestions ?

 Traceback (most recent call last):
  File "C:/Users/PycharmProjects/Vulnerabilities/main.py", line 31, in <module>
    lastGot = p['Last']
KeyError: 'Last'
{'Name': 'Event', 'EventType': 'Summary', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('Summary') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;
{'Name': 'Event', 'EventType': 'Account', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('Account') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;
{'Name': 'Event', 'EventType': 'Agent', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('Agent') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;
{'Name': 'Event', 'EventType': 'server', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('server') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;

Here is the result where I tried to catch p

{'Name': 'Event', 'EventType': 'Summary', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
{'Name': 'Event', 'EventType': 'Account', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
{'Name': 'Event', 'EventType': 'Agent', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
{'Name': 'Event', 'EventType': 'server', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
error occurred
malik
  • 31
  • 6
  • 1
    Looks fine. I suspect the data is not what you think it is. Catch the exception and print `p`. – Peter Wood Jan 20 '20 at 01:20
  • I agree with the above comment. Catch it and try to print `p`. – Raymond C. Jan 20 '20 at 02:27
  • 1
    Why the parentheses around the if statements? Also, variable and function names should follow the `lower_case_with_underscores` style. – AMC Jan 20 '20 at 02:32
  • @PeterWood thank you for responding but what do you mean by the data is not what think it is ? I mean "Last" is just is supposed to carry date and time of when data is supposed to come in . I isolated the for loop and it prints out the text file no problem yet the Keyerror still occurs this issue is very mindboggling – malik Jan 20 '20 at 14:05
  • What is the result of printing p? Edit the question. [mcve] – Peter Wood Jan 20 '20 at 14:16
  • @PeterWood thank you for helping I was able to figureout my problem my 5th field didnt have Last haha such a simple mistake. I do have one more question regarding this is that for the api get request can I go through each field get this response ```SELECT * FROM Event WHERE Event.EventType IN ('Summary') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;``` and input it into my json parameter? – malik Jan 20 '20 at 18:47
  • I don't know enough about json and SQL. You're better searching for an answer, and trying to formulate a [mcve] if you can't find what you need. At the moment you're asking just me. You've reduced your chances of getting help from thousands to one. – Peter Wood Jan 20 '20 at 20:16
  • Does this answer your question? [I'm getting Key error in python](https://stackoverflow.com/questions/10116518/im-getting-key-error-in-python) – AMC Jan 23 '20 at 01:12

1 Answers1

1

Your 5th field doesn't have any Last key.

    {
        "Name": "ADUser",
        "Fields": "*",
        "SortBy": "",
        "SortType": "None"
    }

Make sure you have this Last key. Or, if you know that it'll be missing and thats expected behavior, then use dict.get method to use a default.

lastGot = p.get('Last', 'DEFAULT_LAST')
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Wow thank you cant believe the was the issue thank you but if i have another question to this that I have encountered ```AttributeError: 'str' object has no attribute 'read'``` – malik Jan 20 '20 at 17:18