Guest User

Untitled

a guest
Jul 30th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. Finding and auto-filling HTML login forms in a UIWebView using JavaScript
  2. document.getElementsByTagName('INPUT')[0].value
  3. document.getElementsByTagName('INPUT')[1].value
  4.  
  5. document.getElementsById('id_name').value
  6.  
  7. <input type="text" class="inputtext" name="email" id="email" value="" tabindex="1" />
  8. <input type="password" class="inputtext" name="pass" id="pass" tabindex="2" />
  9.  
  10. <input id="ap_email" name="email" value="" type="email" size="30" maxlength="128" tabindex="1" autocorrect="off" autocapitalize="off" />
  11. <input id="ap_password" name="password" type="password" maxlength="1024" size="20" tabindex="2" onkeypress="displayCapsWarning(event,'ap_caps_warning', this);" class="password"/>
  12.  
  13. <input type="text" tabindex="1" maxlength="20" size="32" name="UID" id="UID">
  14. <input type="password" onkeyup="keyUp(event)" onkeydown="return onlyNumerics(event)" tabindex="2" maxlength="9" size="32" name="PIN" id="PIN" autocomplete="off">
  15.  
  16. <input type="text" spellcheck="false" name="Email" id="Email" value="">
  17. <input type="password" name="Passwd" id="Passwd">
  18.  
  19. document.querySelectorAll("input[type='password']")
  20.  
  21. document.querySelectorAll("input[type='text']")
  22.  
  23. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  24. NSString *savedUsername = @"peter";
  25. NSString *savedPassword = @"Pan123";
  26.  
  27. if (savedUsername.length != 0 && savedPassword.length != 0) {
  28. //create js strings
  29. NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll("input[type='text']");
  30. for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
  31. NSString *loadPasswordJS = [NSString stringWithFormat:@"document.querySelectorAll("input[type='password']").value ='%@'", savedPassword];
  32.  
  33. //autofill the form
  34. [self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
  35. [self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
  36. }
  37. }
  38.  
  39. // Get the HTML from the UIWebView
  40. NSMutableString *html = [[[self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML;"] mutableCopy] autorelease];
  41.  
  42. // Find the range of the first input element.
  43. NSRange firstInputRange = [html rangeOfString:@"<input" options:NSCaseInsensitiveSearch];
  44. // Check if it was found...
  45. if (firstInputRange.location != NSNotFound) {
  46. // Add replace with the populated value attribute appended.
  47. [html replaceCharactersInRange:firstInputRange withString:@"<input value='username'"];
  48.  
  49. // Now do the same for the second input range, checking the html after the first input.
  50. NSRange secondInputRange = [html rangeOfString:@"<input" options:NSCaseInsensitiveSearch range:NSMakeRange(firstInputRange.location+firstInputRange.length, html.length - firstInputRange.location - firstInputRange.length)];
  51. // And if found, append the populated value attribute.
  52. if (secondInputRange.location != NSNotFound) {
  53. [html replaceCharactersInRange:secondInputRange withString:@"<input value='password'"];
  54. }
  55.  
  56. }
  57.  
  58. // Finally, load the amended HTML back into the UIWebView/
  59. [self.webView loadHTMLString:html baseURL:nil];
Add Comment
Please, Sign In to add comment