Package: mitmproxy2swagger
mitmproxy2swaggerhttps://user-images.githubusercontent.com/5400940/168086818-c48f60ab-3f95-42eb-b435-c8b1a6326b81.mp4A tool for automatically convertingmitmproxycaptures toOpenAPI 3.0specifications. This means that you can automatically reverse-engineer REST APIs by just running the apps and capturing the traffic.🆕 NEW!Added support for processing HAR exported from the browser DevTools. SeeUsage - HARfor more details.InstallationFirst you will need python3 and pip3.$pipinstallmitmproxy2swagger# ... or ...$pip3installmitmproxy2swagger# ... or ...$gitclonegit@github.com:alufers/mitmproxy2swagger.git
$cdmitmproxy2swagger
$dockerbuild-tmitmproxy2swagger.Then clone the repo and runmitmproxy2swaggeras per examples below.UsageMitmproxyTo create a specification by inspecting HTTP traffic you will need to:Capture the traffic by using the mitmproxy tool. I personally recommend using mitmweb, which is a web interface built-in to mitmproxy.$mitmweb
Webserverlisteningathttp://127.0.0.1:8081/
Proxyserverlisteningathttp://*:9999
...IMPORTANTTo configure your client to use the proxy exposed by mitm proxy, please consult themitmproxy documentationfor more information.Save the traffic to a flow file.In mitmweb you can do this by using the "File" menu and selecting "Save":Run the first pass of mitmproxy2swagger:$mitmproxy2swagger-i<path_to_mitmptoxy_flow>-o<path_to_output_schema>-p<api_prefix># ... or ...$dockerrun-it-v$PWD:/appmitmproxy2swaggermitmproxy2swagger-i<path_to_mitmptoxy_flow>-o<path_to_output_schema>-p<api_prefix>Please note that you can use an existing schema, in which case the existing schema will be extended with the new data. You can also run it a few times with different flow captures, the captured data will be safely merged.<api_prefix>is the base url of the API you wish to reverse-engineer. You will need to obtain it by observing the requests being made in mitmproxy.For example if an app has made requests like these:https://api.example.com/v1/loginhttps://api.example.com/v1/users/2https://api.example.com/v1/users/2/profileThe likely prefix ishttps://api.example.com/v1.Running the first pass should have created a section in the schema file like this:x-path-templates:# Remove the ignore: prefix to generate an endpoint with its URL# Lines that are closer to the top take precedence, the matching is greedy-ignore:/addresses-ignore:/basket-ignore:/basket/add-ignore:/basket/checkouts-ignore:/basket/coupons/attach/{id}-ignore:/basket/coupons/attach/104754You should edit the schema file with a text editor and remove theignore:prefix from the paths you wish to be generated. You can also adjust the parameters appearing in the paths.Run the second pass of mitmproxy2swagger:$mitmproxy2swagger-i<path_to_mitmptoxy_flow>-o<path_to_output_schema>-p<api_prefix>[--examples]# ... or ...$dockerrun-it-v$PWD:/appmitmproxy2swaggermitmproxy2swagger-i<path_to_mitmptoxy_flow>-o<path_to_output_schema>-p<api_prefix>[--examples]Run the command a second time (with the same schema file). It will pick up the edited lines and generate endpoint descriptions.Please note that mitmproxy2swagger will not overwrite existing endpoint descriptions, if you want to overwrite them, you can delete them before running the second pass.Passing--exampleswill add example data to requests and responses. Take caution when using this option, as it may add sensitive data (tokens, passwords, personal information etc.) to the schema.
Passing--headerswill add headers data to requests and responses. Take caution when using this option, as it may add sensitive data (tokens, passwords, personal information etc.) to the schema.HARCapture and export the traffic from the browser DevTools.In the browser DevTools, go to the Network tab and click the "Export HAR" button.Continue the same way you would do with the mitmproxy dump.mitmproxy2swaggerwill automatically detect the HAR file and process it.Example outputSee theexamples. You will find a generated schema there and an html file with the generated documentation (viaredoc-cli).See the generated html filehere.Development and contributingThis project uses:poetryfor dependency managementpre-commitfor code formatting and lintingpytestfor unit testingTo install the dependencies:poetryinstallRun linters:pre-commitrun--all-filesInstall pre-commit hooks:pre-commitinstallRun tests:poetryrunpytestRun tests with coverage:poetryrunpytest--cov=mitmproxy2swaggerLicenseMIT