Guest User

Untitled

a guest
Jan 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. /*
  2. gcc -g -framework Foundation -framework Security test.c -o test
  3. */
  4.  
  5.  
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include <Security/Security.h>
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10.  
  11. void error(const char* msg) {
  12. fprintf(stderr, "error: %s\n", msg);
  13. exit(-1);
  14. }
  15.  
  16. void check_status(OSStatus status) {
  17. if(status == noErr) return;
  18.  
  19. CFShow(SecCopyErrorMessageString(status, NULL));
  20. exit(-1);
  21. }
  22.  
  23. void write_pl(CFMutableDictionaryRef properties) {
  24. CFWriteStreamRef stdoutStream = NULL;
  25.  
  26. CFURLRef devStdout = CFURLCreateWithFileSystemPath(
  27. NULL,
  28. CFSTR("/dev/stdout"),
  29. kCFURLPOSIXPathStyle,
  30. false
  31. );
  32.  
  33. stdoutStream = CFWriteStreamCreateWithFile(NULL, devStdout);
  34. if (stdoutStream == NULL)
  35. error("cannot create CFWriteStream for /dev/stdout");
  36.  
  37. if (!CFWriteStreamOpen(stdoutStream))
  38. error("cannot open CFWriteStream for /dev/stdout");
  39.  
  40. CFPropertyListWrite(
  41. properties,
  42. stdoutStream,
  43. kCFPropertyListXMLFormat_v1_0,
  44. 0,
  45. NULL
  46. );
  47.  
  48. CFWriteStreamClose(stdoutStream);
  49. }
  50.  
  51.  
  52. int main (int argc, char const *argv[])
  53. {
  54. if(argc != 2) {
  55. fprintf(stderr, "USAGE: %s [password]\n", argv[0]);
  56. exit(-1);
  57. }
  58.  
  59. CFStringRef target = CFStringCreateWithCString (kCFAllocatorDefault, argv[1], kCFStringEncodingUTF8);
  60. OSStatus ok;
  61.  
  62. SecKeychainSearchRef searchRef = NULL;
  63. ok = SecKeychainSearchCreateFromAttributes (
  64. NULL,
  65. kSecInternetPasswordItemClass,
  66. NULL,
  67. &searchRef
  68. );
  69. check_status(ok);
  70.  
  71. CFMutableDictionaryRef results = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
  72. SecKeychainItemRef searchItem = NULL;
  73.  
  74. while (SecKeychainSearchCopyNext(searchRef, &searchItem) != errSecItemNotFound) {
  75. SecKeychainAttributeList attrList;
  76.  
  77. SecKeychainAttribute attrs[2];
  78. attrs[0].tag = kSecServerItemAttr;
  79. attrs[0].length = 0;
  80. attrs[0].data = NULL;
  81.  
  82. attrs[1].tag = kSecAccountItemAttr;
  83. attrs[1].length = 0;
  84. attrs[1].data = NULL;
  85.  
  86. attrList.count = 2;
  87. attrList.attr = attrs;
  88.  
  89. UInt32 length = 0;
  90. void* data = NULL;
  91.  
  92. ok = SecKeychainItemCopyContent(
  93. searchItem,
  94. NULL,
  95. &attrList,
  96. &length,
  97. &data
  98. );
  99.  
  100. check_status(ok);
  101.  
  102. CFStringRef server = CFStringCreateWithBytes(
  103. kCFAllocatorDefault,
  104. attrs[0].data,
  105. attrs[0].length,
  106. kCFStringEncodingUTF8,
  107. false
  108. );
  109.  
  110. CFStringRef username = CFStringCreateWithBytes(
  111. kCFAllocatorDefault,
  112. attrs[1].data,
  113. attrs[1].length,
  114. kCFStringEncodingUTF8,
  115. false
  116. );
  117.  
  118. CFStringRef password = CFStringCreateWithBytes(
  119. kCFAllocatorDefault,
  120. data,
  121. length,
  122. kCFStringEncodingUTF8,
  123. false
  124. );
  125.  
  126. if(CFStringCompare(password, target, 0) == 0) {
  127. CFDictionaryAddValue(results, server, username);
  128. };
  129.  
  130. SecKeychainItemFreeContent(&attrList, data);
  131. CFRelease(searchItem);
  132. }
  133.  
  134. CFRelease(searchRef);
  135. write_pl(results);
  136. return 0;
  137. }
Add Comment
Please, Sign In to add comment