File size: 5,173 Bytes
6c50d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import copy
import requests

def dict_generator(indict, pre=None):
    pre = pre[:] if pre else []
    if isinstance(indict, dict):
        for key, value in indict.items():
            if isinstance(value, dict):
                for d in dict_generator(value, pre + [key]):
                    yield d
            elif isinstance(value, list) or isinstance(value, tuple):
                for v in value:
                    for d in dict_generator(v, pre + [key]):
                        yield d
            else:
                yield pre + [key, value]
    else:
        yield pre + [indict]

def change_value(jsonData,lst,new_value):
    toModifyJson = copy.deepcopy(jsonData)
    tempJson = toModifyJson

    for i in lst[:-2]:
        tempJson = tempJson[i]
    tempJson[lst[-2]] = new_value

    return toModifyJson

def tamper_all_parameter_values(extractedFlatLists,tamperedValueList):
    tamperedValueJsonList = []
    keyList = []
    tValueList = []
    for flatList in extractedFlatLists:
        #FlatList example format: flatList = ['auth', 'passwordCredentials', 'username', 'USER_NAME'] 

        oriKey = flatList[-2]

        #Value Tampering
        for tValue in tamperedValueList:
            tamperedValueJsonList.append(change_value(jsonData, flatList, tValue))
            keyList.append(oriKey)
            tValueList.append(tValue)

    return tamperedValueJsonList,keyList,tValueList

def change_key(jsonData,lst,new_key):
    toModifyJson = copy.deepcopy(jsonData)
    tempJson = toModifyJson

    for i in lst[:-2]:
        tempJson = tempJson[i]
    tempJson.pop(lst[-2])
    tempJson[new_key] = lst[-1]

    return toModifyJson

def tamper_all_parameter_keys(extractedFlatLists,tamperedKeyList):
    tamperedKeyJsonList = []
    tKeyList = []
    valueList = []
    for flatList in extractedFlatLists:
        #FlatList example format: flatList = ['auth', 'passwordCredentials', 'username', 'USER_NAME'] 
        oriValue = flatList[-1]

        #Key Tampering
        for tKey in tamperedKeyList:
                tamperedKeyJsonList.append(change_key(jsonData, flatList, tKey))
                tKeyList.append(tKey)
                valueList.append(oriValue)

    return tamperedKeyJsonList,tKeyList,valueList

def send_all_methods(http_methods,url,payload,key,value):
    for method in http_methods:
        response = requests.request(method,url,json=payload)
        #print(key,":",value,"     ",method,"    ",response.status_code,"    ",len(response.content))
        print("{:20.15}:{:20.15}{:10.8}{:15}{:10}".format(key, value, method, str(response.status_code), str(len(response.content))))

if __name__ == "__main__":

    #Read from textfile/input
    jsonData = {"auth":
        {"passwordCredentials":
            {"username": "USER_NAME",
              "password":"PASSWORD"}
        }
    }

    #Doesn't work for json nested using List
    #Only works for json nested using Dictionary
    #Eg:
    '''
    jsonData = {
   "username" : "my_username",
   "password" : "my_password",
   "validation-factors" : {
      "validationFactors" : [
         {
            "name" : "remote_address",
            "value" : "127.0.0.1"
         }
      ]
   }
}
'''

    HTTP_METHODS_RESTFUL = ['GET','POST','PUT','DELETE','PATCH']
    HTTP_METHODS_NON_RESTFUL = ['HEAD','OPTIONS','TRACE']
    HTTP_METHODS_POLLUTED = ['GVT','TEST','-1']
    HTTP_METHODS = HTTP_METHODS_RESTFUL + HTTP_METHODS_NON_RESTFUL + HTTP_METHODS_POLLUTED

    extractedFlatLists = list(dict_generator(jsonData))
    tamperedValueList = ['qwerty','<script> alert(\'xss\')</script>','<img baseline=1 onerror=alert("XSS")>'] #Read from textfile
    tamperedKeyList = ['qwerty','<script> alert(\'xss\')</script>','<img baseline=1 onerror=alert("XSS")>'] #Read from textfile

    tamperedValueJsonList,tKeyList,valueList = tamper_all_parameter_values(extractedFlatLists,tamperedValueList)
    #print("TamperedValueJSONList: ",tamperedValueJsonList)
    tamperedKeyJsonList,keyList,tValueList = tamper_all_parameter_keys(extractedFlatLists,tamperedKeyList)
    #print("TamperedKeyJSONList: ",tamperedKeyJsonList)
    combinedTamperedJsonList = tamperedValueJsonList + tamperedKeyJsonList
    combinedKeyList = tKeyList + keyList
    combinedValueList = valueList + tValueList
    print("Request Prepared. Sending requests..")
    print("--------------------------------------------------------------------------------------")


    #response = requests.request("CONNECT","http://127.0.0.1:3000/user",json=tamperedValueJsonList[0])
    #print(response)
    print("{:20}{:20}{:10}{:15}{:10}".format("Key","Value", "Method", "Response Code", "Response Length"))
    #print("Modified Parameter    Method      Response Code    Response Length")
    print("--------------------------------------------------------------------------------------")

    for i in range(0,len(combinedTamperedJsonList)):
        #May need to create custom request to modify headers to spoof as a real web request 
        #Modify the URL accordingly
        send_all_methods(HTTP_METHODS_RESTFUL,"http://127.0.0.1:3000/user",combinedTamperedJsonList[i], combinedKeyList[i], combinedValueList[i])