-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnexposeAPILinqpadExample
More file actions
222 lines (187 loc) · 7.32 KB
/
nexposeAPILinqpadExample
File metadata and controls
222 lines (187 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
void Main()
{
//api guid: https://community.rapid7.com/docs/DOC-1896
string pw = "";
//Server PORT accessUser accessUser'sPW UserToModify NewPassword
var nexpose = new Nexpose("servername or IP", "port", "adminUser", pw);
nexpose.GetUserId("dummyuser").Dump();
nexpose.Disconnect();
// nexpose.Connect();
}
// Define other methods and classes here
public class Nexpose{
private string apiVersion = "api/1.1/xml";
private string url;
private string sessionId {get;set;}
public Nexpose(string nexposeServer, string port, string username, string password){
//since we were given all the paramenters call Connect...
Connect(nexposeServer, username,password, port);
}
public Nexpose(){
}
public bool Connect(string nexposeServer, string username, string password, string port){
this.url = "https://"+nexposeServer+":"+port+"/"+apiVersion;
//Create XML for the login response
string xml = nexposeLoginXML(username,password);
string response = sendXMLHTTP(xml);
if(response == null){
return false;
}
var loginResonseXml = XDocument.Parse(response);
//set session ID
sessionId = attributeValue(loginResonseXml.Element("LoginResponse"),"session-id");
if(sessionId != "" && sessionId != null){
return true;
}
return false;
}
public bool Disconnect(){
//create XML request
var req = genericNexposeXMLRequest("LogoutRequest","session-id",sessionId);
string response = sendXMLHTTP(req);
if(response == null){
return false;
}
var outResonseXml = XDocument.Parse(response);
this.sessionId = "";
return true;
}
public List<XElement> ListUsers(){
var userList = XDocument.Parse(sendXMLHTTP(genericNexposeXMLRequest("UserListingRequest","session-id", sessionId)));
var users= userList.Elements("UserListingResponse").Elements();
return users.ToList();
}
public string ChangeUserPassword(string username, string newPassword){
var userID = GetUserId(username);
var userDetails = GetUserDetails(userID);
string roleName = attributeValue(userDetails.Elements("UserConfig").ElementAt(0),"role-name");
string authsrcid = attributeValue(userDetails.Elements("UserConfig").ElementAt(0),"authsrcid");
string name = attributeValue(userDetails.Elements("UserConfig").ElementAt(0),"name");
string fullname = attributeValue(userDetails.Elements("UserConfig").ElementAt(0),"fullname");
var xml = NexposeUserSaveRequest(sessionID,Convert.ToInt32( userIdNumber),roleName,authsrcid,name,fullname, newPassword);
return sendXMLHTTP(xml);
}
private string NexposeUserSaveRequest(string sessionID, string userId,string rolename, string authcid, string name,string fullname, string pw){
XDocument doc = new XDocument(
///new XDeclaration("1.0", "utf-8", null),
new XElement( "UserSaveRequest",
new XAttribute("session-id",sessionID),
new XElement("UserConfig",
new XAttribute("id",userId),
new XAttribute("role-name",rolename),
new XAttribute("authsrcid",authcid),
new XAttribute("password", pw),
new XAttribute("fullname", fullname),
new XAttribute("enabled", "1"),
new XAttribute("name",name))
)
);
var sb = new StringBuilder();
var wr = new ExtentedStringWriter(sb, Encoding.UTF8);
doc.Dump();
return doc.ToString();
}
public XElement GetUserDetails(string userId){
var xml = NexposeUserConfigRequest(sessionId,userId);
var UserConfigResponse = XDocument.Parse(sendXMLHTTP(x));
var temp = UserConfigResponse.Elements("UserConfigResponse").ElementAt(0);
return temp;
return null;
}
public string GetUserId(string username){
var users = ListUsers();
var foundUser= users.FirstOrDefault (u => u.Attribute("userName").Value == username);
return foundUser.Attribute("id").Value;
}
private string nexposeLoginXML(string username, string pw){
XDocument doc = new XDocument(
///new XDeclaration("1.0", "utf-8", null),
new XElement( "LoginRequest",
new XAttribute("user-id",username),
new XAttribute("password",pw.ToString()))
);
var sb = new StringBuilder();
var wr = new ExtentedStringWriter(sb, Encoding.UTF8);
return doc.ToString();
}
private string NexposeUserConfigRequest(string sessionID, string userid){
XDocument doc = new XDocument(
///new XDeclaration("1.0", "utf-8", null),
new XElement( "UserConfigRequest", new XAttribute("session-id",sessionID), new XAttribute("id",userid))
);
var sb = new StringBuilder();
var wr = new ExtentedStringWriter(sb, Encoding.UTF8);
return doc.ToString();
}
//most nexpose requests just need one xml element with an attribute for the session id...
// it would be nice to make this a bit more generic with variable function parameters....
private string genericNexposeXMLRequest(string elementName, string attributeName, string attributeValue){
XDocument doc = new XDocument(
///new XDeclaration("1.0", "utf-8", null),
new XElement( elementName,
new XAttribute(attributeName,attributeValue))
);
var sb = new StringBuilder();
var wr = new ExtentedStringWriter(sb, Encoding.UTF8);
return doc.ToString();
}
private string sendXMLHTTP(string xml)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(xml);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
//the cert is a self cert so is not valid on clients....
request.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback
(delegate { return true; });
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
//XmlDocument xDoc = new XmlDocument();
//XmlTextReader myXmlReader = new XmlTextReader(responseStream);
//xDoc.Load(myXmlReader);
//return xDoc;
string responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}
return null;
}
// retrieve attribute by name
// https://stackoverflow.com/questions/20031852/parse-xml-string-using-c-sharp
private string attributeValue(XElement item, string attributeName)
{
var attribute = item.Attribute(attributeName);
return attribute != null ? attribute.Value : string.Empty;
}
// retrieve a single element by tag name
// https://stackoverflow.com/questions/20031852/parse-xml-string-using-c-sharp
private XElement getElement(XDocument xmlDocument, string elementName)
{
var element = xmlDocument.Descendants("items").Elements().Where (x => x.Name == elementName).FirstOrDefault();
return element != null ? element : null;
}
//https://blog.jsinh.in/use-utf-8-encoding-for-stringwriter-in-c/#.WS24AVUrKHs
public sealed class ExtentedStringWriter : StringWriter
{
private readonly Encoding stringWriterEncoding;
public ExtentedStringWriter(StringBuilder builder, Encoding desiredEncoding)
: base(builder)
{
this.stringWriterEncoding = desiredEncoding;
}
public override Encoding Encoding
{
get
{
return this.stringWriterEncoding;
}
}
}
}