CycriptLoader.xm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * CycriptLoader
  3. *
  4. * Load cycript!
  5. *
  6. * By Sam Binger and Kevin Bradley
  7. *
  8. */
  9. /**
  10. This dylib injects into our target process, iterates through which port is available and then posts a distributed notification that our cycripter binary is waiting to hear back
  11. and starts listening with CYListenServer on the specified port. After cycripter echoes out the command the user can succesfully connect to cycript and control the injected process.
  12. */
  13. #import <Foundation/Foundation.h>
  14. #import "Cycript.h"
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. @interface NSDistributedNotificationCenter : NSNotificationCenter
  19. + (id)defaultCenter;
  20. - (void)addObserver:(id)arg1 selector:(SEL)arg2 name:(id)arg3 object:(id)arg4;
  21. - (void)postNotificationName:(id)arg1 object:(id)arg2 userInfo:(id)arg3;
  22. @end
  23. %ctor
  24. {
  25. //check to see if 1337 is available
  26. int sock = socket(AF_INET, SOCK_STREAM, 0);
  27. if(sock < 0) {
  28. NSLog(@"#### cycript runner: socket error\n");
  29. return;
  30. }
  31. printf("Opened %d\n", sock);
  32. in_port_t port = 1337;
  33. struct sockaddr_in serv_addr;
  34. bzero((char *) &serv_addr, sizeof(serv_addr));
  35. serv_addr.sin_family = AF_INET;
  36. serv_addr.sin_addr.s_addr = INADDR_ANY;
  37. serv_addr.sin_port = port;
  38. BOOL usablePort = FALSE;
  39. while(usablePort != TRUE){
  40. if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  41. if(errno == EADDRINUSE) {
  42. NSLog(@"#### cycript runner: the port is not available: %hu already to other process\n", port);
  43. port++;
  44. serv_addr.sin_port = port;
  45. continue;
  46. } else {
  47. NSLog(@"#### cycript runner: could not bind to process (%d) %s\n", errno, strerror(errno));
  48. break;
  49. //return;
  50. }
  51. } else {
  52. usablePort = TRUE;
  53. NSLog(@"#### cycript runner: success with port %i\n", port);
  54. }
  55. }
  56. socklen_t len = sizeof(serv_addr);
  57. if (getsockname(sock, (struct sockaddr *)&serv_addr, &len) == -1) {
  58. perror("getsockname");
  59. //return;
  60. }
  61. //int port = ntohs(serv_addr.sin_port);
  62. NSLog(@"#### cycript runner: port number %d\n", port);
  63. NSString *portNumber = [NSString stringWithFormat:@"%d", port];
  64. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"cycriptPortAvailable" object:nil userInfo:@{@"port": portNumber}];
  65. /*
  66. if (close (sock) < 0 ) {
  67. NSLog(@"#### cycript runner: did not close: %s\n", strerror(errno));
  68. // return;
  69. }
  70. */
  71. CYListenServer(port);
  72. NSLog(@"running server on port: %d", port);
  73. }