{"action":"created","issue":{"url":"https://api.github.com/repos/rapid7/metasploit-framework/issues/10093","repository_url":"https://api.github.com/repos/rapid7/metasploit-framework","labels_url":"https://api.github.com/repos/rapid7/metasploit-framework/issues/10093/labels{/name}","comments_url":"https://api.github.com/repos/rapid7/metasploit-framework/issues/10093/comments","events_url":"https://api.github.com/repos/rapid7/metasploit-framework/issues/10093/events","html_url":"https://github.com/rapid7/metasploit-framework/pull/10093","id":326232950,"node_id":"MDExOlB1bGxSZXF1ZXN0MTkwMzc1NDcy","number":10093,"title":"Add Winsock2 API for Metasploit::Framework::Compiler::Windows","user":{"login":"wchen-r7","id":1170914,"node_id":"MDQ6VXNlcjExNzA5MTQ=","avatar_url":"https://avatars2.githubusercontent.com/u/1170914?v=4","gravatar_id":"","url":"https://api.github.com/users/wchen-r7","html_url":"https://github.com/wchen-r7","followers_url":"https://api.github.com/users/wchen-r7/followers","following_url":"https://api.github.com/users/wchen-r7/following{/other_user}","gists_url":"https://api.github.com/users/wchen-r7/gists{/gist_id}","starred_url":"https://api.github.com/users/wchen-r7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wchen-r7/subscriptions","organizations_url":"https://api.github.com/users/wchen-r7/orgs","repos_url":"https://api.github.com/users/wchen-r7/repos","events_url":"https://api.github.com/users/wchen-r7/events{/privacy}","received_events_url":"https://api.github.com/users/wchen-r7/received_events","type":"User","site_admin":false},"labels":[{"id":103436071,"node_id":"MDU6TGFiZWwxMDM0MzYwNzE=","url":"https://api.github.com/repos/rapid7/metasploit-framework/labels/library","name":"library","color":"fad8c7","default":false},{"id":757242099,"node_id":"MDU6TGFiZWw3NTcyNDIwOTk=","url":"https://api.github.com/repos/rapid7/metasploit-framework/labels/msf5","name":"msf5","color":"c5def5","default":false}],"state":"closed","locked":false,"assignee":{"login":"busterb","id":4108654,"node_id":"MDQ6VXNlcjQxMDg2NTQ=","avatar_url":"https://avatars3.githubusercontent.com/u/4108654?v=4","gravatar_id":"","url":"https://api.github.com/users/busterb","html_url":"https://github.com/busterb","followers_url":"https://api.github.com/users/busterb/followers","following_url":"https://api.github.com/users/busterb/following{/other_user}","gists_url":"https://api.github.com/users/busterb/gists{/gist_id}","starred_url":"https://api.github.com/users/busterb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/busterb/subscriptions","organizations_url":"https://api.github.com/users/busterb/orgs","repos_url":"https://api.github.com/users/busterb/repos","events_url":"https://api.github.com/users/busterb/events{/privacy}","received_events_url":"https://api.github.com/users/busterb/received_events","type":"User","site_admin":false},"assignees":[{"login":"busterb","id":4108654,"node_id":"MDQ6VXNlcjQxMDg2NTQ=","avatar_url":"https://avatars3.githubusercontent.com/u/4108654?v=4","gravatar_id":"","url":"https://api.github.com/users/busterb","html_url":"https://github.com/busterb","followers_url":"https://api.github.com/users/busterb/followers","following_url":"https://api.github.com/users/busterb/following{/other_user}","gists_url":"https://api.github.com/users/busterb/gists{/gist_id}","starred_url":"https://api.github.com/users/busterb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/busterb/subscriptions","organizations_url":"https://api.github.com/users/busterb/orgs","repos_url":"https://api.github.com/users/busterb/repos","events_url":"https://api.github.com/users/busterb/events{/privacy}","received_events_url":"https://api.github.com/users/busterb/received_events","type":"User","site_admin":false}],"milestone":null,"comments":1,"created_at":"2018-05-24T18:09:50Z","updated_at":"2018-06-02T19:37:05Z","closed_at":"2018-06-02T19:36:03Z","author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/rapid7/metasploit-framework/pulls/10093","html_url":"https://github.com/rapid7/metasploit-framework/pull/10093","diff_url":"https://github.com/rapid7/metasploit-framework/pull/10093.diff","patch_url":"https://github.com/rapid7/metasploit-framework/pull/10093.patch"},"body":"This adds Winsock2 API support for Metasploit::Framework::Compiler::Windows.\r\n\r\n# Verification\r\n\r\n## Client Test\r\n\r\n- [x] Save the following as `sock_client.c`. Make sure you modify `RHOST`:\r\n\r\n```c\r\n#include <String.h>\r\n#include <Winsock2.h>\r\n\r\n#define RHOST \"172.16.85.1\"\r\n#define RPORT 4444\r\n\r\nvoid ShowMessage(LPCTSTR msg) {\r\n  MessageBox(NULL, msg, \"Socket Client\", MB_OK);\r\n}\r\n\r\nint main(void) {\r\n  WSADATA wsaData;\r\n  int iResult = NULL;\r\n  WORD version = MAKEWORD(2, 2);\r\n  iResult = WSAStartup(version, &wsaData);\r\n  if (iResult != 0) {\r\n    ShowMessage(\"Failed to do WSAStartup\");\r\n    return -1;\r\n  }\r\n\r\n  SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n  if (sock == INVALID_SOCKET) {\r\n    ShowMessage(\"Failed to do socket\");\r\n    WSACleanup();\r\n    return -1;\r\n  }\r\n\r\n  struct sockaddr_in config;\r\n  config.sin_family = AF_INET;\r\n  config.sin_addr.S_addr = inet_addr(RHOST);\r\n  config.sin_port = htons(RPORT);\r\n\r\n  iResult = connect(sock, &config, sizeof(config));\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to connect\");\r\n    closesocket(sock);\r\n    WSACleanup();\r\n    return -1;\r\n  }\r\n\r\n  char* message = \"Hello World!\\r\\n\";\r\n  iResult = send(sock, message, strlen(message), 0);\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to send message\");\r\n    return -1;\r\n  }\r\n\r\n  iResult = shutdown(sock, SD_SEND);\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to shutdown\");\r\n  }\r\n\r\n  closesocket(sock);\r\n  WSACleanup();\r\n\r\n  return 0;\r\n}\r\n```\r\n\r\n- [x] Start msfconsole, and switch to IRB\r\n- [x] In IRB, write these lines to create the sock_client.exe:\r\n\r\n```ruby\r\nrequire 'metasploit/framework/compiler/windows'\r\nexe = Metasploit::Framework::Compiler::Windows.compile_c(File.read('sock_client.c'))\r\nFile.write('sock_client.exe', exe)\r\n```\r\n\r\n- [x] Move sock_client.exe to Windows machine.\r\n- [x] On your host machine, start a listener. For example, you can do this with netcat: ```nc -l 4444```\r\n- [x] On your Windows machine, execute sock_client.exe. And your netcat should get get `Hello World!`\r\n\r\n## Server Test\r\n\r\n- [x] Save the following as `sock_server.c`:\r\n\r\n```c\r\n#include <Winsock2.h>\r\n#include <String.h>\r\n\r\n#define LPORT \"4444\"\r\n\r\nvoid ShowMessage(LPCTSTR msg) {\r\n  MessageBox(NULL, msg, \"Socket Server\", MB_OK);\r\n}\r\n\r\nint main(void) {\r\n  WSADATA wsaData;\r\n  WORD version = MAKEWORD(2, 2);\r\n  int iResult = WSAStartup(version, &wsaData);\r\n  if (iResult != 0) {\r\n    ShowMessage(\"Failed to do WSAStartup\");\r\n    return -1;\r\n  }\r\n\r\n  struct addrinfo hints;\r\n  struct addrinfo* addrResult = NULL;\r\n  ZeroMemory(&hints, sizeof(hints));\r\n  hints.ai_flags = AI_PASSIVE;\r\n  hints.ai_family = AF_INET;\r\n  hints.ai_socktype = SOCK_STREAM;\r\n  hints.ai_protocol = IPPROTO_TCP;\r\n  iResult = getaddrinfo(NULL, LPORT, &hints, &addrResult);\r\n  if (iResult != 0) {\r\n    ShowMessage(\"Failed to do getaddrinfo\");\r\n    WSACleanup();\r\n    return -1;\r\n  }\r\n\r\n  WSAPROTOCOL_INFO wsaProtoInfo;\r\n  SOCKET sock = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, NULL);\r\n  if (sock == INVALID_SOCKET) {\r\n    ShowMessage(\"Failed to do WSASocket\");\r\n    freeaddrinfo(addrResult);\r\n    return -1;\r\n  }\r\n\r\n  iResult = bind(sock, addrResult->ai_addr, (int) addrResult->ai_addrlen);\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to bind\");\r\n    freeaddrinfo(addrResult);\r\n    return -1;\r\n  }\r\n\r\n  freeaddrinfo(addrResult);\r\n\r\n  iResult = listen(sock, SOMAXCONN);\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to listen\");\r\n    return -1;\r\n  }\r\n\r\n  SOCKADDR_IN clientInfo;\r\n  int clientInfoLen = sizeof(clientInfo);\r\n  SOCKET clientSocket = accept(sock, &clientInfo, &clientInfoLen);\r\n  if (clientSocket == INVALID_SOCKET) {\r\n    ShowMessage(\"Failed to accept\");\r\n    closesocket(clientSocket);\r\n    return -1;\r\n  }\r\n\r\n  char* message = \"Hello World!\\r\\n\";\r\n  iResult = send(clientSocket, message, strlen(message), 0);\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to send\");\r\n    closesocket(clientSocket);\r\n    return -1;\r\n  }\r\n\r\n  iResult = shutdown(clientSocket, SD_SEND);\r\n  if (iResult == SOCKET_ERROR) {\r\n    ShowMessage(\"Failed to shut down\");\r\n    closesocket(clientSocket);\r\n    return -1;\r\n  }\r\n\r\n  closesocket(clientSocket);\r\n  WSACleanup();\r\n\r\n\r\n  return 0;\r\n}\r\n```\r\n\r\n- [ ] Start msfconsole, and switch to IRB.\r\n- [ ] In IRB, write these lines to create sock_server.exe:\r\n\r\n```ruby\r\nrequire 'metasploit/framework/compiler/windows'\r\nexe = Metasploit::Framework::Compiler::Windows.compile_c(File.read('sock_server.c'))\r\nFile.write('sock_server.exe', exe)\r\n```\r\n\r\n- [x] Move `sock_server.exe` to a Windows machine\r\n- [x] Execute `sock_server.exe`. On `netstat -an |find \"4444\"`, you should see port 4444 listening.\r\n- [x] On your host machine, use netcat to connect to the server. For example: `nc [ip] 4444`\r\n- [x] Your netcat should say `Hello World!`"},"comment":{"url":"https://api.github.com/repos/rapid7/metasploit-framework/issues/comments/394111678","html_url":"https://github.com/rapid7/metasploit-framework/pull/10093#issuecomment-394111678","issue_url":"https://api.github.com/repos/rapid7/metasploit-framework/issues/10093","id":394111678,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NDExMTY3OA==","user":{"login":"busterb","id":4108654,"node_id":"MDQ6VXNlcjQxMDg2NTQ=","avatar_url":"https://avatars3.githubusercontent.com/u/4108654?v=4","gravatar_id":"","url":"https://api.github.com/users/busterb","html_url":"https://github.com/busterb","followers_url":"https://api.github.com/users/busterb/followers","following_url":"https://api.github.com/users/busterb/following{/other_user}","gists_url":"https://api.github.com/users/busterb/gists{/gist_id}","starred_url":"https://api.github.com/users/busterb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/busterb/subscriptions","organizations_url":"https://api.github.com/users/busterb/orgs","repos_url":"https://api.github.com/users/busterb/repos","events_url":"https://api.github.com/users/busterb/events{/privacy}","received_events_url":"https://api.github.com/users/busterb/received_events","type":"User","site_admin":false},"created_at":"2018-06-02T19:37:05Z","updated_at":"2018-06-02T19:37:05Z","author_association":"CONTRIBUTOR","body":"## Release Notes\r\n\r\nThis adds Winsock2 API support to the Metasploit C compiler library."}}
{"actor":{"display_login":"busterb"}}