Files
AI-Red-Teaming-CSCD94/application-and-system-attacks/reverse-engineering/model-stealer.ipynb
T
Jeremy Janella 75faa5c410 added material
2026-05-09 23:21:13 -04:00

187 lines
4.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8d4f4ce4-7136-40b4-bb26-2282a70c01ed",
"metadata": {},
"outputs": [],
"source": [
"N_SAMPLES = 100\n",
"\n",
"MIN_FLIPPER_LENGTH = 150\n",
"MAX_FLIPPER_LENGTH = 250\n",
"\n",
"MIN_BODY_MASS = 2500\n",
"MAX_BODY_MASS = 6500\n",
"\n",
"CLASSIFIER_URL = \"http://154.57.164.64:31234/\"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "aae0ccdc-0d16-4ac4-9e4e-349ca917e0d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Flipper Length (mm) Body Mass (g)\n",
"0 225.360814 4507.338188\n",
"1 155.862659 5086.012537\n",
"2 202.298517 5345.538155\n",
"3 151.772929 5325.544831\n",
"4 157.298876 5988.681592\n"
]
}
],
"source": [
"import random\n",
"import pandas as pd\n",
"\n",
"samples = {\n",
" \"Flipper Length (mm)\": [],\n",
" \"Body Mass (g)\": []\n",
"}\n",
"\n",
"for i in range(N_SAMPLES):\n",
" samples[\"Flipper Length (mm)\"].append(random.uniform(MIN_FLIPPER_LENGTH, MAX_FLIPPER_LENGTH))\n",
" samples[\"Body Mass (g)\"].append(random.uniform(MIN_BODY_MASS, MAX_BODY_MASS))\n",
"\n",
"samples_df = pd.DataFrame(samples)\n",
"print(samples_df.head())\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b06a1bf2-f465-4bfc-af92-5ef3a85efab0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" species\n",
"0 Gentoo\n",
"1 Adelie\n",
"2 Gentoo\n",
"3 Adelie\n",
"4 Adelie\n"
]
}
],
"source": [
"import requests\n",
"import json\n",
"\n",
"predictions = {\"species\": []}\n",
"\n",
"for i in range(N_SAMPLES):\n",
" sample = {\n",
" \"flipper_length\": samples[\"Flipper Length (mm)\"][i],\n",
" \"body_mass\": samples[\"Body Mass (g)\"][i]\n",
" }\n",
"\n",
" prediction = json.loads(requests.get(CLASSIFIER_URL, params=sample).text).get(\"result\")\n",
" predictions[\"species\"].append(prediction)\n",
"\n",
"predictions_df = pd.DataFrame(predictions)\n",
"print(predictions_df.head())\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "19f9f2b1-dc61-4986-ae74-158a19bd8870",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jeremy/.conda/envs/ai/lib/python3.11/site-packages/sklearn/utils/validation.py:1352: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
" y = column_or_1d(y, warn=True)\n"
]
},
{
"data": {
"text/plain": [
"['surrogate.joblib']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.linear_model import LogisticRegression\n",
"import joblib\n",
"\n",
"surrogate_model = make_pipeline(StandardScaler(), LogisticRegression())\n",
"surrogate_model.fit(samples_df, predictions_df)\n",
"\n",
"# save classifier to a file\n",
"joblib.dump(surrogate_model, 'surrogate.joblib')\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ddd1763f-dc46-4d40-b7bb-c9e866fdb632",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'accuracy': 0.9817518248175182, 'flag': 'HTB{ff08c0bb37e16f30a0804053a4de70ed}'}\n"
]
}
],
"source": [
"with open('surrogate.joblib', 'rb') as f:\n",
" file = f.read()\n",
"\n",
"r = requests.post(CLASSIFIER_URL + '/model', files={'file': ('surrogate.joblib', file)})\n",
"\n",
"print(json.loads(r.text))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7093628b-db4e-4f8f-a56e-69882f564303",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}